简体   繁体   English

使用数据框R中行的最后一列的最后一个值添加每列

[英]Add each column with last value of last column of the row in dataframe R

I want to add each column of dataframe with the last column of the row in R. My dataframe- 我想在R的行的最后一列添加每一列数据框。我的数据框 -

L   E   B1  P   B2  M   Value
5   5   0   20  2   5   100
10  6   0   40  15  2   150
6   15  0   50  6   10  160
1   10  0   55  5   20  160
0   20  0   80  0   20  200
10  1   20  80  10  10  250
8   2   40  30  5   10  300
5   3   60  30  5   20  350
5   4   30  75  5   20  400
1   0   50  80  0   10  400
2   0   40  60  5   20  500
0   0   60  50  0   30  500

So 1st row will be like- 所以第一排就像 -

L   E   B1  P   B2  M   Value
5*100   5*100   0*100   20*100  2*100   5*100   100
10  6   0   40  15  2   150
6   15  0   50  6   10  160
1   10  0   55  5   20  160
0   20  0   80  0   20  200
10  1   20  80  10  10  250
8   2   40  30  5   10  300
5   3   60  30  5   20  350
5   4   30  75  5   20  400
1   0   50  80  0   10  400
2   0   40  60  5   20  500
0   0   60  50  0   30  500

I tried to use lapply 我试着用lapply

lapply(df1, function(x) x * tail(x,1) )

But it takes the row value , so how to get each row last column value or any specific column to add with all other column values in R 但它需要行值,因此如何获取每一行的最后一列值或任何特定列以与R中的所有其他列值相加

Using dplyr and assuming your dataframe is df: 使用dplyr并假设您的数据帧是df:

library(dplyr)

df %>% mutate_each(funs(. * Value), -Value)

Not clear if you want to multiply or divide (there seems to be a contradiction between what you ask for and your own attempt) but here's an approach for multiplication: 不清楚你是否想要乘法或除法(你要求的和你自己的尝试之间似乎存在矛盾),但这是一种乘法的方法:

cbind(mydf[-length(mydf)] * mydf[[length(mydf)]], mydf[length(mydf)])
#       L    E    B1     P   B2     M Value
# 1   500  500     0  2000  200   500   100
# 2  1500  900     0  6000 2250   300   150
# 3   960 2400     0  8000  960  1600   160
# 4   160 1600     0  8800  800  3200   160
# 5     0 4000     0 16000    0  4000   200
# 6  2500  250  5000 20000 2500  2500   250
# 7  2400  600 12000  9000 1500  3000   300
# 8  1750 1050 21000 10500 1750  7000   350
# 9  2000 1600 12000 30000 2000  8000   400
# 10  400    0 20000 32000    0  4000   400
# 11 1000    0 20000 30000 2500 10000   500
# 12    0    0 30000 25000    0 15000   500

The basic idea is to just multiply all the columns except for the last one by the values in the last column. 基本思想是将除最后一列之外的所有列乘以最后一列中的值。 Since that column has been dropped, you add it back in with cbind . 由于该列已被删除,因此请使用cbind将其重新添加。

Here's another base R option: 这是另一个基本R选项:

n <- ncol(df)
df[-n] <- df[-n] * df[[n]]

Note: running this code will modify your existing data.frame. 注意:运行此代码将修改现有的data.frame。 If you want to create a new data.frame and keep the old one as is, you'd better use the answer by Ananda Mahto or one of the others. 如果你想创建一个新的data.frame并保持旧的data.frame,你最好使用Ananda Mahto或其他人的答案。

Just to complete the picture, you could also update the data by reference using data.table package 只是为了完成图片,您还可以使用data.table包通过引用更新数据

library(data.table)
setDT(df)[, names(df)[-length(df)] := 
            lapply(.SD, "*", df$Value), 
            .SDcols = -"Value"]
df
#        L    E    B1     P   B2     M Value
#  1:  500  500     0  2000  200   500   100
#  2: 1500  900     0  6000 2250   300   150
#  3:  960 2400     0  8000  960  1600   160
#  4:  160 1600     0  8800  800  3200   160
#  5:    0 4000     0 16000    0  4000   200
#  6: 2500  250  5000 20000 2500  2500   250
#  7: 2400  600 12000  9000 1500  3000   300
#  8: 1750 1050 21000 10500 1750  7000   350
#  9: 2000 1600 12000 30000 2000  8000   400
# 10:  400    0 20000 32000    0  4000   400
# 11: 1000    0 20000 30000 2500 10000   500
# 12:    0    0 30000 25000    0 15000   500

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM