如何在R中对单列进行乘法(平方,除法等)运算

4
print(elasticband)
strech distance tension
1     67      148       5
2     98      120      10
3     34      173      15
4     50       60      20
5     45      263      25
6     42      141      30
7     89      166      35

我有一个数据框,想要修改单个列的值(例如,把张力列的每个值平方),而不影响其他列像elasticband**2。

有什么建议吗?

附注:我不是很擅长这个,所以修复得越简单越好。

1个回答

10
> transform(elasticband, tension2=tension^2)
  strech distance tension tension2
1     67      148       5       25
2     98      120      10      100
3     34      173      15      225
4     50       60      20      400
5     45      263      25      625
6     42      141      30      900
7     89      166      35     1225

其他替代方案包括:

elasticband$tension2 <- elasticband[, "tension"]^2

或者
elasticband$tension2 <-  elasticband$tension^2

如果您只需要向量作为输出

elasticband[, "tension"]^2

或者

elasticband$tension^2

2
再来一条:within(elasticband,tension <- tension^2) - thelatemail

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接