Home > Enterprise >  Remove columns when they include specific string in R
Remove columns when they include specific string in R

Time:12-21

I would like to remove those columns that include LA in it. A sample dataset looks like:

testdata <- data.frame(id = c(1,2,3),
                   v1 = c("LA", "C","D"),
                   v2 = c("N","M","LA"),
                   v3 = c("D","E","T"))

> testdata
  id v1 v2 v3
1  1 LA  N  D
2  2  C  M  E
3  3  D LA  T

How can I remove v1 and v2 and get the desired dataset below?

   > testdata
      id   v3
    1  1    D
    2  2    E
    3  3    T

CodePudding user response:

testdata%>%
  select(-which(sapply(., function(x) any(x=="LA"))))

  id v3
1  1  D
2  2  E
3  3  T

CodePudding user response:

Using sapply or vapply you could do:

testdata[vapply(testdata, function(x) !any(grepl("LA", x)), FUN.VALUE = logical(1))]
#>   id v3
#> 1  1  D
#> 2  2  E
#> 3  3  T

testdata[sapply(testdata, function(x) !any(grepl("LA", x)))]
#>   id v3
#> 1  1  D
#> 2  2  E
#> 3  3  T

Or using dplyr:

library(dplyr)

testdata %>%
  select(where(~!any(grepl("LA", .x))))
#>   id v3
#> 1  1  D
#> 2  2  E
#> 3  3  T

CodePudding user response:

Using discard

library(purrr)
discard(testdata, ~ "LA" %in% .x)
  id v3
1  1  D
2  2  E
3  3  T
  • Related