Reputation: 57
How to multiply some specific columns by an expression?
For example I have 9 columns, but I would like 2 to 9 to be multiplied by the expression 28.8372*28.8372*0.0001
I wrote this but it doesn't make sense:
df<-read.table(text="year veg wetland crop grass Urban Soiol water Total
2005 21430607 1367412 1190076 26545075 46618 39273 85208 709857278
2006 20679662 2019488 1167270 26673076 47338 35501 81914 709857278
2007 21111969 1476695 1133977 26811365 47839 38140 82477 709857278
2008 21393529 1171087 1223445 26757701 48063 27112 83323 709857278
2009 21707049 890611 1312864 26645771 49623 16910 81429 709857278
2010 20646100 2020408 1298895 26583630 50637 18467 86133 709857278
2011 20886556 1827600 1370779 26457435 51225 17341 93306 709857278
2012 21132326 1602643 1513077 26281770 53145 23551 97760 709857278
2013 19720346 2989292 1685743 26116417 54487 33398 104469 709857278
2014 19926085 2734241 2164979 25690817 55699 27513 104913 709857278
2015 20691165 1816887 2291247 25713225 56924 27638 107166 709857278
2016 19989336 2440118 2534635 25553211 57758 26041 103128 709857278
2017 20640458 1637314 2772866 25468701 57838 26098 100855 709857278
2018 19705954 2458960 3074894 25276010 59308 28252 100611 709857278", sep="", header=TRUE)
df%>%
summarise(across(['Veg':'water']*(28.8372*28.8372*0.0001))
Upvotes: 1
Views: 565
Reputation: 887118
We can also use summarise
as summarise
can return more than one row. Note that summarise
returns only the columns passed and normally it is used to return a single value per column (or if it is grouped, one value per group per column). Most probably, OP would require mutate
as @Anoushiravan R showed in the post
library(dplyr)
df %>%
summarise(across(veg:water, ~ . * 28.8372*28.8372*0.0001))
The issue in OP's code is trying to subset the range of columns with []
(not needed) and not using a lambda expression
Upvotes: 2
Reputation: 101343
You can use the following base R code
df[-1] <- df[-1]* 28.8372*28.8372*0.0001
Upvotes: 2
Reputation: 21908
I hope this is what you are looking for:
library(dplyr)
df %>%
mutate(across(2:9, ~ .x * (28.8372*28.8372*0.0001)))
year veg wetland crop grass Urban Soiol water Total
1 2005 1782135 113711.81 98964.83 2207446 3876.679 3265.880 7085.762 59030603
2 2006 1719688 167937.41 97068.32 2218091 3936.553 2952.207 6811.838 59030603
3 2007 1755638 122799.61 94299.72 2229590 3978.215 3171.662 6858.656 59030603
4 2008 1779052 97385.73 101739.74 2225128 3996.843 2254.591 6929.008 59030603
5 2009 1805124 74061.80 109175.68 2215820 4126.570 1406.209 6771.506 59030603
6 2010 1716897 168013.92 108014.04 2210652 4210.892 1535.686 7162.683 59030603
7 2011 1736893 151980.31 113991.80 2200158 4259.790 1442.050 7759.179 59030603
8 2012 1757331 133273.24 125825.08 2185550 4419.454 1958.464 8129.566 59030603
9 2013 1639913 248584.77 140183.71 2171800 4531.052 2777.325 8687.476 59030603
10 2014 1657022 227375.14 180036.21 2136408 4631.840 2287.937 8724.398 59030603
11 2015 1720644 151089.43 190536.46 2138271 4733.709 2298.332 8911.754 59030603
12 2016 1662281 202916.33 210776.22 2124964 4803.063 2165.528 8575.961 59030603
13 2017 1716428 136156.43 230587.13 2117937 4809.716 2170.268 8386.941 59030603
14 2018 1638716 204483.20 255703.30 2101913 4931.959 2349.391 8366.651 59030603
Upvotes: 3