[英]R: how to aggregate and separate x into multiple columns? FUN = unique
我正在按edu(教育级别)和id_study聚合一个data.frame,而我正在使用的功能是“唯一的”。
db1 <- aggregate (edu ~ id_study, data=steps, FUN=unique)
我得到的结果是正确的,但是(用于edu的)字符串被转换为数字元素,并且都在一列中。 我该如何纠正? 正确答案的人会打勾!
谢谢大家阅读本文并提供帮助。
编辑****************以下是数据集。
小号
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"
我已经颠倒了公式中变量的顺序,这似乎可以满足您的要求。
首先,提供一些数据准备代码,为其指定列名称并强制使用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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.