繁体   English   中英

R - 划分 data.frame 的一些列并保留其他列

[英]R - divide some columns of a data.frame and keeping the others

我有一个由字符和数字变量组成的大型 data.frame。 我需要将第 6 列划分为第 4 5 6 列,并保持 1 2 3 不变。

你的问题有点含糊(例如,你想要元素明智的划分吗?),但这就是你要找的吗?

 ## set up some test data
 data.org=data.frame(matrix(1:100,ncol=10))

 ## make a copy of the org. data
 data=data.org

 ## perform your element by element division
 data[,4] = data.org[,4]/data.org[,6]
 data[,5] = data.org[,5]/data.org[,6]
 data[,6] = data.org[,6]/data.org[,6]

 ## Or the entire operation can be done with one line by
 data[,4:6] = data.org[,4:6] / data.org[,6]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM