“想要的”结果由下面的“执行”功能给出。我以为我可以通过使用一些嵌套来获得相同的效果,但是无法使其正常工作。
library(dplyr)
library(tidyr)
# Function rr is given
rr = function(x){
# This should be an expensive and possibly random function
r = range(x + rnorm(length(x),0.1))
# setNames(r, c("min", "max")) # fails, expecting single value
# list(min = r[1], max= r[2]) # fails
list(r) # Works, but result is in "long" form without min/max
}
# Works, but syntactically awkward
iris %>% group_by(Species) %>%
do( {
r = rr(.$Sepal.Width)[[1]]
data_frame(min = r[1], max = r[2])
})
# This give the long format, but without column
# names min/max
iris %>% group_by(Species) %>%
summarize(
range = rr(Sepal.Length)
) %>% unnest(range)
Unnest()
会始终以“长”格式取消列出嵌套列,但spread()
如果您创建key
列,则可以用来获取所需的输出。
library(dplyr)
library(tidyr)
iris %>%
group_by(Species) %>%
summarize(range = rr(Sepal.Length)) %>%
unnest(range) %>% mutate(newcols = rep(c("min", "max"), 3)) %>%
spread(newcols, range)
# Species max min
# (fctr) (dbl) (dbl)
#1 setosa 7.636698 3.292692
#2 versicolor 9.792319 3.337382
#3 virginica 9.810723 3.367066
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句