带有 seq() 的 R dplyr

艾琳

我需要使用另一列的 seq 对数据框中的一列进行变异。例如iris,我想为每个物种添加一个新列,seq(min(Sepal.Length),max(Sepal.Length),length=100)

我试过(没有成功):

 iris %>% 
     group_by(Species) %>% 
     mutate(seqq = seq(min(Sepal.Length),max(Sepal.Length),  100))

有任何想法吗?

谢谢你!

阿克伦

mutate需要返回与原始数据或 group_by 中的行数相同的行数。我们可能会使用summarise

library(dplyr)
iris %>% 
     group_by(Species) %>% 
     summarise(seq = seq(min(Sepal.Length),max(Sepal.Length), 
         length = 100), .groups = 'drop')

-输出

# A tibble: 300 x 2
# Groups:   Species [3]
   Species   seq
   <fct>   <dbl>
 1 setosa   4.3 
 2 setosa   4.32
 3 setosa   4.33
 4 setosa   4.35
 5 setosa   4.36
 6 setosa   4.38
 7 setosa   4.39
 8 setosa   4.41
 9 setosa   4.42
10 setosa   4.44
# … with 290 more rows

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章