Remove Na Values from a Vector

Remove NA values from a vector

Trying ?max, you'll see that it actually has a na.rm = argument, set by default to FALSE. (That's the common default for many other R functions, including sum(), mean(), etc.)

Setting na.rm=TRUE does just what you're asking for:

d <- c(1, 100, NA, 10)
max(d, na.rm=TRUE)

If you do want to remove all of the NAs, use this idiom instead:

d <- d[!is.na(d)]

A final note: Other functions (e.g. table(), lm(), and sort()) have NA-related arguments that use different names (and offer different options). So if NA's cause you problems in a function call, it's worth checking for a built-in solution among the function's arguments. I've found there's usually one already there.

How to remove NA values in vector in R

And even shorter:

v <- na.omit(v)

how to remove NA in character vector in R

Assuming progression_dab is a character vector, like x below, you can do:

x <- c("a", NA, "b")

na.omit(x)
## "a" "b"

or:

theNAs <- is.na(x) # the indices of the NAs
x[!theNAs] # remove theses indices to x
## "a" "b"

How to remove all the NA from a Vector?

Use is.na with vector indexing

x <- c(NA, 3, NA, 5)
x[!is.na(x)]
[1] 3 5

I also refer the honourable gentleman / lady to the excellent R introductory manuals, in particular Section 2.7 Index vectors; selecting and modifying subsets of a data set

modify_at to remove NA values in each element in a list

Creating some data.

library(tidyverse)
mylist <-
list(tibble(a = c(1, 2, NA),
b = c(2, 2, 2)),
tibble(c = rep(1, 5),
d = sample(c(NA, 2), 5, replace = TRUE)))

The .at argument in purrr::modify_at() specifies the list element to modify, not the column within the dataframe nested in the list. purrr::modify() works for your purposes.

modify(mylist, drop_na)
#> [[1]]
#> # A tibble: 2 x 2
#> a b
#> <dbl> <dbl>
#> 1 1 2
#> 2 2 2
#>
#> [[2]]
#> # A tibble: 4 x 2
#> c d
#> <dbl> <dbl>
#> 1 1 2
#> 2 1 2
#> 3 1 2
#> 4 1 2

purrr::map() also works. Since your input and output are both list objects, map() is sufficient here, while modify() would be preferred if your input is of another class than a regular list and you want to conserve that class attribute for the output.

map(mylist, drop_na)
#> [[1]]
#> # A tibble: 2 x 2
#> a b
#> <dbl> <dbl>
#> 1 1 2
#> 2 2 2
#>
#> [[2]]
#> # A tibble: 4 x 2
#> c d
#> <dbl> <dbl>
#> 1 1 2
#> 2 1 2
#> 3 1 2
#> 4 1 2

base R

lapply(mylist, na.omit)
#> [[1]]
#> # A tibble: 2 x 2
#> a b
#> <dbl> <dbl>
#> 1 1 2
#> 2 2 2
#>
#> [[2]]
#> # A tibble: 4 x 2
#> c d
#> <dbl> <dbl>
#> 1 1 2
#> 2 1 2
#> 3 1 2
#> 4 1 2

Remove NA elements from column of vectors using dplyr

We can specify a lambda function to do this

library(dplyr)
library(purrr)
df1 <- df %>%
mutate(col_comb = pmap(select(., starts_with('col')),
~ as.character(na.omit(c(...)))))

-output

df1$col_comb
#[[1]]
#[1] "a" "b" "c"

#[[2]]
#[1] "a" "c"

#[[3]]
#[1] "a"

#[[4]]
#character(0)

Or another option is c_across with rowwise

df %>% 
rowwise %>%
mutate(col_comb = list(na.omit(c_across(-id)))) %>%
ungroup


Related Topics



Leave a reply



Submit