Khu*_*eed 11 r apply dataframe
我想在R中的一个包含行总和和产品的data.frame中添加一列.请考虑以下数据框
x y z
1 2 3
2 3 4
5 1 2
Run Code Online (Sandbox Code Playgroud)
我想得到以下内容
x y z sum prod
1 2 3 6 6
2 3 4 9 24
5 1 2 8 10
Run Code Online (Sandbox Code Playgroud)
我试过了
sum = apply(ages,1,add)
Run Code Online (Sandbox Code Playgroud)
但它给了我一个行向量.有人可以给我一个有效的命令来总结和产品,并将它们附加到原始数据框,如上所示?
akr*_*run 20
尝试
transform(df, sum=rowSums(df), prod=x*y*z)
# x y z sum prod
#1 1 2 3 6 6
#2 2 3 4 9 24
#3 5 1 2 8 10
Run Code Online (Sandbox Code Playgroud)
要么
transform(df, sum=rowSums(df), prod=Reduce(`*`, df))
# x y z sum prod
#1 1 2 3 6 6
#2 2 3 4 9 24
#3 5 1 2 8 10
Run Code Online (Sandbox Code Playgroud)
另一种选择是使用rowProds由matrixStats
library(matrixStats)
transform(df, sum=rowSums(df), prod=rowProds(as.matrix(df)))
Run Code Online (Sandbox Code Playgroud)
如果你正在使用 apply
df[,c('sum', 'prod')] <- t(apply(df, 1, FUN=function(x) c(sum(x), prod(x))))
df
# x y z sum prod
#1 1 2 3 6 6
#2 2 3 4 9 24
#3 5 1 2 8 10
Run Code Online (Sandbox Code Playgroud)
另一种方法。
require(data.table)
# Create data
dt <- data.table(x = c(1,2,5), y = c(2,3,1), z = c(3,4,2))
# Create index
dt[, i := .I]
# Compute sum and prod
dt[, sum := sum(x, y, z), by = i]
dt[, prod := prod(x, y, z), by = i]
dt
# Compute sum and prod using .SD
dt[, c("sum", "prod") := NULL]
dt
dt[, sum := sum(.SD), by = i, .SDcols = c("x", "y", "z")]
dt[, prod := prod(.SD), by = i, .SDcols = c("x", "y", "z")]
dt
# Compute sum and prod using .SD and list
dt[, c("sum", "prod") := NULL]
dt
dt[, c("sum", "prod") := list(sum(.SD), prod(.SD)), by = i,
.SDcols = c("x", "y", "z")]
dt
# Compute sum and prod using .SD and lapply
dt[, c("sum", "prod") := NULL]
dt
dt[, c("sum", "prod") := lapply(list(sum, prod), do.call, .SD), by = i,
.SDcols = c("x", "y", "z")]
dt
Run Code Online (Sandbox Code Playgroud)