0

I would like to turn this dataframe from wide to long. I know that there are some similar posts about the topic, for instance I have tried all the options from this one:

Reshaping wide to long with multiple values columns

but I keep having the same error message: Warning in value[[jj]][ri] <- if (is.factor(xij)) as.vector(xij) else xij : number of items to replace is not a multiple of replacement length

I don't know how I can fix this, any help? Thank you very much

df3<-structure(list(id = structure(c("SA01", "SA02", "SA03", "SA04", 
"SA05", "SA06", "SA07", "SA08", "SA09", "SA10", "SA11", "SA12", 
"SA13", "SA14", "SA15", "SA16", "SA17", "SA18", "SA19", "SA20"
), label = "Code of PrevenGo", format.spss = "A5", display_width = 12L), 
    group = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Metab", 
    "SA", "SC"), class = "factor"), age_1 = structure(c(11.47, 
    9.6, 6.36, 10.72, 11.32, 11.66, 11.67, 7.29, 11.85, 10.72, 
    10, 9, 11.91, 10.8, 11.1, 10.61, 11.11, 7.6, 9.24, 10.72), format.spss = "F16.2", display_width = 15L), 
    whz_1 = structure(c(-0.43, 1.55, -0.19, 0.94, 0.87, 0.32, 
    1.97, 1.54, 1.68, 1.79, 1.25, 0.78, -0.86, 1.75, 0.28, 2.02, 
    3.14, 1.67, 1.03, 2.21), label = "Weight/Length", format.spss = "F5.2", display_width = 11L), 
    age_2 = structure(c(12.02739726, 10.07945205, 6.890410959, 
    11.28219178, 11.86575342, NA, 12.21369863, 7.805479452, NA, 
    11.31780822, 9.561643836, 9.561643836, 12.46575342, 11.35616438, 
    11.66027397, 11.14520548, 11.64383562, NA, NA, 11.19726027
    ), label = "6m Age", format.spss = "F16.2", display_width = 15L), 
    whz_2 = structure(c(-0.82, 1.62, -0.2, 1.08, 0.76, NA, 1.76, 
    1.45, NA, 1.6, 1.07, 0.79, -0.92, 1.83, 0.48, 2.21, 3.22, 
    NA, NA, 2.09), label = "6m Weight/Length", format.spss = "F5.2", display_width = 11L)), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))

dfstack<-reshape(df3, idvar = "id", varying = list(c(3,5),c(4,6)), v.names = c("age", "whz"), direction = "long")
  • 1
    It may be easier with `pivot_longer` i.e. `library(tidyr); pivot_longer(df3, cols = contains("_"), names_to = c(".value", "index"), names_sep = "_")` – akrun Jun 14 '22 at 16:37
  • Your `reshape` code can be `reshape(df3, idvar = c("id", "group"), sep = "_", varying = list(c(3, 5), c(4, 6)), v.names = c("age", "whz"), direction = "long")` i.e you left out the 'group' column – akrun Jun 14 '22 at 16:39
  • @akrun leaving `group` should be fine. Base R `reshape` will even add am `id` column if one is not specified. – Onyambu Jun 14 '22 at 17:03
  • 2
    Yes, `reshape` is kind of unusual in its behavior – akrun Jun 14 '22 at 17:04
  • `reshape(df3, 3:ncol(df3), dir='long', sep='_')` Should work. Note that you should have same number of columns within each variable. Note that your error cannot be reproduced using the data given – Onyambu Jun 14 '22 at 17:05
  • @akrun you were right I had forgotten the group i's all working now. Thank you so much – Charlotte Juton Jun 14 '22 at 20:37
  • @onyambu Sorry, that's weird because I can reproduce it from a new markdown file – Charlotte Juton Jun 14 '22 at 20:38
  • Try running it on R console and see what hapens – Onyambu Jun 14 '22 at 20:39
  • The error is not because of the reshape. The error is somewhere else – Onyambu Jun 14 '22 at 20:40
  • @onyambu I use R studio not R console maybe it's because of this.... – Charlotte Juton Jun 15 '22 at 07:23
  • Rstudio has 4 panes, the editor, console, help, environment panes. Console is where you see the results. Rstudio is just an IDE – Onyambu Jun 15 '22 at 13:17
  • @onyambu it's working for me when I run it from a blank session. It reproduces all the warnings (Warning in value[[jj]][ri] <- if (is.factor(xij)) as.vector(xij) else xij : number of items to replace is not a multiple of replacement length) that I got before. Not sure why it does not work for you. – Charlotte Juton Jun 16 '22 at 08:35
  • That warning is coming from a different function. Are you running ONLY the code above?? – Onyambu Jun 16 '22 at 11:28
  • @onyambu yes only the code above – Charlotte Juton Jun 17 '22 at 07:49

0 Answers0