1

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)
  }
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Makoto Miyazaki
  • 1,743
  • 2
  • 23
  • 39

1 Answers1

0
library(dplyr)
library(tidyr)
library(tibble)

df %>%
  rownames_to_column("rowname") %>%
  separate(rowname, c("rowname_prefix", "rowname_suffix"), "-") %>%
  gather(col_name, value, -rowname_prefix, -rowname_suffix) %>%
  mutate(col_name = paste(col_name, rowname_prefix, sep="-")) %>%
  select(-rowname_prefix) %>%
  spread(col_name, value) %>%
  mutate(rowname_suffix = paste0("R", rowname_suffix)) %>%
  column_to_rownames("rowname_suffix")

Output is:

   C1-1 C1-2 C1-3 C2-1 C2-2 C2-3 C3-1 C3-2 C3-3 C4-1 C4-2 C4-3
R1  100  109  150   36   26   14  136  166  164   76   87   24
R2  120  -33  100  -33   87   36   87   42  765   42   24   76
R3  150  100  150   14   36   14  164  136  164   24   76   94
R4   20  100   10   36   36   26  136  136  106   76   76   76

Sample data:

df <- structure(list(C1 = c(100L, 120L, 150L, 20L, 109L, -33L, 100L, 
100L, 150L, 100L, 150L, 10L), C2 = c(36L, -33L, 14L, 36L, 26L, 
87L, 36L, 36L, 14L, 36L, 14L, 26L), C3 = c(136L, 87L, 164L, 136L, 
166L, 42L, 136L, 136L, 164L, 765L, 164L, 106L), C4 = c(76L, 42L, 
24L, 76L, 87L, 24L, 76L, 76L, 24L, 76L, 94L, 76L)), .Names = c("C1", 
"C2", "C3", "C4"), class = "data.frame", row.names = c("1-1", 
"1-2", "1-3", "1-4", "2-1", "2-2", "2-3", "2-4", "3-1", "3-2", 
"3-3", "3-4"))
#     C1  C2  C3 C4
#1-1 100  36 136 76
#1-2 120 -33  87 42
#1-3 150  14 164 24
#1-4  20  36 136 76
#2-1 109  26 166 87
#2-2 -33  87  42 24
#2-3 100  36 136 76
#2-4 100  36 136 76
#3-1 150  14 164 24
#3-2 100  36 765 76
#3-3 150  14 164 94
#3-4  10  26 106 76
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Prem
  • 11,775
  • 1
  • 19
  • 33