首页 > 解决方案 > 使用 R 中的行组为简单方程的结果添加新行

问题描述

我在完成这个简单的任务时遇到了麻烦。

在下文data.frame中,如何添加包含Sepal.Length*0.75 + Sepal.Width*0.25每列结果的额外行?谢谢

as.data.frame(t(iris[1:5,1:4]))
               1   2   3   4   5
Sepal.Length 5.1 4.9 4.7 4.6 5.0
Sepal.Width  3.5 3.0 3.2 3.1 3.6
Petal.Length 1.4 1.4 1.3 1.5 1.4
Petal.Width  0.2 0.2 0.2 0.2 0.2

标签: rdataframe

解决方案


@akrun 的回答似乎是最好的,但如果您只想使用最终数据集,您可以使用行引用 -

x <- as.data.frame(t(iris[1:5,1:4]))
x[c('new'),] <- x[c('Sepal.Length'), ]*0.75 + x[c('Sepal.Width'), ]*0.25

推荐阅读