Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201919

Data Table R: molten or reshape data table without stacked variable column

$
0
0

Data

str(DT)
Classes ‘data.table’ and 'data.frame':  6 obs. of  3b variables:
 $ A  : Factor w/ 6 levels "0","1","2","3",..: 1 2 3 4 5 6
 $ N  : num  13 11 10 5 1 1
 $ ID : Factor w/ 1 level "1": 1 1 1 1 1 1

I'd like to reshape this DT to the DT below. If I melt DT by melt.data.table(DT, id.vars = "ID", measure.vars = 1:2) I get a following wacky melted DT.

   ID  N     variable value
 1: 1 13         A     0
 2: 1 11         A     1
 3: 1 10         A     2
 4: 1  5         A     3
 5: 1  1         A     4
 6: 1  1         A     5
 7: 1 13         N    13
 8: 1 11         N    11
 9: 1 10         N    10
10: 1  5         N     5
11: 1  1         N     1
12: 1  1         N     1

My desired output is as follows. Basically, molten DT should have a variable (or new) column without stacked variable columns from an pre-molten DT

Desired Output

     ID   variable  value    N 
 1:   1     A        0      13
 2:   1     A        1      11
 3:   1     A        2      10
 4:   1     A        3       5  
 5:   1     A        4       1
 6:   1     A        5       1

I'd appreciate any pointers

Little late but here is an original data table:

   A  N  ID
1:   0 13   1
2:   1 11   1
3:   2 10   1
4:   3  5   1
5:   4  1   1
6:   5  1   1

Viewing all articles
Browse latest Browse all 201919

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>