Reputation: 1083
DF <- structure(list(`2005` = c(NA, NA, NA, "30, NA", "18", NA), `2006` = c(NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_
), `2007` = c("15", NA, "18", NA, "30, 18, NA", NA), `2008` = c("16",
NA, NA, "30, 27, NA", "18, 30, NA", NA), `2009` = c("15", NA,
NA, "20, NA", "30, 18, NA", NA), `2010` = c(NA, NA, NA, "30, NA, 20",
NA, NA), `2011` = c(NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_), `2012` = c(NA,
NA, NA, "20, 30", NA, "26"), `2013` = c("15", NA, "19", NA, NA,
NA), `2014` = c(NA, NA, "18", NA, NA, NA), `2015` = c(NA, NA,
"18", NA, "18, NA", NA), `2016` = c(NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_)), .Names = c("2005",
"2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013",
"2014", "2015", "2016"), row.names = c(NA, 6L), class = "data.frame")
Given the above data frame, some elements contain a vector of "30, NA, 20"
or "18, 30, NA"
. I would like R to remove these NA values from the strings with numbers so it only outputs "30, 20"
and "18, 30"
respectively.
I have tried with different functions of gsub
but it doesn't seem to be working quite well and most questions I find while searching aren't applicable to my situation.
Upvotes: 0
Views: 34
Reputation: 28675
DF[] <- lapply(DF, function(x) gsub(', NA', '', x))
More general solution by @Dave2e, which works for strings starting with NA:
DF[] <- lapply(DF, function(x) gsub("[ ,]{0,3}NA", "", x))
Upvotes: 1