A MWE.
df <- tibble(ID = c(1:12),
Group = c(rep(1,3), rep(2,3), rep(3,3), rep(4,3)),
Choice = c( rep(1.5, 3), round(rnorm(n=9,mean=10,sd=5), digits = 1) )
)
What I want is to identify, in each group, whether the group members decisions are the same or not. The desired output should be something like:
df <- tibble(ID = c(1:12),
Group = c(rep(1,3), rep(2,3), rep(3,3), rep(4,3)),
Choice = c( rep(1.5, 3), round(rnorm(n=9,mean=10,sd=5), digits = 1) ),
Symmetric = c ( rep(1, 3), rep(0, 9) )
)
Is there a fast and simple solution for this? Thanks a lot!
Couple of ideas -
library(dplyr)
df %>% mutate(Symmetric = as.integer(n_distinct(Choice) == 1), .by = Group)
In base R :
transform(df, Symmetric = as.integer(ave(Choice, Group, FUN = \(x) length(unique(x)) == 1)))
Choice
is a numeric value we can also check it's standard deviation sd
df %>% mutate(Symmetric = as.integer(sd(Choice) == 0), .by = Group)
In base R :
transform(df, Symmetric = as.integer(ave(Choice, Group, FUN = sd) == 0))
All of which return :
# ID Group Choice Symmetric
#1 1 1 1.5 1
#2 2 1 1.5 1
#3 3 1 1.5 1
#4 4 2 9.7 0
#5 5 2 -0.2 0
#6 6 2 12.9 0
#7 7 3 23.7 0
#8 8 3 2.3 0
#9 9 3 12.5 0
#10 10 4 7.5 0
#11 11 4 2.0 0
#12 12 4 14.0 0