我有这样的数据:
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")
正如标题所述,我想将第二列中的值除以一行中的所有列。应忽略第一列,因为它是一个名称。当然,第一列可以删除并作为行名称放置,如果它使生活更轻松。
我想将它应用于此数据框中的所有行。
我很简单,但我的思绪今天不能正常运作。
答案 0 :(得分:6)
您可以使用函数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
答案 1 :(得分:5)
我们可以通过复制第二列然后除以没有第一列或第二列的数据集子集来使长度相等
df1[-(1:2)] <- df1[-(1:2)]/df1[,2][row(df1[-(1:2)])]
df1
# 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
答案 2 :(得分:2)
这是我的解决方案,我猜想可以写成一行:
df3 <- df1 %>% select(-Col,everything()) %>% mutate_if(is.numeric, funs(./Col))
df3[["Col"]]<-df1[["Col"]]
df3 <- df3 %>% select(Col, dKO1, dKO2, sdi1)
答案 3 :(得分:1)
一个解决方案:
for (column.name in names(tbl_data) [3:ncol(tbl_data)]) {
tbl_data[column.name] = tbl_data[column.name] / tbl_data$Col
}
答案 4 :(得分:1)
require(magrittr)
df[,-(1:2)] %<>% sapply(`/`, df[,2])