I have a data as shown below:
Data | Flag | Answer |
---|---|---|
X | 1 | Yes |
X | 2 | Yes |
Y | 1 | Yes |
Y | 2 | No |
Z | 1 | Yes |
Z | 2 | Yes |
and I want to convert it to:
Data | 1 | 2 |
---|---|---|
X | Yes | Yes |
Y | Yes | No |
Z | Yes | Yes |
I know I have to use dcast function but I am not able to manipulate the data to the desired form. Help needed.
Thanks.
CodePudding user response:
reshape2::dcast(dat, Data ~ Flag, value.var = "Answer")
# Data 1 2
# 1 X Yes Yes
# 2 Y Yes No
# 3 Z Yes Yes
Data
dat <- structure(list(Data = c("X", "X", "Y", "Y", "Z", "Z"), Flag = c(1L, 2L, 1L, 2L, 1L, 2L), Answer = c("Yes", "Yes", "Yes", "No", "Yes", "Yes")), class = "data.frame", row.names = c(NA, -6L))