I am trying to reshape some of my data and haven't had any luck with the reshape2 package. Nothing I've tried is quite what I'm looking for given my data.
I have a data.frame that looks like this (where name1 corresponds to age1, and name2 corresponds to age2, etc):
city <- c("New York", "Philadelphia", "Chicago")
state <- c("NY", "PA", "IL")
name1 <- c("Tim", NA, NA)
name2 <- c("Bob", "Jim", "Bill")
name3 <- c(NA, NA, "Jeff")
age1 <- c(40, NA, NA)
age2 <- c(30, 29, 34)
age3 <- c(NA, NA, 27)
df <- data.frame(city, state, name1, name2, name3, age1, age2, age3)
city state name1 name2 name3 age1 age2 age3
1 New York NY Tim Bob <NA> 40 30 NA
2 Philadelphia PA <NA> Jim <NA> NA 29 NA
3 Chicago IL <NA> Bill Jeff NA 34 27
In my actual data set, I have about 50 "name" and "age" variables each.
Ultimately, I'm aiming for 4 variables: city, state, name, age. I also don't want the NA's in my finished data set. So what I'm looking to achieve is this:
city state name age
1 New York NY Tim 40
2 New York NY Bob 30
3 Philadelphia PA Jim 29
4 Chicago IL Bill 34
5 Chicago IL Jeff 27
Any help would be greatly appreciated! I've tried the reshape2 and tidyR packages, but no luck yet!
Thanks!