Casting data in R -
i have data set 4 different treatments listed in 1 column. treatment 1, treatment 2, treatment 3, , treatment 4 results displayed next in column. trying cast data can run anova see treatment effective. when try using cast function error message. how sepperate treatments 4 separate columns how effective in column next it. there column observation number listed.
i'd this,
your data:
df <- data.frame(group = rep(letters[1:4], 4), scores = runif(16))
assuming data 2 columns, 1 column specifying group , values in it. first bit of below count occurrences of each group , spreads data accordingly.
library(tidyr) library(dplyr) df %>% group_by(group) %>% mutate(count = row_number()) %>% spread(group, scores)
result:
count b c d 1 1 0.6211185 0.2278333 0.68751500 0.03104363 2 2 0.4507366 0.1834150 0.05700584 0.18217047 3 3 0.3411344 0.4382062 0.23057716 0.85572707 4 4 0.8514964 0.8386036 0.95319578 0.76257128
your values vary because these randomly generated , forgot set.seed
. if have uneven number of cases per group, fill empty cells nas.
Comments
Post a Comment