I have a data set with a column names and a binary (0/1) column variable called column1. I would like to add three columns:
- percentage, that summarizes for each person all 1's and divide it by total for that person, thus every 1's + 0's. For John, this value would be 0.667 (2/3), for Rick this would be 0.5 (1/2) etc.
- total1and- total2as summations of respectively the 1's or 0's from the- column1, again for each person in- names.
Example script + desired output. How to best approach this?
names <- c("John", "John", "John", "John", "Rick", "Rick", "Katie", "Katie", "Katie", "Harry", "Harry" )
column1 <- c(1,0,1,NA,0,1,1,1,0,1,1)
df1 <- data.frame(names,column1)
#Desired ouput
#names  column1  percentage   total1  total0
#John   1        0.667        2       1
#John   0        0.667        2       1
#John   1        0.667        2       1
#John   NA       0.667        2       1
#Rick   0        0.500        1       1
#Rick   1        0.500        1       1
#Katie  1        0.667        2       1  
#Katie  1        0.667        2       1
#Katie  0        0.667        2       1
#Harry  1        1.000        2       0
#Harry  1        1.000        2       0
 
     
    