-7

I have 24 rows of data, and 2820 columns. How can I split this large horizontally table to vertical, cut every 60 column and stack it under each other:

This is a sample pic of 12 col and 6 rows:

Before:

Before

After:

After

A5C1D2H2I1M1N2O1R2T1
  • 177,446
  • 27
  • 370
  • 450
Vip
  • 21
  • 1
  • 1
  • 4
  • 3
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions – MrFlick Feb 07 '18 at 21:10
  • Sorry, I just uploaded some examples to illustrate the question. Thank you. – Vip Feb 08 '18 at 19:08

3 Answers3

0

I'm not sure I understand the question, though I think what you're looking for is the melt() function. This will convert your wide dataset to a long one. That being said, I have no idea what your intentions are, so I'm not sure if the number 60 has any importance.

If melt() is what you want, you can use it by specifying an id variable, suppose its col1, and everything else will automatically become a variable. If your table is named "x":

col1  col2  col3  col4  ...
A     22     5    17
B     43     6    54
...

melt(x, id.vars = col1)

col1  variable  value
A     col2      22     
A     col3      5
A     col4      17
...
bk18
  • 1,157
  • 6
  • 19
  • 1
    I don't think wide-long is the problem here. My understanding is that they just want to "slice" into columns 1-60, 61-120...2761-2820 and then essentially `rbind` those slices together. – neilfws Feb 07 '18 at 21:36
  • This is a dangerous thing to do, unless the columns have no independent meaning in the first place, in which case why aren't they in the same column to begin wiith? – bk18 Feb 07 '18 at 21:37
  • But I agree that's what it sounds like the op is asking. – bk18 Feb 07 '18 at 21:37
  • Yes, I want to slice the table. And columns have no independent meaning. I have this table as a dataframe. – Vip Feb 07 '18 at 21:49
  • The answer below works for a `data.frame` same like for a `matrix` – RolandASc Feb 08 '18 at 10:18
0

Including a reduced example, you could do the following:

# sample data
myMat <- matrix(rnorm(24*282), nrow = 24)

myList <- lapply(1:(282/6), function(x) myMat[, (6*(x-1)+1):(6*x)])

myNewMat <- do.call("rbind", args = myList)

dim(myNewMat)
## [1] 1128    6
RolandASc
  • 3,654
  • 1
  • 8
  • 26
0

A variation on @RolandASc's answer would be something like the following:

mydf
#   X_1      X_2  X_3 X_4 X_5      X_6  X_7 X_8 X_9     X_10  X_11 X_12
# 1  A1 1/1/2016 2:30   5  D1 1/1/2017 5:30  15  A2 5/1/2016 12:30   50
# 2  B1 1/2/2016 3:30   5  E1 1/2/2017 6:30  25  B2 5/2/2016 13:30  500
# 3  C1 1/3/2016 4:30   8  F1 1/3/2017 7:30  80  C2 5/3/2016 14:30  800

library(data.table)
rbindlist(split.default(mydf, 0:(ncol(mydf)-1) %/% 4))
#    X_1      X_2   X_3 X_4
# 1:  A1 1/1/2016  2:30   5
# 2:  B1 1/2/2016  3:30   5
# 3:  C1 1/3/2016  4:30   8
# 4:  D1 1/1/2017  5:30  15
# 5:  E1 1/2/2017  6:30  25
# 6:  F1 1/3/2017  7:30  80
# 7:  A2 5/1/2016 12:30  50
# 8:  B2 5/2/2016 13:30 500
# 9:  C2 5/3/2016 14:30 800

To do this every 60 columns for a data.frame with 2820 columns, you can use the following to create the split indices:

0:(2820-1) %/% 60

Sample data:

mydf <- data.frame(
  X_1 = c("A1", "B1", "C1"), X_2 = c("1/1/2016", "1/2/2016", "1/3/2016"), 
  X_3 = c("2:30", "3:30", "4:30"), X_4 = c(5, 5, 8), 
  X_5 = c("D1", "E1", "F1"), X_6 = c("1/1/2017", "1/2/2017", "1/3/2017"), 
  X_7 = c("5:30", "6:30", "7:30"), X_8 = c(15, 25, 80), 
  X_9 = c("A2", "B2", "C2"), X_10 = c("5/1/2016", "5/2/2016", "5/3/2016"), 
  X_11 = c("12:30", "13:30", "14:30"), X_12 = c(50, 500, 800)
)
A5C1D2H2I1M1N2O1R2T1
  • 177,446
  • 27
  • 370
  • 450