0

I would like to convert data from wide to long format using R in Rstudio

Here's my code

df <- read.csv("bcghg_emissions_1990-2016_pub2018.csv",
               header = FALSE)
library("reshape2")                                           # Load reshape2
dl <- melt(df,                                 # Apply melt function
                   id.vars = c(""))

I am using the reshape2 library, but I am not sure how I should reshape my data, you can find a screenshot of a part of the data.

My question is what columns/rows should I put in the id.vars so it reshapes the data to long format.

Thanks a lot! screenshot of the data

Edit: after using dl <- tidyr::pivot_longer(df, cols = -(V1:V4)) here is the outcome, problem is it's not right the years are on the top I want them to replace the "name" collumn so instead of V1 or V2 we have the year of the emission.

screenshot after conversion

Ilyas Ennaboulsi
  • 29
  • 1
  • 1
  • 7

0 Answers0