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:
After:
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:
After:
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
...
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
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)
)