7

我正在尝试使用包daply中的功能,plyr但无法正确输出。尽管构成矩阵的变量是数字,但矩阵的元素是列表,而不是变量本身。例如,这里是数据的一小部分:

   Month Vehicle Samples
1 Oct-10   31057     256
2 Oct-10   31059     316
3 Oct-10   31060     348
4 Nov-10   31057     267
5 Nov-10   31059     293
6 Nov-10   31060     250
7 Dec-10   31057     159
8 Dec-10   31059     268
9 Dec-10   31060     206

我希望能够以矩阵格式可视化数据,看起来像这样:

  Month
Vehicle Oct-10 Nov-10 Dec-10
  31057    256    267    159
  31059    316    293    268
  31060    348    250    206

这是我使用的几种替代语法(后者是因为我的原始数据框的列比我在此处显示的要多):

daply(DF, .(Vehicle, Month), identity)
daply(DF,.(Vehicle,Month), colwise(identity,.(Samples)))

然而,我得到的却是相当深奥:

       Month
Vehicle Oct-10 Nov-10 Dec-10
  31057 List,3 List,3 List,3
  31059 List,3 List,3 List,3
  31060 List,3 List,3 List,3

正如一些评论者所建议的那样,我在输出中使用了该str函数,这是一个摘录:

List of 9
 $ :'data.frame':       1 obs. of  3 variables:
  ..$ Month  : Ord.factor w/ 3 levels "Oct-10"<"Nov-10"<..: 1
  ..$ Vehicle: Factor w/ 3 levels "31057","31059",..: 1
  ..$ Samples: int 256
 $ :'data.frame':       1 obs. of  3 variables:
  ..$ Month  : Ord.factor w/ 3 levels "Oct-10"<"Nov-10"<..: 1
  ..$ Vehicle: Factor w/ 3 levels "31057","31059",..: 2
  ..$ Samples: int 316

我错过了什么?另外,有没有办法简单地使用基本包来做到这一点?谢谢!

Dput如果您想重现它,下面是数据框的:

structure(list(Month = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 
3L, 3L), .Label = c("Oct-10", "Nov-10", "Dec-10"), class = c("ordered", 
"factor")), Vehicle = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 
2L, 3L), .Label = c("31057", "31059", "31060"), class = "factor"), 
    Samples = c(256L, 316L, 348L, 267L, 293L, 250L, 159L, 268L, 
    206L)), .Names = c("Month", "Vehicle", "Samples"), class = "data.frame", row.names = c(NA, 
9L))
4

2 回答 2

8

identity功能不是您想要的。来自帮助页面,“所有 plyr 函数都使用相同的拆分-应用-组合策略:它们将输入拆分为更简单的部分,将 .fun 应用于每个部分,然后将这些部分组合成一个数据结构。” 在这种情况下,较简单的部分是具有唯一车辆/月份组合的原始数据帧的子集;恒等函数只返回该子集,然后使用这些子集填充生成的矩阵。

也就是说,您获得的矩阵的每个元素都是一个数据框(它是一种列表),其中的行具有该月/车辆组合。

> try1 <- daply(DF, .(Vehicle, Month), identity)
> try1[1,1]
[[1]]
   Month Vehicle Samples
1 Oct-10   31057     256

相反,您想使用一个仅获取该Samples数据框部分的函数,如下所示:

daply(DF, .(Vehicle, Month), function(x) x$Samples)

这导致

       Month
Vehicle Oct-10 Nov-10 Dec-10
  31057    256    267    159
  31059    316    293    268
  31060    348    250    206

执行此操作的一些替代方法是使用castreshape(返回数据框)

cast(DF, Vehicle~Month, value="Samples")

中的修订版reshape2;第一个返回一个数据框,第二个返回一个矩阵

dcast(DF, Vehicle~Month, value_var="Samples")
acast(DF, Vehicle~Month, value_var="Samples")

从包装xtabsstats

xtabs(Samples ~ Vehicle + Month, DF)

或者手动,使用矩阵索引一点也不难;几乎所有的代码都只是设置矩阵。

with(DF, {
  out <- matrix(nrow=nlevels(Vehicle), ncol=nlevels(Month),
                dimnames=list(Vehicle=levels(Vehicle), Month=levels(Month)))
  out[cbind(Vehicle, Month)] <- Samples
  out
})

stats包中的reshape函数也可以用来做这个,但是语法比较难,自从学了包之后就没cast用过melt一次reshape

于 2011-08-11T02:14:42.333 回答
2

如果我们在标题中使用 OP,那么他们可能正在寻找在 R 中始终可用的基本data.matrix()包中的标准函数。

data.matrix()通过在将数据帧转换为矩阵之前将任何因子转换为它们的数字编码来工作。考虑以下数据框:

dat <- data.frame(A = 1:10, B = factor(sample(c("X","Y"), 10, replace = TRUE)))

如果我们通过转换as.matrix()得到一个字符矩阵:

> head(as.matrix(dat))
     A    B  
[1,] " 1" "X"
[2,] " 2" "X"
[3,] " 3" "Y"
[4,] " 4" "Y"
[5,] " 5" "Y"
[6,] " 6" "Y"

或者,如果通过matrix()一个得到一个带有维度的列表(一个列表数组 -顺便提一下部分?daply

> head(matrix(dat))
     [,1]      
[1,] Integer,10
[2,] factor,10 
> str(matrix(dat))
List of 2
 $ : int [1:10] 1 2 3 4 5 6 7 8 9 10
 $ : Factor w/ 2 levels "X","Y": 1 1 2 2 2 2 1 2 2 1
 - attr(*, "dim")= int [1:2] 2 1

data.matrix(),但是,做了预期的事情:

> mat <- data.matrix(dat)
> head(mat)
     A B
[1,] 1 1
[2,] 2 1
[3,] 3 2
[4,] 4 2
[5,] 5 2
[6,] 6 2
> str(mat)
 int [1:10, 1:2] 1 2 3 4 5 6 7 8 9 10 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:2] "A" "B"
于 2011-08-10T08:21:04.640 回答