Create dummy variable for below or above median within group r

Suppose I have a dataframe like the following: X Y Z 1 b 3 2 a 8 3 a 7 4 c 1 5 b 6 6 a 4 7 a 9 8 b 5 9 a 4 I want to create columns A and B, which are dummy variables for if the value of… Read More Create dummy variable for below or above median within group r

how to put multiple cols into long format base on suffix of variable names

I have wide df with multiple measurements. I would like to change wide to long. How should I do this. I know how to do 2 cols, but not multiples. Could someone guide me on this? Input on the top, and ideal output on the bottom: df<-structure(list(Subject = c("Tom", "Tom", "Tom", "Tom", "Tom", "Tom", "Tom",… Read More how to put multiple cols into long format base on suffix of variable names

Add total rows to data frame by group using two grouping variables in R

Suppose I have the data frame like the one below with two grouping variables "Group" and "Gender" and two additional variables with counts: Group <- c("Group1","Group1","Group2","Group2") Gender <- c("Male","Female","Male","Female") Y <- c(7,5,6,10) N <- c(45,8,2,11) data <- cbind.data.frame(Group,Gender,Y,N) > data Group Gender Y N 1 Group1 Male 7 45 2 Group1 Female 5 8 3… Read More Add total rows to data frame by group using two grouping variables in R

R pattern for updating a df column based on another df, when present

I often find myself wanting to update a data frame based on a separate data frame that has new values for a subset of columns and rows. For example: library(dplyr) df_original <- data.frame( id=c(1,2,3), name=c("John", "Rose", "Kanaya"), address=c("100 Street st.", "413 Old St.", "200 Drive Dr.") ) df_newinfo <- data.frame(id=c(2), address=c("612 New St.")) I want… Read More R pattern for updating a df column based on another df, when present

Is there an R function to do grouped operations on a data frame without collapsing it?

Apologies if the question isn’t formulated correctly in the title, I am fairly new to this and still not used to the exact terminology. I need to add a column to a data frame that contains the result of operations (e.g., mean, sum, etc.) grouped by values in other columns. At the same time, I… Read More Is there an R function to do grouped operations on a data frame without collapsing it?