18

In python, and more specifically in pandas, I can work with MultIndex on rows or columns. Is there an equivalent in R? I was checking several tutorials, such as the one in https://en.wikibooks.org/wiki/R_Programming/Working_with_data_frames, but I couldn't find a proper R equivalent.

As an example I have the following data frame:

   A-1  A-2 B-1 B-2
0  1    2    0   1
1  2    0    1   3
2  4    1    3   2

I want it to look like:

   A         B
   1    2    1   2
0  1    2    0   1
1  2    0    1   3
2  4    1    3   2

Other relevant answers I have found from stackoverflow

  1. Set columns as index
  2. Paste multiple columns to an index
Community
  • 1
  • 1
goofd
  • 2,028
  • 2
  • 21
  • 33
  • http://stackoverflow.com/questions/17560683/create-a-table-in-r-with-header-expanding-on-two-columns-using-xtable-or-any-pac – user227710 Jun 19 '15 at 17:49
  • 6
    The power of `MultiIndex` is one of the reasons python/pandas is sometimes preferable to R for data manipulation. `R` does not support the same type of hierarchical indexing. – tegancp Jun 19 '15 at 17:52
  • @user227710 This looks promising. does this `tables` package also allows you to do a multi-index on rows? – goofd Jun 19 '15 at 17:57
  • @tegancp hmm I see. I am mainly looking for some work around. personally I love the `MultiIndex` feature in `pandas` – goofd Jun 19 '15 at 17:58
  • 1
    R provides an array-class that might offer some of the features illustrated but doesn't have a print method that would immediately display as you demonstrated. There is an `ftable` function that allows "flattening" of higher dimensioned tables. – IRTFM Jun 19 '15 at 20:51
  • @tegancp R data.table does support multi key indexing. – Adam Lee Perelman Nov 10 '21 at 09:09

1 Answers1

9

Given that you were looking for a "work around" I will give you an admittedly limited one. Arrays in R can only hold one mode (which contrary to most people's understanding can include lists)

>   arr1 <- matrix(scan(), 3,byrow=TRUE) 
1:   1    2    0   1
5:   2    0    1   3
9:   4    1    3   2
13: 
Read 12 items
> arr2 <- array(arr1, c(3,2,2))  # Re-dimensioning can also be done with `dim<-`
> arr2
, , 1

     [,1] [,2]
[1,]    1    2
[2,]    2    0
[3,]    4    1

, , 2

     [,1] [,2]
[1,]    0    1
[2,]    1    3
[3,]    3    2

> dimnames(arr2) <- list( rows=0:2, subcat=1:2, majorcat=c("A","B") )
> arr2
, , majorcat = A

    subcat
rows 1 2
   0 1 2
   1 2 0
   2 4 1

, , majorcat = B

    subcat
rows 1 2
   0 0 1
   1 1 3
   2 3 2

After setting this up, there is a display method that delivers something like what you requested:

> ftable(arr2, row.vars=1)
     subcat   1   2  
     majorcat A B A B
rows                 
0             1 0 2 1
1             2 1 0 3
2             4 3 1 2

Looks like I needed to specify it differently :

> ftable(arr2, row.vars=1, col.vars=3:2)
     majorcat A   B  
     subcat   1 2 1 2
rows                 
0             1 2 0 1
1             2 0 1 3
2             4 1 3 2
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • 1
    great! I know i didn't mention this in the question but in order to export this to a csv/excel am guessing i need to do the following? http://stackoverflow.com/a/28640799/2423379 – goofd Jun 21 '15 at 04:49
  • If it doesn't paste as you expect, Excel has a very neat fixed width import wizard. – IRTFM Jun 21 '15 at 07:09