[英]Product multiple columns by a single column in a dataframe
我的數據如下:
datas6=structure(list(Exp = c("BA", "CI", "CE", "DE"), Var1 = c(0L,
0L, 0L, 0L), Var2 = c(0L, 0L, 1L, 0L), Var3 = c(0L, 1L, 1L, 0L
), Var4 = c(1L, 1L, 1L, 1L), Freq = c("6", "2", "7", "5")), .Names = c("Exp",
"Var1", "Var2", "Var3", "Var4", "Freq"), row.names = c(NA, 4L
), class = "data.frame")
這是一個基本問題,我很抱歉,找不到一種方法將 datas6[2] 乘以 datas6[5] 乘以 datas6[6] 這樣的偽代碼:
datas6[2:5]=as.numeric(datas6[2:5])*as.numeric(datas6[6])
返回錯誤:
Error: object (list) can not be converted automatically into a type 'double'
也試過這個:
new_df <- ddply(datas6, (datas6[2:5]), transform, new_column = as.numeric(datas6[2:5])*as.numeric(datas6[6]))
不起作用: Error in UseMethod("as.quoted"
)
非常感謝。
我們可以通過
datas6[2:5] <- datas6[2:5] * as.numeric(datas6[,6])
datas6[2:5]
# Var1 Var2 Var3 Var4
#1 0 0 0 6
#2 0 0 2 2
#3 0 7 7 7
#4 0 0 0 5
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.