R:如何将x聚合并分离为多个列?乐趣=独特
我正在按教育程度和id_学习汇总data.frame,我使用的功能是“独特的”R:如何将x聚合并分离为多个列?乐趣=独特,r,aggregate,unique,multiple-columns,data-cleaning,R,Aggregate,Unique,Multiple Columns,Data Cleaning,我正在按教育程度和id_学习汇总data.frame,我使用的功能是“独特的” 我已经颠倒了公式中变量的顺序,它似乎做了你想要的 首先,一些数据准备代码,为它指定列名并强制类data.frame colnames(steps) <- c("id_study", "edu") steps <- as.data.frame(steps) db2 <- aggregate (id_study ~ edu, data = steps, FUN = unique) db2 # edu
我已经颠倒了公式中变量的顺序,它似乎做了你想要的 首先,一些数据准备代码,为它指定列名并强制类
data.frame
colnames(steps) <- c("id_study", "edu")
steps <- as.data.frame(steps)
db2 <- aggregate (id_study ~ edu, data = steps, FUN = unique)
db2
# edu id_study
#1 11 DZA_2003_STEPS
#2 12 DZA_2003_STEPS
#3 3 DZA_2003_STEPS
#4 6 DZA_2003_STEPS
#5 7 DZA_2003_STEPS
#6 9 DZA_2003_STEPS
colnames(steps)可能列edu
是一个因子,内部编码为整数。尝试as.character(edu)
。您可以发布示例数据吗?请使用dput(步骤)
的输出编辑问题。或者,如果输出的dput(head(steps,20))
@RuiBarradas太大,我已经按照要求进行了更新
structure(c("DZA_2003_STEPS", "DZA_2003_STEPS", "DZA_2003_STEPS",
"DZA_2003_STEPS", "DZA_2003_STEPS", "DZA_2003_STEPS", "DZA_2003_STEPS",
"DZA_2003_STEPS", "DZA_2003_STEPS", "DZA_2003_STEPS", "DZA_2003_STEPS",
"DZA_2003_STEPS", "DZA_2003_STEPS", "DZA_2003_STEPS", "DZA_2003_STEPS",
"DZA_2003_STEPS", "DZA_2003_STEPS", "DZA_2003_STEPS", "DZA_2003_STEPS",
"DZA_2003_STEPS", "9", "7", "6", "9", "7", "6", "3", "12", "3",
"3", "3", "3", "3", "6", "6", "3", "3", "6", "11", "7"), .Dim = c(20L,
2L))
[,id_study] [edu]
[1,] "DZA_2003_STEPS" "9"
[2,] "DZA_2003_STEPS" "7"
[3,] "DZA_2003_STEPS" "6"
[4,] "DZA_2003_STEPS" "9"
[5,] "DZA_2003_STEPS" "7"
[6,] "DZA_2003_STEPS" "6"
[7,] "DZA_2003_STEPS" "3"
[8,] "DZA_2003_STEPS" "12"
[9,] "DZA_2003_STEPS" "3"
[10,] "DZA_2003_STEPS" "3"
[11,] "DZA_2003_STEPS" "3"
[12,] "DZA_2003_STEPS" "3"
[13,] "DZA_2003_STEPS" "3"
[14,] "DZA_2003_STEPS" "6"
[15,] "DZA_2003_STEPS" "6"
[16,] "DZA_2003_STEPS" "3"
[17,] "DZA_2003_STEPS" "3"
[18,] "DZA_2003_STEPS" "6"
[19,] "DZA_2003_STEPS" "11"
[20,] "DZA_2003_STEPS" "7"
colnames(steps) <- c("id_study", "edu")
steps <- as.data.frame(steps)
db2 <- aggregate (id_study ~ edu, data = steps, FUN = unique)
db2
# edu id_study
#1 11 DZA_2003_STEPS
#2 12 DZA_2003_STEPS
#3 3 DZA_2003_STEPS
#4 6 DZA_2003_STEPS
#5 7 DZA_2003_STEPS
#6 9 DZA_2003_STEPS