I have a dataset of several rows of characters. For reasons I can't get into here, I need to leave these columns as character columns, but need to filter out rows containing letters.
In short, something like this:
A | B | C |
---|---|---|
1 | A | 4 |
2 | 3 | B |
5 | 6 | 7 |
Without converting the columns into numeric columns, I need to filter out the first two rows because they contain text characters.
I tried the following but no luck:
df %>%
filter(str_detect( c(A:C), "[AZ]") != "[AZ]")
CodePudding user response:
library(dplyr)
df %>%
filter(if_all(.fns = function(x) as.numeric(x)))
# A tibble: 1 x 3
A B C
<int> <chr> <chr>
1 5 6 7
CodePudding user response:
If you want to use stringr
and str_detect
you can try:
library(stringr)
library(dplyr)
df %>%
filter(across(A:C, ~!str_detect(., "[A-Z]")))
Or to filter
based on all columns in the data.frame:
df %>%
filter(across(everything(), ~!str_detect(., "[A-Z]")))
Output
A B C
1 5 6 7
CodePudding user response:
In base R you might do df[complete.cases(sapply(df, match, 0:9)),]