简体   繁体   English

在 R 中重塑海量数据的有效方法

[英]Efficient way to Reshape Huge Data in R

I have a huge data in R that I am trying to reshape it in an efficient way.我在R中有大量数据,我正试图以有效的方式对其进行重塑。 As you see, the changes happen in column Med and Num .如您所见,更改发生在MedNum列中。 BTW, I have already tried to find a rigid answer but I was not successful.顺便说一句,我已经尝试找到一个严格的答案,但我没有成功。

Any idea would be great!任何想法都会很棒!

Thanks.谢谢。

Original shape:原来的形状:

User    Date    Sex    Age  Ethic   Med    Num  Diag   Dis  Doc   Month
27168   1/1/2002    1   49  NULL    42506   1   2000    0   13545   1
27168   1/1/2002    1   49  NULL    32603   9   2000    0   13545   1
27168   1/1/2002    1   49  NULL    32602   5   2000    0   13545   1
27168   1/1/2002    1   49  NULL    34533   1   2000    0   13545   1
12335   2/1/2002    0   87  2       42506   2   8654    2   34568   1
12335   2/1/2002    0   87  2       65873   4   8654    2   34568   1

Desired Shape:所需形状:

User    Date   Sex  Age Ethic  42506  32603  32602  34533  65873  Diag  Dis  Doc  Month
27168 1/1/2002  1   49  NULL     1      9      5      1      0    2000   0  13545   1
12335 2/1/2002  0   87   2       2      0      0      0      4    8654   2  34568   1
df %>%
   pivot_wider(names_from = Med, values_from = Num, values_fill = 0)

# A tibble: 2 x 14
   User Date       Sex   Age Ethic  Diag   Dis   Doc Month `42506` `32603` `32602` `34533` `65873`
  <int> <chr>    <int> <int> <chr> <int> <int> <int> <int>   <int>   <int>   <int>   <int>   <int>
1 27168 1/1/2002     1    49 NULL   2000     0 13545     1       1       9       5       1       0
2 12335 2/1/2002     0    87 2      8654     2 34568     1       2       0       0       0       4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM