I have a dataframe and am trying to divide each column in dataframe by last row value:
我有一个数据帧,我试图将数据帧中的每一列除以最后一行值:
A <- c(1:10)
B <- c(2:11)
C <- c(3:12)
df1 <- data.frame(A,B,C)
df2 <- df1/df1[10,]
However I get an error. I would be grateful to know what I am doing wrong.
但是我收到了一个错误。我很想知道我做错了什么。
4 个解决方案
#1
10
data.frames
aren't made for those kinds of operations.
data.frames不适用于那些类型的操作。
data.frame(lapply(df1, function(X) X/X[10]))
Should do the trick. Or use a matrix instead.
应该做的伎俩。或者使用矩阵代替。
df1 = as.matrix(df1)
> t(t(df1)/df1[10,])
A B C
[1,] 0.1 0.1818182 0.2500000
[2,] 0.2 0.2727273 0.3333333
[3,] 0.3 0.3636364 0.4166667
[4,] 0.4 0.4545455 0.5000000
[5,] 0.5 0.5454545 0.5833333
[6,] 0.6 0.6363636 0.6666667
[7,] 0.7 0.7272727 0.7500000
[8,] 0.8 0.8181818 0.8333333
[9,] 0.9 0.9090909 0.9166667
[10,] 1.0 1.0000000 1.0000000
#2
6
Dividing by c(df[10,])
works, as well, such as:
除以c(df [10,])也可以,例如:
df1/c(df1[10,])
#3
3
> df1[] <- lapply(df1, function(x) x/tail(x,1) )
> df1
A B C
1 0.1 0.1818182 0.2500000
2 0.2 0.2727273 0.3333333
3 0.3 0.3636364 0.4166667
4 0.4 0.4545455 0.5000000
5 0.5 0.5454545 0.5833333
6 0.6 0.6363636 0.6666667
7 0.7 0.7272727 0.7500000
8 0.8 0.8181818 0.8333333
9 0.9 0.9090909 0.9166667
10 1.0 1.0000000 1.0000000
This is somewhat more painful but might be faster in bigger data situations:
这有点痛苦,但在更大的数据情况下可能会更快:
data.matrix(df1) / rep( unlist(df1[10,]), each=nrow(df1) )
Frank is partly correct (and I was close with one of my earlier failed attempts to use sweep:
弗兰克在某种程度上是正确的(我之前尝试过使用扫描失败的一次尝试很接近:
sweep(df1, 2, unlist(df1[10, ]), "/")
#4
0
sapply(names(df1),function(x)df1[,x]/df1[nrow(df1),x])
A B C
[1,] 0.1 0.1818182 0.2500000
[2,] 0.2 0.2727273 0.3333333
[3,] 0.3 0.3636364 0.4166667
[4,] 0.4 0.4545455 0.5000000
[5,] 0.5 0.5454545 0.5833333
[6,] 0.6 0.6363636 0.6666667
[7,] 0.7 0.7272727 0.7500000
[8,] 0.8 0.8181818 0.8333333
[9,] 0.9 0.9090909 0.9166667
[10,] 1.0 1.0000000 1.0000000
#1
10
data.frames
aren't made for those kinds of operations.
data.frames不适用于那些类型的操作。
data.frame(lapply(df1, function(X) X/X[10]))
Should do the trick. Or use a matrix instead.
应该做的伎俩。或者使用矩阵代替。
df1 = as.matrix(df1)
> t(t(df1)/df1[10,])
A B C
[1,] 0.1 0.1818182 0.2500000
[2,] 0.2 0.2727273 0.3333333
[3,] 0.3 0.3636364 0.4166667
[4,] 0.4 0.4545455 0.5000000
[5,] 0.5 0.5454545 0.5833333
[6,] 0.6 0.6363636 0.6666667
[7,] 0.7 0.7272727 0.7500000
[8,] 0.8 0.8181818 0.8333333
[9,] 0.9 0.9090909 0.9166667
[10,] 1.0 1.0000000 1.0000000
#2
6
Dividing by c(df[10,])
works, as well, such as:
除以c(df [10,])也可以,例如:
df1/c(df1[10,])
#3
3
> df1[] <- lapply(df1, function(x) x/tail(x,1) )
> df1
A B C
1 0.1 0.1818182 0.2500000
2 0.2 0.2727273 0.3333333
3 0.3 0.3636364 0.4166667
4 0.4 0.4545455 0.5000000
5 0.5 0.5454545 0.5833333
6 0.6 0.6363636 0.6666667
7 0.7 0.7272727 0.7500000
8 0.8 0.8181818 0.8333333
9 0.9 0.9090909 0.9166667
10 1.0 1.0000000 1.0000000
This is somewhat more painful but might be faster in bigger data situations:
这有点痛苦,但在更大的数据情况下可能会更快:
data.matrix(df1) / rep( unlist(df1[10,]), each=nrow(df1) )
Frank is partly correct (and I was close with one of my earlier failed attempts to use sweep:
弗兰克在某种程度上是正确的(我之前尝试过使用扫描失败的一次尝试很接近:
sweep(df1, 2, unlist(df1[10, ]), "/")
#4
0
sapply(names(df1),function(x)df1[,x]/df1[nrow(df1),x])
A B C
[1,] 0.1 0.1818182 0.2500000
[2,] 0.2 0.2727273 0.3333333
[3,] 0.3 0.3636364 0.4166667
[4,] 0.4 0.4545455 0.5000000
[5,] 0.5 0.5454545 0.5833333
[6,] 0.6 0.6363636 0.6666667
[7,] 0.7 0.7272727 0.7500000
[8,] 0.8 0.8181818 0.8333333
[9,] 0.9 0.9090909 0.9166667
[10,] 1.0 1.0000000 1.0000000