Skip to content

Instantly share code, notes, and snippets.

@erzk
Created May 11, 2015 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erzk/a495ac530124b8462f9f to your computer and use it in GitHub Desktop.
Save erzk/a495ac530124b8462f9f to your computer and use it in GitHub Desktop.
change format from wide to long
library(tidyr)
foreign_victims_wide <- read.csv("foreign_victims_wide.csv")
# reshape to long format
foreign_victims_long <- gather(foreign_victims_wide, Year, Victims, X1998:X2012)
# remove Xs
# could be omitted if using check.names=FALSE in read.csv()
foreign_victims_long$Year <- gsub("X", "", paste(foreign_victims_long$Year))
write.csv(foreign_victims_long, file = "foreign_victims_long.csv", row.names=FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment