I have several years worth of data on individuals, but their names are formatted differently each year. Half of the names are already in "First Last" order but I can't figure out how to successfully edit the other half ("Last, First").
Here's a sample df:
name <- c("First1 Last1","Last2, First2", "Last3, First3", "First4 Last4", "First5 Last5")
salary <-c(51000, 72000,125000,67000,155000)
year <-c(2012,2014,2013,2013,2014)
df <- data.frame(name, salary, year, stringsAsFactors=FALSE)
Here are things I've tried: split up the text by comma:
df$name2 <- strsplit(df$name, ", ") #to split the character string by comma
df$name3 <-paste(df$name2, collapse=" ") #to collapse the newly created vectors back into a string
df$name4 <-paste(rev(df$name2)) #to try pasting each vector in reverse order
df$name5 <-paste(rev(df$name2)[2:1]) #trying again...
I've printed the correct names, but backwards, and printed them on the wrong rows, but despite all googling I can't get it to work correctly. What am I doing wrong?