Hello! I am trying to modify the shape of my dataset so that the "Topic" columns get carried over into their own rows.
I want df1 to look like df2:
df1 <- data.frame (ID = c(100, 101, 102, 103),
Topic1 = c("Cats", "Dogs", "Horses", "Pigs"),
Topic2 = c("Dogs", "Cats", "", "Cats"),
Topic3 = c("Pigs", "Goats", "", "Cats"),
Year = c(2011, 2012, 2011, 2014)
)
df2 <- data.frame(ID = c(100, 100, 100,
101, 101, 101,
102, 102, 102,
103, 103, 103),
Topic = c("Cats", "Dogs", "Pigs",
"Dogs", "Cats", "Goats",
"Horses", "", "",
"Pigs", "Cats", "Cats"),
Year = c(2011, 2011, 2011,
2012, 2012, 2012,
2011, 2011, 2011,
2014, 2014, 2014)
)
I am not sure if there is a normalization function in R? This article gives an example of what I'm trying to do. Maybe the solution is a pivot_longer?
Any help appreciated!