带有dcast data.table的宽格式

时间:2016-06-03 20:34:33

标签: r data.table dcast

我想转换像这样的表(*):

set.seed(1)
mydata <- data.frame(ID=rep(1:4, each=3), R=rep(1:3, times=4), FIXED=rep(runif(4), each=3), AAA=rnorm(12), BBB=rbinom(12,12,0.5), CCC=runif(12))

ID R    FIXED    AAA  BBB   CCC
 1 1    0.26   -0.83   8   0.82
 1 2    0.26    1.59   5   0.64
 1 3    0.26    0.32   6   0.78
 2 1    0.37   -0.82   6   0.55
 2 2    0.37    0.48   6   0.52
 2 3    0.37    0.73   4   0.78
 3 1    0.57    0.57   8   0.02
 3 2    0.57   -0.30   7   0.47
 3 3    0.57    1.51   7   0.73
 4 1    0.90    0.38   4   0.69
 4 2    0.90   -0.62   7   0.47
 4 3    0.90   -2.21   6   0.86    

进入宽格式,如下:

ID  FIXED   AAA1    BBB2    CCC2    FIXED2  AAA2    BBB2    CCC2    FIXED3  AAA3    BBB3    CCC3
1   0.27    0.49       7    0.73     0.37   0.74       4    0.69      0.57  0.58       7    0.48
2   0.91    -0.31      6    0.86     0.20   1.51       8    0.44      0.90  0.39       7    0.24
3   0.94    -0.62      7    0.07     0.66  -2.21       6    0.10      0.63  1.12       6    0.32
4   0.06    -0.04      7    0.52     0.21  -0.02       3    0.66      0.18  0.94       6    0.41

我该怎么做? 我试过

dcast(mydata, ID + FIXED ~ R, value.var=(names(mydata)[3:5])   

甚至写出列名“AAA”,“BBB”,“CCC”,但它会产生错误,我无法获得我需要的宽格式。我也尝试了其他选择,没有运气。

我该怎么做?

(*)实际上有更多的专栏,但故事是一样的。

错误是:

Error in .subset2(x, i, exact = exact) : 
  recursive indexing failed at level 2
In addition: Warning message:
In if (!(value.var %in% names(data))) { :
  the condition has length > 1 and only the first element will be used

2 个答案:

答案 0 :(得分:3)

您正在引用错误的值变量(AAABBBCCC列的索引编号为4 - 6),您应该使用setDT()来转换数据框到数据表。使用:

dcast(setDT(mydata), ID + FIXED ~ R, value.var = names(mydata)[4:6])

给出:

   ID     FIXED      AAA_1      AAA_2      AAA_3 BBB_1 BBB_2 BBB_3     CCC_1     CCC_2     CCC_3
1:  1 0.2655087 -0.8356286  1.5952808  0.3295078     8     5     6 0.8209463 0.6470602 0.7829328
2:  2 0.3721239 -0.8204684  0.4874291  0.7383247     6     6     4 0.5530363 0.5297196 0.7893562
3:  3 0.5728534  0.5757814 -0.3053884  1.5117812     8     7     7 0.0233312 0.4772301 0.7323137
4:  4 0.9082078  0.3898432 -0.6212406 -2.2146999     4     7     6 0.6927316 0.4776196 0.8612095

如果您未转换为数据表,则data.table包将退回dcast reshape2的实施,该value.var无法处理多个sep = '.'的,因此错误信息。

如果您想要其他分隔符,可以将dcast参数添加到 stack.push(UnsobergamesApplication.combine); type = UnsobergamesApplication.combine.get(position).getType(); if (type == 0) { if(!UnsobergamesApplication.combine.isEmpty()) { cat_id = UnsobergamesApplication.combine.get(position).getId(); // UnsobergamesApplication.combine = handler.getListbyId(String.valueOf(cat_id)); Log.e("Combine list is..", UnsobergamesApplication.combine.toString()); adapter = new Category_Adapter(SubCategoryActivity.this, android.R.layout.simple_list_item_1, UnsobergamesApplication.combine); // adapter = new ArrayAdapter(SubCategoryActivity.this, android.R.layout.simple_list_item_1, UnsobergamesApplication.combine); listView_Combine.setAdapter(adapter); } } else if (type == 1) { Log.e("next screen", String.valueOf(type)); UnsobergamesApplication.title_name = UnsobergamesApplication.combine.get(position).getName(); UnsobergamesApplication.description = UnsobergamesApplication.combine.get(position).getDescription(); Intent intent = new Intent(SubCategoryActivity.this,Details_Activity.class); startActivity(intent); overridePendingTransition(R.anim.push_left_in, R.anim.push_up_out); }

答案 1 :(得分:1)

serviceA.serviceB