Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I multiply (square, divide, etc.) a single column in R

Tags:

r

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

So I have this data frame, and I want to be able to alter a single column (For example, square everything in the tension column) without affecting the others like elasticband**2

Any tips?

P.S. I'm not too good at this, so the simpler the fix the better

like image 946
user3059126 Avatar asked Dec 26 '22 16:12

user3059126


1 Answers

> 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

other alternatives are:

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

Or

elasticband$tension2 <-  elasticband$tension^2

If you only want a vector as output

elasticband[, "tension"]^2

Or

elasticband$tension^2
like image 51
Jilber Urbina Avatar answered Dec 28 '22 07:12

Jilber Urbina