【问题标题】:How can I get the average of lists in many columns of data.frame如何获得 data.frame 多列中列表的平均值
【发布时间】:2014-11-03 18:17:54
【问题描述】:
dput(head(P[,1:2],12))

structure(list(valoracion.c1 = list(c(0.75, 1, 1), c(0.75, 1, 
1), c(0.75, 1, 1), c(0.75, 1, 1), c(0.75, 1, 1), c(0.5, 0.75, 
1), c(0.75, 1, 1), c(0.75, 1, 1), c(0.5, 0.75, 1), c(0.75, 1, 
1), c(0.5, 0.75, 1), c(0.75, 1, 1)), valoracion.c2 = list(c(0.75, 
1, 1), c(0.75, 1, 1), c(0.5, 0.75, 1), c(0.75, 1, 1), c(0.75, 
1, 1), c(0.75, 1, 1), c(0.5, 0.75, 1), c(0.75, 1, 1), c(0.25, 
0.5, 0.75), c(0.25, 0.5, 0.75), c(0.75, 1, 1), c(0.5, 0.75, 1
))), .Names = c("valoracion.c1", "valoracion.c2"), row.names = c(NA, 
12L), class = "data.frame")

我想获取每列的平均值并保留数据结构。我试过了

类似的东西

dat2

使用调试重新运行 mean.default(init, x[[i]]) 中的错误: 'trim' 必须是长度为 1 的数字

有人可以帮我吗?

【问题讨论】:

  • 你能显示 dput 输出吗? dput(head(data,12))
  • 这不是data.frame。在 R 中,每一列都应该有一个(唯一的)名称。
  • 从预期输出中也不清楚哪些值用于求和。
  • 为什么需要在 data.frame 中列出列?这似乎不是最理想的。

标签: r


【解决方案1】:

假设您显示的数据与以下数据类似:

dat2 <- dat1[1,]
dat2[] <- lapply(dat1, function(x) list(Reduce(`+`, x)))
dat2
#       Col1       Col2       Col3
#1 18, 19, 23 20, 15, 25 22, 21, 17


head(dat1,3)
#    Col1    Col2    Col3
#1 2, 1, 3 2, 1, 3 2, 3, 1
#2 2, 1, 3 3, 1, 2 1, 3, 2
#3 1, 2, 3 3, 1, 2 2, 3, 1

数据

set.seed(45)
dat1 <- data.frame(Col1=I(lapply(1:10, function(i) sample(3))),
    Col2=I(lapply(1:10, function(i) sample(3))),
    Col3= I(lapply(1:10, function(i) sample(3))))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-05
    • 2012-03-18
    • 2022-08-14
    • 2015-09-11
    • 2018-11-27
    • 2021-03-04
    • 1970-01-01
    相关资源
    最近更新 更多