0

So I have figured out a solution on how I can transform my data from wide to long. The dataset had 190 columns representing 19 variables for 10 years (and additionally 31 columns that did not require transformation).I did it through rbinid(ddply) from the plyr package.

It is very heavy and very long.

I tried this solution (among others), but the syntax is off

My data:

DF <- structure(list(id = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 25L,25L, 25L), .Label
                                    = c(1:1000), class = "character"),
                     X  = c(809L, 81L, 862L,747L, 224L, 721L, 310L, 53L, 853L, 642L),
                     "X (- 1 år)" = c(926L, 409L,825L, 702L, 803L, 63L, 319L, 941L, 598L, 830L),
                     Y  = c(447L,164L, 8L, 775L, 471L, 196L, 30L, 420L, 47L, 327L),
                     "Y (- 1 år)" = c(335L,164L, 503L, 407L, 662L, 139L, 111L, 721L, 340L, 178L), 
                     Year = c(2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021), 
                     "Year (- 1år)" = c(2020, 2020, 2020, 2020, 2020, 2020, 2020,2020, 2020, 2020), 
                     bcat = c("yes", "no","yes", "no","yes", "yes","yes", "no","yes", "no")), .Names = c("id","X",
                                                                                                     "X (- 1 år)", "Y", "Y (- 1 år)", "Year", "Year (-1 år)", "bcat"), class = "data.frame", row.names = c(1L,2L, 3L, 4L, 5L, 6L, 7L, 98L, 99L, 100L))

DF$id <- as.character(sample(100, size = nrow(DF), replace= TRUE))

Code that needs fitting:

reshape (DF, 
         direction = "long",
         varying = list(names(DF) [2:5]),
         v.names = "Y", "X",
         idvar = c("id"),
         timevar = "Year", "Year (-1 år)",
         times = 2020:2021)

its times 10 because of 10 years. (In the example only 2) 10 of the columns just specify year in all columns (I made them as 1) 180 define values for 18 categories for each year 31 columns define values that are identical for all those years.

Any solutions?

1 Answers1

0

I usually use the function reshape() in base R or gather() from tidyr to transform. There is some debate about which is better functionally, but they utilize shorter code and may be more efficient. Here is a link to the R codebook for gather() and the newer reshape2() function. http://www.cookbook-r.com/Manipulating_data/Converting_data_between_wide_and_long_format/

Also, here's a link from a previous debate on stack about which option is better (they're talking about long to wide but it is similar code). How to reshape data from long to wide format

  • Hi, thanks for reaching out and providing links. The Stack-examples Im aware of, they are good but dont fix my issue. The cookbook is also excellent for future use. So this example is quite close, yet they already have a year-column and only need to get the values to long. - What I have, are multiple columns with values, and multiple columns with years. Any Idea how I could edit this: reshape(d, direction = "long", varying = list(names(d)[3:7]), v.names = "Value", idvar = c("Code", "Country"), timevar = "Year", times = 1950:1954) – SarpsRosing Oct 21 '22 at 11:52
  • I made an adjustment to make it simpler to read. Do you think you know the alternative way to go here? – SarpsRosing Oct 21 '22 at 13:37