[ad_1]
I have a data frame:
set.seed(100)
A <- floor(runif(5, min=0, max=10))
B <- floor(runif(5, min=0, max=10))
C <- floor(runif(5, min=0, max=10))
D <- floor(runif(5, min=0, max=10))
df <- data.frame(A,B,C,D)
df$ms <- rowMeans(df)
df
A B C D ms
1 3 4 6 6 4.75
2 2 8 8 2 5.00
3 5 3 2 3 3.25
4 0 5 3 3 2.75
5 4 1 7 6 4.50
Now I’d like to add columns (lower and greater) with column names when the value in particular row is lower in columns A and B than mean and greater in columns C and D also than mean. Desired result:
A B C D ms lower greater
1 3 4 6 6 4.75 A,B C,D
2 2 8 8 2 5.00 A C
3 5 3 2 3 3.25 B NA
4 0 5 3 3 2.75 A NA
5 4 1 7 6 4.50 A,B C,D
I was trying to do this with which()
however I stuck, could you please give me a hint?
lapply(apply(df,1, function(x) which(df$ms)),names)
[ad_2]