r - 使用 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
解决方案
@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
推荐阅读
- css - CSS中文本的背景颜色?
- ios - 收到此错误:条件绑定的初始化程序必须具有可选类型,而不是“布尔”
- docker - Codeship/Docker 上的 Terraform S3 后端身份验证
- python - 禁用 Pycharm 打印自动完成
- syntax - VHDL:如何在 VHDL 中使用 rem 和 mod 命令?(语法问题)
- python - Jupyter Notebook Pandas 输出中的 iframe
- git - 回滚以开发没有最后合并功能的分支
- python - 如何使用 Python、PyQt5 和 Pyinstaller 修复未正确显示的按钮
- split - 带有多个分隔符的 ansible string.split()
- python - 在 Google Cloud 服务的输出张量中累积预测