[英]dplyr group_by() with summarise() not working as *I* exoect
I realize this is operator-error but I've been searching the documentation and this site for an answer and I can't figure it out.我意识到这是操作员错误,但我一直在搜索文档和此站点以寻找答案,但我无法弄清楚。
Here's my data:这是我的数据:
df <- structure(list(ID= c("A757EHpLOya", "A757EHpLOya", "A757EHpLOya",
"A757EHpLOya", "A757EHpLOya", "AcjfpLUXjwt", "AcjfpLUXjwt", "AcjfpLUXjwt",
"AcjfpLUXjwt", "AcjfpLUXjwt", "AcjfpLUXjwt", "AcjfpLUXjwt", "AcjfpLUXjwt",
"AcjfpLUXjwt", "AcjfpLUXjwt", "aHNXoYj7uNJ", "aHNXoYj7uNJ", "aHNXoYj7uNJ",
"aHNXoYj7uNJ", "aitNX6Qxkon", "aitNX6Qxkon", "As7tGowP84e", "As7tGowP84e",
"As7tGowP84e"), group= structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L), .Label = c("1", "2", "3", "4", "5", "6"), class = "factor"),
year = c(2018, 2018, 2018, 2018, 2018, 2018, 2018,
2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
2018, 2018, 2018, 2018, 2018, 2018, 2018), sex = c("Female",
"Female", "Female", "Male", "Unknown Sex", "Female", "Female",
"Female", "Female", "Male", "Male", "Male", "Male", "Unknown Sex",
"Unknown Sex", "Female", "Female", "Female", "Male", "Female",
"Female", "Female", "Female", "Female"), agecat = structure(c(2L,
3L, 6L, 6L, 1L, 2L, 2L, 3L, 6L, 2L, 2L, 3L, 6L, 1L, 1L, 2L,
3L, 6L, 6L, 3L, 6L, 2L, 3L, 6L), .Label = c("1", "2", "3",
"4", "5", "6"), class = "factor"), value = c(10, 18, 30, 18,
16, 55, 89, 281, 418, 71, 35, 37, 295, 11, 189, 10, 37, 94,
53, 13, 12, 1, 3, NA)), row.names = c(NA, 24L), class = "data.frame")
Which looks like this:看起来像这样:
My goal is to select just one ID and then create a random variable for it, so there's ONE row for each ID:我的目标是 select 只有一个 ID,然后为其创建一个随机变量,因此每个 ID 都有一行:
mu = 0.25
sd = 0.05
IDmu <- data.frame(df %>%
group_by(ID) %>%
summarise(p = rnorm(n(), mean=mu, sd=sd)) %>%
select(ID, p))
But this gives me this:但这给了我这个:
I suspect this is because of n() but I've tried length(), nrow(), count() and can't get it to work.我怀疑这是因为 n() 但我尝试了 length()、nrow()、count() 并且无法让它工作。 So, this is unveiling more fundamental non-understanding of the tidyverse on my part.
因此,这揭示了我对 tidyverse 更根本的非理解。
Thanks!谢谢!
Thanks to @akrun, the correct code is:感谢@akrun,正确的代码是:
IDmu <- data.frame(df %>%
group_by(ID) %>%
summarise(p = rnorm(1, mean=mu, sd=sd)) %>%
select(ID, p))
I lost sight of the fact that I later want to use the value of n() for something else but there's probably a better way to do that then here.我忽略了这样一个事实,即我后来想将 n() 的值用于其他事情,但这里可能有更好的方法来做到这一点。
Thanks!谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.