I have a simple data frame in R Studio that I would like to pivot. I have been using the 'gather()' and 'spread()' functions for more complex transposition, but I cannot figure out how to do a more simple pivot.
I have the following data:
structure(list(MeanExports = c(1581743294.4, 13114218975.85,
376303283.26, 367309850000, 60019049993.7, 632530400000, 4276027773.125,
3.63436e+11, 1530830933.675, 317033050000), Development = c("Less",
"Less", "Less", "More", "More", "More", "Less", "More", "Less",
"More")), row.names = c(331L, 2635L, 4363L, 13435L, 16171L, 16747L,
17323L, 17755L, 18331L, 29563L), class = "data.frame")
I would like to be able to create a new data frame with the columns 'More Developed' and 'Less Developed' with five rows (there are five values for each of these categories). There is lots of advice online for more complicated procedures, but I am sure there will be a simple method that I am overlooking. Any help would be amazing.
stack()
and unstack()
are (often overlooked) base R functions.
dfw <- unstack(df)
names(dfw) <- paste(names(dfw), 'Developed')
# Less Developed More Developed
# 1 1581743294 367309850000
# 2 13114218976 60019049994
# 3 376303283 632530400000
# 4 4276027773 363436000000
# 5 1530830934 317033050000