I am trying to create a new column by dividing a column A of integers (data1/2/3 below) by the mode of column A when grouped by another column B of integers (group1/2 below)
group1=rep(1:5,each=2)
group2=rep(6:10, each=2)
data1=c(1,1,1,1,1,4,5,6,3,8)
data2=c(5,4,5,7,8,5,2,1,1,5)
data3=c(6,6,8,9,5,4,3,3,1,1)
DF=data.frame(group1,group2,data1,data2,data3)
group1 group2 data1 data2 data3
1 1 6 1 5 6
2 1 6 1 4 6
3 2 7 1 5 8
4 2 7 1 7 9
5 3 8 1 8 5
6 3 8 4 5 4
7 4 9 5 2 3
8 4 9 6 1 3
9 5 10 3 1 1
10 5 10 8 5 1
I have been successful in doing this one column at a time (see code below), but I would like to be able to generalize it:
DF %>%
group_by(group2) %>%
mutate(group2_mode = as.integer(head(names(sort(table(data2))),1))) %>%
mutate(group2_data2 = data2/group2_mode) %>%
#select(-c(group1_mode)) %>%
ungroup()
# A tibble: 10 x 7
group1 group2 data1 data2 data3 group2_mode group2_data2
<int> <int> <dbl> <dbl> <dbl> <int> <dbl>
1 1 6 1 5 6 4 1.25
2 1 6 1 4 6 4 1
3 2 7 1 5 8 5 1
4 2 7 1 7 9 5 1.4
5 3 8 1 8 5 5 1.6
6 3 8 4 5 4 5 1
7 4 9 5 2 3 1 2
8 4 9 6 1 3 1 1
9 5 10 3 1 1 1 1
10 5 10 8 5 1 1 5
This works but is clunky when written out for each data/group combination.
I have tried iterating through for loops as follows:
for (i in colnames(DF[,3:5])){
for (k in colnames(DF[,1:2])){
DF %>%
group_by(k) %>%
mutate(paste(c(k,"_",i), collapse = '') <- i/as.integer(head(names(sort(table(i))),1)))
}
}
And receive the following error:
Error: Column `k` is unknown
I expect the output to be similar to the first code chunk above but for each data/group combination. I have also tried labeling all of the mutated columns in the for loop the same thing, but that also results in the same error. I suspect the issue lies in the group_by statement, but I can't figure out how.
Thank you for your time