Search code examples
rstringsubsetna

Remove any blank spaces of NAs


I have a vector which contains numbers, NAs and blanks where I only wish to keep the numbers. I was initially trying a gsub out everything that wasnt a number using;

gsub(!'[:digit:]', '', myexample) 

Although had no joy. Thinking more I'm not sure a gsub is the best think to use here, maybe a which() argument although I'm not sure how you'd only select digits.

Example of vector shown below;

myexample
# [1]   "319939" ""       ""       "318273" NA       "317889" NA       "316060"

Part two of my question is when we are dealing with characters rather than numbers. In this instance I wish to do the same (keep the names), and remove the nas (na-na) and black values ("-"). Although this time as this vector comes from a paste R does not recognize na-na as a true NA value.

characterexample
"random-name"      "-"                 "-"   "na-na"              "name-random"

Solution

  • I think you could use grep instead of gsub

    > myexmaple <- c("319939", "", "", "318273", NA, "317889", NA, "316060")
    
    > grep("^\\d+$", myexmaple, value = TRUE)
    [1] "319939" "318273" "317889" "316060"
    

    For you second question, you can use %in%

    > characterexample[!characterexample %in% c("na-na", "-")]
    [1] "random-name" "name-random"