structure(list(Other = c(NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_),
Years = c("2005, 2005, 2006, 2006, 2007", "2011, 2014",
"2007", "2011, 2011, 2011, 2012, 2012, 2012",
"2006, 2006, 2012, 2012, 2015")),
.Names = c("Other", "Years"), row.names = 1:4, class = "data.frame")
Given the above data frame, the second column has a bunch of years arranged with commas. I'd like to create a new column which adds the total number of years in each element in the column. So the final data frame looks like this:
structure(list(Other = c(NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_),
Years = c("2005, 2005, 2006, 2006, 2007","2011, 2014", "2007",
"2011, 2011, 2011, 2012, 2012, 2012",
"2006, 2006, 2012, 2012, 2015"),
yearlength = c(5, 2, 1, 6, 5)),
.Names = c("Other", "Years", "yearlength"), row.names = 1:4, class = "data.frame")
I've tried with solution such as stack$yearlength <- count.fields(textConnection(stack), sep = ",")
but I can't quite get it to work.