1

I have a data set.

structure(list(X = c("ID", NA, "TOM", "TOM", "TOM", "TOM", "TOM", 
"JAY", "JAY", "JAY", "JAY", "JAY"), X.1 = c("number", NA, "1", 
"1", "1", "1", "1", "2", "2", "2", "2", "2"), X.4 = c("10/1", 
"1", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a"), X.5 = c("10/1", 
"2", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a"), X.6 = c("10/1", 
"3", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a"), X.7 = c("10/1", 
"4", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a"), X.8 = c("10/1", 
"5", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a"), X.9 = c("10/1", 
"6", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a"), X.10 = c("10/1", 
"7", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a"), X.11 = c("10/1", 
"8", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a"), X.13 = c("10/2", 
"1", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a"), X.14 = c("10/2", 
"2", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a"), X.15 = c("10/2", 
"3", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a"), X.16 = c("10/2", 
"4", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a"), X.17 = c("10/2", 
"5", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a"), X.18 = c("10/2", 
"6", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a"), X.19 = c("10/2", 
"7", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a"), X.20 = c("10/2", 
"8", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a")), row.names = c(3L, 
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L), class = "data.frame")


ID number 10/1 10/1 10/1 10/2 10/2 10/2
N/A N/A    1    2    3    1     2    3
Tom  1     a    a    a    a     a    a
Jay  1     a    a    a    a     a    a

I want to melt date like 10/1... and division like 1, 2, 3 into columns.

What I want is:

ID number variable1 variable2 value
Tom  1       10/1       1       a
Tom  1       10/1       2       a
Tom  1       10/1       3       a
Tom  1       10/2       1       a
Tom  1       10/2       2       a
Tom  1       10/2       3       a
Jay  1       10/1       1       a
Jay  1       10/1       2       a
Jay  1       10/1       3       a
Jay  1       10/2       1       a
Jay  1       10/2       2       a
Jay  1       10/2       3       a
Jay  1       10/1       1       a

The melt function of reshape2 supports only one pair of column names.

Yun
  • 89
  • 6
  • 1
    Please make your input data reproducible (Check out `dput()` if you haven't) – s_baldur Oct 25 '21 at 10:58
  • Does that answer your quesiton? https://stackoverflow.com/questions/2185252/reshaping-data-frame-from-wide-to-long-format – deschen Oct 25 '21 at 11:07
  • 1
    @deschen No.. I want to transform date (10/1, 10/2...) and 1, 2, 3 row data. – Yun Oct 25 '21 at 11:26
  • The question is not a duplicate. The linked answer doesn't answer this question because this example has a second ID row in the data frame – Scransom Oct 25 '21 at 11:27
  • If your data are `df` then reset the names using `df_names <- paste(names(df), df[1,], sep = "_") ` then: `names(df)[3:8] <- df_names[3:8]`, then use `pivot_longer ` from `tidyr` or `tidyverse` : `df[2:nrow(df),] %>% pivot_longer( cols = !c(ID, number), names_to = c("variable1", "variable2"), names_sep = "_", values_to = "value" )` – Scransom Oct 25 '21 at 11:28
  • I still consider this a duplicate. The only difference is to a) get rid of the first row and then after reshaping to long format, append the 1, 2, 3 info through a simple `rep(1:3, 6)` thing. – deschen Oct 25 '21 at 11:37
  • @Scransom This seems to work fine. I'll give it a try. – Yun Oct 25 '21 at 12:20
  • "...this a duplicate. The only difference..." :thinking_face_emoji: – Scransom Oct 26 '21 at 06:05

0 Answers0