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.
DF[] <- lapply(DF, function(x) gsub(', NA', '', x))
@ Dave2e提供了更通用的解决方案,该解决方案适用于以NA开头的字符串:
DF[] <- lapply(DF, function(x) gsub("[ ,]{0,3}NA", "", x))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.