data.table中的rowwise制表

Ren*_*rop 2 r data.table

拥有data.table如下:

   station       w_1       w_2
1:    1757     ar_2d lm_h_step
2:    2171 lm_h_step lm_h_step
3:    2812 lm_h_step lm_h_step
4:    4501 lm_h_step lm_h_step
5:    4642     ar_2d lm_h_step
6:    5029     ar_2d lm_h_step
7:    5480 lm_h_step lm_h_step
8:    5779     ar_2d     ar_2d
9:    5792     ar_1d     ar_2d
Run Code Online (Sandbox Code Playgroud)

我想列出每站方法的频率.
所以预期的结果是

          1757  2171  2812 ...
lm_h_step    1     2     2
ar_2d        1     0     0 
ar_1d        0     0     0 ...
Run Code Online (Sandbox Code Playgroud)

到目前为止我尝试了什么:

apply(dat,1,table)
Run Code Online (Sandbox Code Playgroud)

正在产生正确的结果,但它没有正确地形成.

有任何想法吗?

数据输入:

structure(list(station = c(1757L, 2171L, 2812L, 4501L, 4642L, 
                           5029L, 5480L, 5779L, 5792L), w_1 = c("ar_2d", "lm_h_step", "lm_h_step", 
                                                                "lm_h_step", "ar_2d", "ar_2d", "lm_h_step", "ar_2d", "ar_2d"), 
               w_2 = c("lm_h_step", "lm_h_step", "lm_h_step", "lm_h_step", 
                       "lm_h_step", "lm_h_step", "lm_h_step", "ar_2d", "ar_2d")), .Names = c("station", 
                                                                                             "w_1", "w_2"), class = c("data.table", "data.frame"), row.names = c(NA, 
                                                                                                                                                                 -9L))
Run Code Online (Sandbox Code Playgroud)

Dav*_*urg 5

尝试dcast/melt组合

对于data.table v> = 1.9.5,请使用此选项

dcast(melt(dat, "station"), value ~ station, length)
#        value 1757 2171 2812 4501 4642 5029 5480 5779 5792
# 1:     ar_1d    0    0    0    0    0    0    0    0    1
# 2:     ar_2d    1    0    0    0    1    1    0    2    1
# 3: lm_h_step    1    2    2    2    1    1    2    0    0
Run Code Online (Sandbox Code Playgroud)

对于data.table v <1.9.5,您还需要加载reshape2并显式使用dcast.data.table(因为reshape2::dcast它不是通用的,没有dcast.data.table方法).

reshape2::melt另一方面,它是通用的(参见methods(melt))并且有一个melt.data.table方法,所以你不需要告诉它任何东西.它会知道你要取决于使用哪种方法classdat

require(reshape2)
dcast.data.table(melt(dat, "station"), value ~ station, length)
#        value 1757 2171 2812 4501 4642 5029 5480 5779 5792
# 1:     ar_1d    0    0    0    0    0    0    0    0    1
# 2:     ar_2d    1    0    0    0    1    1    0    2    1
# 3: lm_h_step    1    2    2    2    1    1    2    0    0
Run Code Online (Sandbox Code Playgroud)

如果你没有严格使用data.table方法挑剔,你也可以使用reshape2::recast(参见@shadows注释)这是上面的解决方案的包装,但使用reshape2::dcast而不是,dcast.data.table因此将返回一个data.frame对象,而不是data.table

recast(dat, value ~ station, id.var = "station", length)
#       value 1757 2171 2812 4501 4642 5029 5480 5779 5792
# 1     ar_1d    0    0    0    0    0    0    0    0    1
# 2     ar_2d    1    0    0    0    1    1    0    2    1
# 3 lm_h_step    1    2    2    2    1    1    2    0    0
Run Code Online (Sandbox Code Playgroud)

  • @shadow我这里只使用`data.table`包.它可以用`reshape2`来完成,我猜,但效率可能会低一些. (2认同)
  • 在`data.table`的开发版本中也是如此.发布版本实际上还没有函数`dcast`,但需要使用`dcast.data.table`.所以对我来说,你的代码使用`reshape2 :: dcast`而不是`dcast.data.table`. (2认同)