【问题标题】:R & dplyr - grouping and adding new columnR & dplyr - 分组和添加新列
【发布时间】:2019-10-02 08:35:22
【问题描述】:

我在 R 中有下面提到的代码,我正在尝试添加一个新列,它应该是按变量分组的第一列中的值的总和。

我使用了 dplyr 包和 mutate 函数,但不幸的是,我在应用代码时收到以下警告消息:

total_tests$total <- total_tests %>% group_by(school_id) %>% mutate(total=sum(distinct_tests)) 

警告信息: 在 cbind(x[0:(framecol - 1)], cols) : 结果的行数不是向量长度的倍数(arg 1)

前 20 行的 Dput 输出:

structure(list(distinct_tests = c(121L, 7L, 32L, 12L, 1L, 1L, 
1L, 1L, 2L, 4L, 3L, 15L, 1L, 5L, 49L, 2L, 2L, 3L, 1L, 38L), test_type = structure(c(2L, 
2L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L), .Label = c("EXAM", "HW", "SELF_SERVICE", "SHORT_TEST"
), class = "factor"), school_id = structure(c(113L, 113L, 113L, 
113L, 113L, 217L, 217L, 217L, 217L, 20L, 20L, 21L, 21L, 21L, 
84L, 84L, 84L, 84L, 94L, 94L), .Label = c("1000", "1002", "1003", 
"1004", "1006", "1007", "1008", "1010", "1011", "1012", "1013", 
"1014", "1015", "1019", "1020", "1021", "1022", "1023", "1024", 
"103", "104", "1042", "1043", "1044", "1045", "1053", "1054", 
"1056", "1057", "1058", "1059", "1060", "1061", "1062", "1063", 
"1064", "1065", "1066", "1068", "1069", "1070", "1071", "1072", 
"1073", "1074", "1075", "1076", "1077", "1078", "1155", "1156", 
"1157", "1158", "1159", "1176", "1217", "1227", "1228", "1234", 
"1235", "1257", "1261", "1262", "1263", "1264", "1265", "1266", 
"1267", "1268", "1273", "1274", "1275", "1276", "1277", "1278", 
"1279", "1281", "1282", "1305", "1306", "1343", "1344", "1414", 
"144", "1560", "1593", "1612", "1614", "1645", "1646", "1650", 
"1653", "1654", "166", "167", "1676", "1677", "1679", "1681", 
"1682", "1683", "1685", "1696", "1711", "1773", "186", "1871", 
"1912", "1914", "2196", "2217", "2280", "23", "2301", "264", 
"2640", "2642", "2667", "2668", "2720", "2721", "2746", "2791", 
"284", "285", "2872", "2888", "304", "3044", "3184", "3195", 
"3220", "3221", "3222", "3224", "3225", "3238", "3307", "3324", 
"3347", "3362", "346", "3489", "3496", "3511", "3516", "3591", 
"366", "368", "369", "3749", "3771", "3849", "386", "387", "388", 
"3886", "389", "390", "3912", "3913", "392", "393", "3936", "3937", 
"394", "395", "396", "397", "399", "400", "4026", "4032", "4049", 
"4062", "4072", "4147", "424", "428", "430", "4310", "432", "433", 
"434", "464", "484", "485", "486", "487", "488", "525", "526", 
"528", "546", "548", "564", "565", "566", "567", "568", "569", 
"584", "585", "586", "589", "590", "591", "593", "594", "595", 
"596", "626", "627", "645", "646", "647", "68", "686", "688", 
"705", "744", "745", "746", "747", "748", "749", "765", "784", 
"785", "786", "788", "789", "805", "807", "808", "809", "810", 
"811", "812", "813", "816", "817", "818", "819", "820", "821", 
"822", "824", "828", "829", "830", "831", "832", "833", "834", 
"835", "836", "837", "838", "840", "841", "843", "844", "845", 
"846", "847", "849", "850", "851", "852", "853", "855", "856", 
"857", "860", "863", "864", "865", "866", "867", "868", "869", 
"870", "871", "872", "875", "877", "878", "879", "881", "882", 
"884", "885", "886", "909", "910", "912", "916", "917", "925", 
"929", "930", "933", "938", "939", "941", "944", "948", "954", 
"955", "957", "962", "963", "967", "968", "969", "973", "974", 
"975", "977", "978", "979", "981", "NULL"), class = "factor")), row.names = c(NA, 
20L), class = "data.frame")

【问题讨论】:

  • 您必须将指令分配给数据框,而不是列total_tests &lt;- total_tests %&gt;% group_by(school_id) %&gt;% mutate(total=sum(distinct_tests))
  • 这很好,但是如果我使用这一行 total_tests &lt;- total_tests %&gt;% group_by(school_id) %&gt;% mutate(total=sum(distinct_tests)) 我会得到第四列的总和,而不是分组的总和。
  • 这很奇怪,因为我得到了带有分组总和的第四列。
  • 很奇怪,我已经清理了空间并再次加载了 dplyr 库。这次它确实奏效了,但我发誓在输出之前是不同的。无论如何。感谢您的帮助!
  • 有时可能会发生你已经加载了一些包,如plyr,其函数名称相同,如mutate,如果你没有指定包(通过dplyr::mutate),R考虑到错误的功能。

标签: r dplyr


【解决方案1】:

根据 iago 的评论,您还需要使用 summarise 函数而不是 mutate 来确保正确汇总输出。

totals <- total_tests %>% 
  group_by(school_id) %>% 
  summarise(total=sum(distinct_tests)) 

【讨论】:

  • 你是对的!但我想按组细分总和(在本例中为 school_id)。这就是为什么,我考虑过使用mutate而不是summarise
【解决方案2】:

由于您已经在使用mutate,因此您不需要使用total_tests$total 来创建新列,因为函数mutate 已经这样做了。所以,我想你可以试试这个:

total_tests <- total_tests %>% group_by(school_id) %>% mutate(total=sum(distinct_tests)) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多