與plyr我一起工作時,我經常發現將其用于adply必須應用于每一行的標量函數很有用。例如data(iris)library(plyr)head( adply(iris, 1, transform , Max.Len= max(Sepal.Length,Petal.Length)) ) Sepal.Length Sepal.Width Petal.Length Petal.Width Species Max.Len1 5.1 3.5 1.4 0.2 setosa 5.12 4.9 3.0 1.4 0.2 setosa 4.93 4.7 3.2 1.3 0.2 setosa 4.74 4.6 3.1 1.5 0.2 setosa 4.65 5.0 3.6 1.4 0.2 setosa 5.06 5.4 3.9 1.7 0.4 setosa 5.4現在,我使用的dplyr更多,我想知道是否有一種整潔/自然的方式來做到這一點?因為這不是我想要的:library(dplyr)head( mutate(iris, Max.Len= max(Sepal.Length,Petal.Length)) ) Sepal.Length Sepal.Width Petal.Length Petal.Width Species Max.Len1 5.1 3.5 1.4 0.2 setosa 7.92 4.9 3.0 1.4 0.2 setosa 7.93 4.7 3.2 1.3 0.2 setosa 7.94 4.6 3.1 1.5 0.2 setosa 7.95 5.0 3.6 1.4 0.2 setosa 7.96 5.4 3.9 1.7 0.4 setosa 7.9
3 回答

白衣非少年
TA貢獻1155條經驗 獲得超0個贊
您需要按行分組:
iris %>% group_by(1:n()) %>% mutate(Max.Len= max(Sepal.Length,Petal.Length))
這就是在中1所做的adply。
- 3 回答
- 0 關注
- 725 瀏覽
添加回答
舉報
0/150
提交
取消