I have such data:
dput(tbl_data[1:5])
structure(list(Name = c("Mark", "Anders", "Tom", "Vin", "Marcel",
"Tyta", "Gerta", "Moses", "Hank", "Rita", "Margary"), Col = c(1769380097.5,
1444462500, 1499146687.5, 1276309375, 22279500, 3114023471, 2961012500,
3978937423.5, 1703925000, 1838885550, 1483386250), dKO1 = c(1534931323.07692,
1794881375, 2292661687.5, 855786250, 21915500, 3056061512.25,
3581940000, 3766909703.25, 2043300000, 2135859875, 1482031250
), dKO2 = c(1628137500, 1781982737.5, 1659391250, 741220687.5,
41242000, 2833327766.38514, 3675450000, 3592650662.5, 1586512500,
1934575000, 1467271250), sdi1 = c(1545572702.88461, 1748600000,
1745026687.5, 1556481250, NaN, 3551716021.25, 3108137500, 3718036445,
1380278750, 2217526000, 1026813750)), .Names = c("Name", "Col",
"dKO1", "dKO2", "sdi1"), row.names = c(29L, 30L, 1278L, 1295L,
1296L, 1297L, 1298L, 1307L, 1642L, 1674L, 1754L), class = "data.frame")
As the title states I would like to divide all the columns in a row by the value from 2nd column. First column should be ignored because it's a name. Of course first column can be deleted and put as a row names if it makes life easier.
I would like to apply it for all the rows in this data frame.
I quite simple but my mind is not working properly today.
Data Visualization using R Programming To divide each column by a particular column, we can use division sign (/). For example, if we have a data frame called df that contains three columns say x, y, and z then we can divide all the columns by column z using the command df/df[,3].
You can use function sweep
:
tbl_data[, -(1:2)] <- sweep(tbl_data[, -(1:2)], 1, tbl_data[, 2], "/")
tbl_data
Name Col dKO1 dKO2 sdi1
29 Mark 1769380098 0.8674967 0.9201740 0.8735108
30 Anders 1444462500 1.2425947 1.2336649 1.2105541
1278 Tom 1499146688 1.5293111 1.1068905 1.1640133
1295 Vin 1276309375 0.6705163 0.5807531 1.2195172
1296 Marcel 22279500 0.9836621 1.8511187 NaN
1297 Tyta 3114023471 0.9813868 0.9098608 1.1405553
1298 Gerta 2961012500 1.2097011 1.2412815 1.0496874
1307 Moses 3978937424 0.9467125 0.9029171 0.9344295
1642 Hank 1703925000 1.1991725 0.9310929 0.8100584
1674 Rita 1838885550 1.1614969 1.0520367 1.2059076
1754 Margary 1483386250 0.9990865 0.9891363 0.6922093
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With