Supppose I have this data-frame:
my_df <- data.frame(area = c(200, 300, 400, 500, 600, 201, 301, 401, 501, 601),
type = c("urban", "forest", "shrubs", "water", "vegetation", "urban", "forest", "shrubs", "water", "vegetation"),
year = c(2015,2015,2015,2015,2015,2016,2016,2016,2016,2016))
my_df
#> area type year
#> 1 200 urban 2015
#> 2 300 forest 2015
#> 3 400 shrubs 2015
#> 4 500 water 2015
#> 5 600 vegetation 2015
#> 6 201 urban 2016
#> 7 301 forest 2016
#> 8 401 shrubs 2016
#> 9 501 water 2016
#> 10 601 vegetation 2016
I would like to convert the attributes from column type to new column names with expected output like this:
#> urban forest shrubs water vegetation year
#> 1 200 300 400 500 600 2015
#> 2 201 301 401 501 601 2016
I however encountered similar questions here and ended up using tidyr::pivot_wider but I couldn't get exactly what I wanted.
It would be great help if you could help me on this part. If possible, base R functions would be preferred.