I'm using R and I have a data frame called df
which has (n*P) rows and N columns.
C1 C2 ... CN-1 CN
1-1 100 36 ... 136 76
1-2 120 -33 ... 87 42
1-3 150 14 ... 164 24
:
1-n 20 36 ... 136 76
2-1 109 26 ... 166 87
2-2 -33 87 ... 42 24
2-3 100 36 ... 136 76
:
2-n 100 36 ... 136 76
:
P-1 150 14 ... 164 24
P-2 100 36 ... 765 76
P-3 150 14 ... 164 94
:
P-n 10 26 ... 106 76
And I want to transform this data frame into a data frame with n rows and (N*P) columns. The new data frame, df.new
, should look like
C1-1 C2-1 ... CN-1-1 CN-1 C1-2 C2-2 ... CN-1-2 CN-2 ... C1-P C2-P ... CN-1-P CN-P
R1 100 36 ... 136 76 20 36 ... 136 76 ... 150 14 ... 164 24
R2 120 -33 ... 87 42 109 26 ... 166 87 ... 100 36 ... 765 76
:
:
Rn 20 36 ... 136 76 100 36 ... 136 76 ... 10 26 ... 106 76
That is to say, the first N columns of df.new
are rbind of rows 1-1, 2-1, 3-1, ... , P-1 of df
. The next N columns of df.new
are rbind of rows 1-2, 2-2, 3-2, ... , P-2 of df
. It follows till the last N columns of df.new
which will be composed of rows rows 1-n, 2-n, 3-n, ... , P-n of df
. (R1 of df.new
is cbind of rows 1-1, 1-2,...,1-n. R2 of df.new
is cbind of rows 2-1, 2-2,...,2-n. Rn of df.new
is cbind of rows P-1, P-2,...,P-n.)
n, P and N are variables so the value of them depend on the case. I tried to create df.new
using for loops but doesn't work well.
Here is my try which I kind of gave up.
for (j in 1:n) {
df.new <- data.frame(matrix(vector(), 1, dim(df)[2],
dimnames = list(c(), colnames(df))),
stringsAsFactors=F)
for (i in 1:nrow(df)) {
if (i %% n == 0) {
df.new <- rbind(df.new, df[i,])
} else if (i %% n == j) {
df.new <- rbind(df.new, df[i,])
}
}
assign(paste0("df.new", j), df.new)
}