0

I'm trying to convert from wide format into long format, but can't find good result. The point is that i need to make it in two ways- using packet Tidyverse, and without it, while i have problems with one way.

that is my data frame:

df2 <- data.frame(name = c("John", "Anna", "Jonte"),
              Matematyka = c(2, 3, 5),
              Polski = c(5, 5, 4)) 

and i need to convert the data frame into this:

 name subject mark
1 John math 2
2 Anna math 3
3 Jonte math 5
4 John eng 5
5 Anna eng 5
6 Jonte eng 4 

i have something like this, but it have some errors (look down)

reshape(df2, direction="long", varying = list(names(df2)[2:3]), v.names =list ("subject","mark"), idvar = "name")

but its only in one way... I need to make it using tidyverse and without tidyverse package, any ideas?

 Error in min(j) : invalid 'type' (list) of argument
yarnabrina
  • 1,561
  • 1
  • 10
  • 30
Potato
  • 172
  • 1
  • 12
  • The tidyverse has the `gather` and `spread` functions. The reshape2 has `cast` and `melt`. – R. Schifini May 19 '19 at 14:34
  • Using base `reshape` use: `r <- reshape(df2, direction="long", varying = list(2:3), timevar = "subject", times = names(df2[2:3]), v.names = "mark", idvar = "name"); rownames(r) <- NULL` and `reshape(r, direction = "wide")` and with dplyr/tidyr use `g <- gather(df2, subject, grade, -name)` and `spread(g, subject, grade)` . – G. Grothendieck May 19 '19 at 14:41

0 Answers0