Filter_at selected columns with multiple str_detect patternsHow to sort a dataframe by multiple column(s)how to make a bar plot for a list of dataframes?how to make a network of user in a dataframe?error “no applicable method for 'regroup' applied to an object of class ”c('integer', 'numeric')“”“minimum count is not zero” error for zero inflated modelMatplot not plotting datasetAdd secondary X-axis with facetsR error: “invalid type (NULL) for variable”R- How to use map() into map()How can I add a regression plot for a multiple regression for certain x values?
Is there really no use for MD5 anymore?
Do I have an "anti-research" personality?
What does ゆーか mean?
Don’t seats that recline flat defeat the purpose of having seatbelts?
How to limit Drive Letters Windows assigns to new removable USB drives
Aliens crash on Earth and go into stasis to wait for technology to fix their ship
What are the characteristics of a typeless programming language?
Check if a string is entirely made of the same substring
"You've called the wrong number" or "You called the wrong number"
Can we say “you can pay when the order gets ready”?
As an international instructor, should I openly talk about my accent?
Why did C use the -> operator instead of reusing the . operator?
Why do games have consumables?
Does a large simulator bay have standard public address announcements?
Pulling the rope with one hand is as heavy as with two hands?
A strange hotel
What happened to Captain America in Endgame?
How much cash can I safely carry into the USA and avoid civil forfeiture?
Why must Chinese maps be obfuscated?
Is the claim "Employers won't employ people with no 'social media presence'" realistic?
On The Origin of Dissonant Chords
Apply MapThread to all but one variable
Alignment of various blocks in tikz
How to pronounce 'c++' in Spanish
Filter_at selected columns with multiple str_detect patterns
How to sort a dataframe by multiple column(s)how to make a bar plot for a list of dataframes?how to make a network of user in a dataframe?error “no applicable method for 'regroup' applied to an object of class ”c('integer', 'numeric')“”“minimum count is not zero” error for zero inflated modelMatplot not plotting datasetAdd secondary X-axis with facetsR error: “invalid type (NULL) for variable”R- How to use map() into map()How can I add a regression plot for a multiple regression for certain x values?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
What i should do should be pretty easy, yet, the newcomer that I am, I spent way too much time on trying to achieve this. With this script I try to filter out ALL observations from a data frame that contain ANY of the mentioned patterns.
The script is:
df1 <- filter_at(df, vars(contains("Pair")),
any_vars(str_detect(., pattern="quinoaquinoa|lupinelupine", negate=TRUE)))
I do not get any error when I run this, however nothing changes and the expressions are not taken out from the dataframe. As i understand these functions i could also place a !
in front of str_detect
instead of the negate=TRUE
, however neither works.
Note, the data frame is actually larger (has columns other than those containing "Pair", and the patterns to filter out will always be different and are retrieved from another data frame.
The data frame looks like:
str(df)
'data.frame': 653 obs. of 6 variables:
$ Pair_1: Factor w/ 7 levels "grasscloverleycamelina",..: 3 7 7 3 3 3 7 6 6 6 ...
$ Pair_2: Factor w/ 20 levels "camelinacamelina",..: 10 6 6 8 8 10 6 8 8 10 ...
$ Pair_3: Factor w/ 20 levels "camelinacamelina",..: 19 20 20 20 19 19 20 20 20 16 ...
$ Pair_4: Factor w/ 23 levels "camelinacamelina",..: 9 8 8 8 9 9 4 1 1 5 ...
$ Pair_5: Factor w/ 20 levels "camelinacamelina",..: 9 12 16 16 13 13 12 12 11 11 ...
$ Pair_6: Factor w/ 20 levels "camelinacamelina",..: 20 13 9 17 20 20 5 7 8 8 ...
dput
dataframe:
structure(list(Pair_1 = structure(c(3L, 7L, 7L, 3L, 3L, 3L), .Label = c("grasscloverleycamelina",
"grasscloverleyquinoa", "lupinecamelina", "lupinegrasscloverley",
"lupinelupine", "lupinequinoa", "lupinespringcereal"), class = "factor"),
Pair_2 = structure(c(10L, 6L, 6L, 8L, 8L, 10L), .Label = c("camelinacamelina",
"camelinagrasscloverley", "camelinalupine", "camelinaquinoa",
"camelinaspringcereal", "grasscloverleycamelina", "grasscloverleygrasscloverley",
"grasscloverleylupine", "grasscloverleyquinoa", "grasscloverleyspringcereal",
"quinoacamelina", "quinoagrasscloverley", "quinoalupine",
"quinoaquinoa", "quinoaspringcereal", "springcerealcamelina",
"springcerealgrasscloverley", "springcereallupine", "springcerealquinoa",
"springcerealspringcereal"), class = "factor"), Pair_3 = structure(c(19L,
20L, 20L, 20L, 19L, 19L), .Label = c("camelinacamelina",
"camelinagrasscloverley", "camelinalupine", "camelinaquinoa",
"camelinaspringcereal", "grasscloverleycamelina", "grasscloverleygrasscloverley",
"grasscloverleylupine", "grasscloverleyquinoa", "grasscloverleyspringcereal",
"quinoacamelina", "quinoagrasscloverley", "quinoalupine",
"quinoaquinoa", "quinoaspringcereal", "springcerealcamelina",
"springcerealgrasscloverley", "springcereallupine", "springcerealquinoa",
"springcerealspringcereal"), class = "factor"), Pair_4 = structure(c(9L,
8L, 8L, 8L, 9L, 9L), .Label = c("camelinacamelina", "camelinagrasscloverley",
"camelinalupine", "camelinaquinoa", "camelinaspringcereal",
"grasscloverleycamelina", "grasscloverleygrasscloverley",
"grasscloverleyquinoa", "grasscloverleyspringcereal", "lupinecamelina",
"lupinegrasscloverley", "lupinelupine", "lupinequinoa", "lupinespringcereal",
"quinoacamelina", "quinoagrasscloverley", "quinoaquinoa",
"quinoaspringcereal", "springcerealcamelina", "springcerealgrasscloverley",
"springcereallupine", "springcerealquinoa", "springcerealspringcereal"
), class = "factor"), Pair_5 = structure(c(9L, 12L, 16L,
16L, 13L, 13L), .Label = c("camelinacamelina", "camelinagrasscloverley",
"camelinaquinoa", "camelinaspringcereal", "grasscloverleycamelina",
"grasscloverleygrasscloverley", "grasscloverleyquinoa", "grasscloverleyspringcereal",
"lupinecamelina", "lupinegrasscloverley", "lupinequinoa",
"lupinespringcereal", "quinoacamelina", "quinoagrasscloverley",
"quinoaquinoa", "quinoaspringcereal", "springcerealcamelina",
"springcerealgrasscloverley", "springcerealquinoa", "springcerealspringcereal"
), class = "factor"), Pair_6 = structure(c(20L, 13L, 9L,
17L, 20L, 20L), .Label = c("camelinacamelina", "camelinagrasscloverley",
"camelinaquinoa", "camelinaspringcereal", "grasscloverleycamelina",
"grasscloverleygrasscloverley", "grasscloverleyquinoa", "grasscloverleyspringcereal",
"lupinecamelina", "lupinegrasscloverley", "lupinequinoa",
"lupinespringcereal", "quinoacamelina", "quinoagrasscloverley",
"quinoaquinoa", "quinoaspringcereal", "springcerealcamelina",
"springcerealgrasscloverley", "springcerealquinoa", "springcerealspringcereal"
), class = "factor")), row.names = c(NA, 6L), class = "data.frame")
r regex filter dplyr
add a comment |
What i should do should be pretty easy, yet, the newcomer that I am, I spent way too much time on trying to achieve this. With this script I try to filter out ALL observations from a data frame that contain ANY of the mentioned patterns.
The script is:
df1 <- filter_at(df, vars(contains("Pair")),
any_vars(str_detect(., pattern="quinoaquinoa|lupinelupine", negate=TRUE)))
I do not get any error when I run this, however nothing changes and the expressions are not taken out from the dataframe. As i understand these functions i could also place a !
in front of str_detect
instead of the negate=TRUE
, however neither works.
Note, the data frame is actually larger (has columns other than those containing "Pair", and the patterns to filter out will always be different and are retrieved from another data frame.
The data frame looks like:
str(df)
'data.frame': 653 obs. of 6 variables:
$ Pair_1: Factor w/ 7 levels "grasscloverleycamelina",..: 3 7 7 3 3 3 7 6 6 6 ...
$ Pair_2: Factor w/ 20 levels "camelinacamelina",..: 10 6 6 8 8 10 6 8 8 10 ...
$ Pair_3: Factor w/ 20 levels "camelinacamelina",..: 19 20 20 20 19 19 20 20 20 16 ...
$ Pair_4: Factor w/ 23 levels "camelinacamelina",..: 9 8 8 8 9 9 4 1 1 5 ...
$ Pair_5: Factor w/ 20 levels "camelinacamelina",..: 9 12 16 16 13 13 12 12 11 11 ...
$ Pair_6: Factor w/ 20 levels "camelinacamelina",..: 20 13 9 17 20 20 5 7 8 8 ...
dput
dataframe:
structure(list(Pair_1 = structure(c(3L, 7L, 7L, 3L, 3L, 3L), .Label = c("grasscloverleycamelina",
"grasscloverleyquinoa", "lupinecamelina", "lupinegrasscloverley",
"lupinelupine", "lupinequinoa", "lupinespringcereal"), class = "factor"),
Pair_2 = structure(c(10L, 6L, 6L, 8L, 8L, 10L), .Label = c("camelinacamelina",
"camelinagrasscloverley", "camelinalupine", "camelinaquinoa",
"camelinaspringcereal", "grasscloverleycamelina", "grasscloverleygrasscloverley",
"grasscloverleylupine", "grasscloverleyquinoa", "grasscloverleyspringcereal",
"quinoacamelina", "quinoagrasscloverley", "quinoalupine",
"quinoaquinoa", "quinoaspringcereal", "springcerealcamelina",
"springcerealgrasscloverley", "springcereallupine", "springcerealquinoa",
"springcerealspringcereal"), class = "factor"), Pair_3 = structure(c(19L,
20L, 20L, 20L, 19L, 19L), .Label = c("camelinacamelina",
"camelinagrasscloverley", "camelinalupine", "camelinaquinoa",
"camelinaspringcereal", "grasscloverleycamelina", "grasscloverleygrasscloverley",
"grasscloverleylupine", "grasscloverleyquinoa", "grasscloverleyspringcereal",
"quinoacamelina", "quinoagrasscloverley", "quinoalupine",
"quinoaquinoa", "quinoaspringcereal", "springcerealcamelina",
"springcerealgrasscloverley", "springcereallupine", "springcerealquinoa",
"springcerealspringcereal"), class = "factor"), Pair_4 = structure(c(9L,
8L, 8L, 8L, 9L, 9L), .Label = c("camelinacamelina", "camelinagrasscloverley",
"camelinalupine", "camelinaquinoa", "camelinaspringcereal",
"grasscloverleycamelina", "grasscloverleygrasscloverley",
"grasscloverleyquinoa", "grasscloverleyspringcereal", "lupinecamelina",
"lupinegrasscloverley", "lupinelupine", "lupinequinoa", "lupinespringcereal",
"quinoacamelina", "quinoagrasscloverley", "quinoaquinoa",
"quinoaspringcereal", "springcerealcamelina", "springcerealgrasscloverley",
"springcereallupine", "springcerealquinoa", "springcerealspringcereal"
), class = "factor"), Pair_5 = structure(c(9L, 12L, 16L,
16L, 13L, 13L), .Label = c("camelinacamelina", "camelinagrasscloverley",
"camelinaquinoa", "camelinaspringcereal", "grasscloverleycamelina",
"grasscloverleygrasscloverley", "grasscloverleyquinoa", "grasscloverleyspringcereal",
"lupinecamelina", "lupinegrasscloverley", "lupinequinoa",
"lupinespringcereal", "quinoacamelina", "quinoagrasscloverley",
"quinoaquinoa", "quinoaspringcereal", "springcerealcamelina",
"springcerealgrasscloverley", "springcerealquinoa", "springcerealspringcereal"
), class = "factor"), Pair_6 = structure(c(20L, 13L, 9L,
17L, 20L, 20L), .Label = c("camelinacamelina", "camelinagrasscloverley",
"camelinaquinoa", "camelinaspringcereal", "grasscloverleycamelina",
"grasscloverleygrasscloverley", "grasscloverleyquinoa", "grasscloverleyspringcereal",
"lupinecamelina", "lupinegrasscloverley", "lupinequinoa",
"lupinespringcereal", "quinoacamelina", "quinoagrasscloverley",
"quinoaquinoa", "quinoaspringcereal", "springcerealcamelina",
"springcerealgrasscloverley", "springcerealquinoa", "springcerealspringcereal"
), class = "factor")), row.names = c(NA, 6L), class = "data.frame")
r regex filter dplyr
1
Please also format inline code for readability, I helped you this time.
– jay.sf
Mar 9 at 9:43
add a comment |
What i should do should be pretty easy, yet, the newcomer that I am, I spent way too much time on trying to achieve this. With this script I try to filter out ALL observations from a data frame that contain ANY of the mentioned patterns.
The script is:
df1 <- filter_at(df, vars(contains("Pair")),
any_vars(str_detect(., pattern="quinoaquinoa|lupinelupine", negate=TRUE)))
I do not get any error when I run this, however nothing changes and the expressions are not taken out from the dataframe. As i understand these functions i could also place a !
in front of str_detect
instead of the negate=TRUE
, however neither works.
Note, the data frame is actually larger (has columns other than those containing "Pair", and the patterns to filter out will always be different and are retrieved from another data frame.
The data frame looks like:
str(df)
'data.frame': 653 obs. of 6 variables:
$ Pair_1: Factor w/ 7 levels "grasscloverleycamelina",..: 3 7 7 3 3 3 7 6 6 6 ...
$ Pair_2: Factor w/ 20 levels "camelinacamelina",..: 10 6 6 8 8 10 6 8 8 10 ...
$ Pair_3: Factor w/ 20 levels "camelinacamelina",..: 19 20 20 20 19 19 20 20 20 16 ...
$ Pair_4: Factor w/ 23 levels "camelinacamelina",..: 9 8 8 8 9 9 4 1 1 5 ...
$ Pair_5: Factor w/ 20 levels "camelinacamelina",..: 9 12 16 16 13 13 12 12 11 11 ...
$ Pair_6: Factor w/ 20 levels "camelinacamelina",..: 20 13 9 17 20 20 5 7 8 8 ...
dput
dataframe:
structure(list(Pair_1 = structure(c(3L, 7L, 7L, 3L, 3L, 3L), .Label = c("grasscloverleycamelina",
"grasscloverleyquinoa", "lupinecamelina", "lupinegrasscloverley",
"lupinelupine", "lupinequinoa", "lupinespringcereal"), class = "factor"),
Pair_2 = structure(c(10L, 6L, 6L, 8L, 8L, 10L), .Label = c("camelinacamelina",
"camelinagrasscloverley", "camelinalupine", "camelinaquinoa",
"camelinaspringcereal", "grasscloverleycamelina", "grasscloverleygrasscloverley",
"grasscloverleylupine", "grasscloverleyquinoa", "grasscloverleyspringcereal",
"quinoacamelina", "quinoagrasscloverley", "quinoalupine",
"quinoaquinoa", "quinoaspringcereal", "springcerealcamelina",
"springcerealgrasscloverley", "springcereallupine", "springcerealquinoa",
"springcerealspringcereal"), class = "factor"), Pair_3 = structure(c(19L,
20L, 20L, 20L, 19L, 19L), .Label = c("camelinacamelina",
"camelinagrasscloverley", "camelinalupine", "camelinaquinoa",
"camelinaspringcereal", "grasscloverleycamelina", "grasscloverleygrasscloverley",
"grasscloverleylupine", "grasscloverleyquinoa", "grasscloverleyspringcereal",
"quinoacamelina", "quinoagrasscloverley", "quinoalupine",
"quinoaquinoa", "quinoaspringcereal", "springcerealcamelina",
"springcerealgrasscloverley", "springcereallupine", "springcerealquinoa",
"springcerealspringcereal"), class = "factor"), Pair_4 = structure(c(9L,
8L, 8L, 8L, 9L, 9L), .Label = c("camelinacamelina", "camelinagrasscloverley",
"camelinalupine", "camelinaquinoa", "camelinaspringcereal",
"grasscloverleycamelina", "grasscloverleygrasscloverley",
"grasscloverleyquinoa", "grasscloverleyspringcereal", "lupinecamelina",
"lupinegrasscloverley", "lupinelupine", "lupinequinoa", "lupinespringcereal",
"quinoacamelina", "quinoagrasscloverley", "quinoaquinoa",
"quinoaspringcereal", "springcerealcamelina", "springcerealgrasscloverley",
"springcereallupine", "springcerealquinoa", "springcerealspringcereal"
), class = "factor"), Pair_5 = structure(c(9L, 12L, 16L,
16L, 13L, 13L), .Label = c("camelinacamelina", "camelinagrasscloverley",
"camelinaquinoa", "camelinaspringcereal", "grasscloverleycamelina",
"grasscloverleygrasscloverley", "grasscloverleyquinoa", "grasscloverleyspringcereal",
"lupinecamelina", "lupinegrasscloverley", "lupinequinoa",
"lupinespringcereal", "quinoacamelina", "quinoagrasscloverley",
"quinoaquinoa", "quinoaspringcereal", "springcerealcamelina",
"springcerealgrasscloverley", "springcerealquinoa", "springcerealspringcereal"
), class = "factor"), Pair_6 = structure(c(20L, 13L, 9L,
17L, 20L, 20L), .Label = c("camelinacamelina", "camelinagrasscloverley",
"camelinaquinoa", "camelinaspringcereal", "grasscloverleycamelina",
"grasscloverleygrasscloverley", "grasscloverleyquinoa", "grasscloverleyspringcereal",
"lupinecamelina", "lupinegrasscloverley", "lupinequinoa",
"lupinespringcereal", "quinoacamelina", "quinoagrasscloverley",
"quinoaquinoa", "quinoaspringcereal", "springcerealcamelina",
"springcerealgrasscloverley", "springcerealquinoa", "springcerealspringcereal"
), class = "factor")), row.names = c(NA, 6L), class = "data.frame")
r regex filter dplyr
What i should do should be pretty easy, yet, the newcomer that I am, I spent way too much time on trying to achieve this. With this script I try to filter out ALL observations from a data frame that contain ANY of the mentioned patterns.
The script is:
df1 <- filter_at(df, vars(contains("Pair")),
any_vars(str_detect(., pattern="quinoaquinoa|lupinelupine", negate=TRUE)))
I do not get any error when I run this, however nothing changes and the expressions are not taken out from the dataframe. As i understand these functions i could also place a !
in front of str_detect
instead of the negate=TRUE
, however neither works.
Note, the data frame is actually larger (has columns other than those containing "Pair", and the patterns to filter out will always be different and are retrieved from another data frame.
The data frame looks like:
str(df)
'data.frame': 653 obs. of 6 variables:
$ Pair_1: Factor w/ 7 levels "grasscloverleycamelina",..: 3 7 7 3 3 3 7 6 6 6 ...
$ Pair_2: Factor w/ 20 levels "camelinacamelina",..: 10 6 6 8 8 10 6 8 8 10 ...
$ Pair_3: Factor w/ 20 levels "camelinacamelina",..: 19 20 20 20 19 19 20 20 20 16 ...
$ Pair_4: Factor w/ 23 levels "camelinacamelina",..: 9 8 8 8 9 9 4 1 1 5 ...
$ Pair_5: Factor w/ 20 levels "camelinacamelina",..: 9 12 16 16 13 13 12 12 11 11 ...
$ Pair_6: Factor w/ 20 levels "camelinacamelina",..: 20 13 9 17 20 20 5 7 8 8 ...
dput
dataframe:
structure(list(Pair_1 = structure(c(3L, 7L, 7L, 3L, 3L, 3L), .Label = c("grasscloverleycamelina",
"grasscloverleyquinoa", "lupinecamelina", "lupinegrasscloverley",
"lupinelupine", "lupinequinoa", "lupinespringcereal"), class = "factor"),
Pair_2 = structure(c(10L, 6L, 6L, 8L, 8L, 10L), .Label = c("camelinacamelina",
"camelinagrasscloverley", "camelinalupine", "camelinaquinoa",
"camelinaspringcereal", "grasscloverleycamelina", "grasscloverleygrasscloverley",
"grasscloverleylupine", "grasscloverleyquinoa", "grasscloverleyspringcereal",
"quinoacamelina", "quinoagrasscloverley", "quinoalupine",
"quinoaquinoa", "quinoaspringcereal", "springcerealcamelina",
"springcerealgrasscloverley", "springcereallupine", "springcerealquinoa",
"springcerealspringcereal"), class = "factor"), Pair_3 = structure(c(19L,
20L, 20L, 20L, 19L, 19L), .Label = c("camelinacamelina",
"camelinagrasscloverley", "camelinalupine", "camelinaquinoa",
"camelinaspringcereal", "grasscloverleycamelina", "grasscloverleygrasscloverley",
"grasscloverleylupine", "grasscloverleyquinoa", "grasscloverleyspringcereal",
"quinoacamelina", "quinoagrasscloverley", "quinoalupine",
"quinoaquinoa", "quinoaspringcereal", "springcerealcamelina",
"springcerealgrasscloverley", "springcereallupine", "springcerealquinoa",
"springcerealspringcereal"), class = "factor"), Pair_4 = structure(c(9L,
8L, 8L, 8L, 9L, 9L), .Label = c("camelinacamelina", "camelinagrasscloverley",
"camelinalupine", "camelinaquinoa", "camelinaspringcereal",
"grasscloverleycamelina", "grasscloverleygrasscloverley",
"grasscloverleyquinoa", "grasscloverleyspringcereal", "lupinecamelina",
"lupinegrasscloverley", "lupinelupine", "lupinequinoa", "lupinespringcereal",
"quinoacamelina", "quinoagrasscloverley", "quinoaquinoa",
"quinoaspringcereal", "springcerealcamelina", "springcerealgrasscloverley",
"springcereallupine", "springcerealquinoa", "springcerealspringcereal"
), class = "factor"), Pair_5 = structure(c(9L, 12L, 16L,
16L, 13L, 13L), .Label = c("camelinacamelina", "camelinagrasscloverley",
"camelinaquinoa", "camelinaspringcereal", "grasscloverleycamelina",
"grasscloverleygrasscloverley", "grasscloverleyquinoa", "grasscloverleyspringcereal",
"lupinecamelina", "lupinegrasscloverley", "lupinequinoa",
"lupinespringcereal", "quinoacamelina", "quinoagrasscloverley",
"quinoaquinoa", "quinoaspringcereal", "springcerealcamelina",
"springcerealgrasscloverley", "springcerealquinoa", "springcerealspringcereal"
), class = "factor"), Pair_6 = structure(c(20L, 13L, 9L,
17L, 20L, 20L), .Label = c("camelinacamelina", "camelinagrasscloverley",
"camelinaquinoa", "camelinaspringcereal", "grasscloverleycamelina",
"grasscloverleygrasscloverley", "grasscloverleyquinoa", "grasscloverleyspringcereal",
"lupinecamelina", "lupinegrasscloverley", "lupinequinoa",
"lupinespringcereal", "quinoacamelina", "quinoagrasscloverley",
"quinoaquinoa", "quinoaspringcereal", "springcerealcamelina",
"springcerealgrasscloverley", "springcerealquinoa", "springcerealspringcereal"
), class = "factor")), row.names = c(NA, 6L), class = "data.frame")
r regex filter dplyr
r regex filter dplyr
edited Mar 13 at 7:42
Wiktor Stribiżew
332k16149232
332k16149232
asked Mar 9 at 9:16
BellaLinBellaLin
33
33
1
Please also format inline code for readability, I helped you this time.
– jay.sf
Mar 9 at 9:43
add a comment |
1
Please also format inline code for readability, I helped you this time.
– jay.sf
Mar 9 at 9:43
1
1
Please also format inline code for readability, I helped you this time.
– jay.sf
Mar 9 at 9:43
Please also format inline code for readability, I helped you this time.
– jay.sf
Mar 9 at 9:43
add a comment |
2 Answers
2
active
oldest
votes
You can loop over column which has "Pair" in the dataframe check if the required pattern in present or not, create a matrix of logical vectors and select rows which have no occurrence of the pattern.
cols <- grep("Pair", names(df))
df[rowSums(sapply(df[cols],function(x) grepl("quinoaquinoa|lupinelupine", x)))== 0, ]
Thanks, it works! Would you mind explaining a bit how this one works?
– BellaLin
Mar 10 at 7:41
@BellaLin Usinggrep
we first find out our columns which has "Pair" in it. Then we loop over those columns usingsapply
and check which of them have the required pattern in it. Just runsapply(df[cols],function(x) grepl("quinoaquinoa|lupinelupine", x))
and you'll get a matrix with TRUE/FALSE value in it indicating whether the pattern is present in it or not. Now we dorowSums
and select only those rows which has no occurrence of the pattern at all in their row. (== 0
).
– Ronak Shah
Mar 10 at 8:25
add a comment |
There is no string containing "quinoaquinoa" or "lupinelupine" in your dataframe. I think the pattern you're using is inccorect. This works : filter_at(df, vars(contains("Pair")), any_vars(str_detect(., pattern = "quinoa|lupine")))
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55075782%2ffilter-at-selected-columns-with-multiple-str-detect-patterns%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can loop over column which has "Pair" in the dataframe check if the required pattern in present or not, create a matrix of logical vectors and select rows which have no occurrence of the pattern.
cols <- grep("Pair", names(df))
df[rowSums(sapply(df[cols],function(x) grepl("quinoaquinoa|lupinelupine", x)))== 0, ]
Thanks, it works! Would you mind explaining a bit how this one works?
– BellaLin
Mar 10 at 7:41
@BellaLin Usinggrep
we first find out our columns which has "Pair" in it. Then we loop over those columns usingsapply
and check which of them have the required pattern in it. Just runsapply(df[cols],function(x) grepl("quinoaquinoa|lupinelupine", x))
and you'll get a matrix with TRUE/FALSE value in it indicating whether the pattern is present in it or not. Now we dorowSums
and select only those rows which has no occurrence of the pattern at all in their row. (== 0
).
– Ronak Shah
Mar 10 at 8:25
add a comment |
You can loop over column which has "Pair" in the dataframe check if the required pattern in present or not, create a matrix of logical vectors and select rows which have no occurrence of the pattern.
cols <- grep("Pair", names(df))
df[rowSums(sapply(df[cols],function(x) grepl("quinoaquinoa|lupinelupine", x)))== 0, ]
Thanks, it works! Would you mind explaining a bit how this one works?
– BellaLin
Mar 10 at 7:41
@BellaLin Usinggrep
we first find out our columns which has "Pair" in it. Then we loop over those columns usingsapply
and check which of them have the required pattern in it. Just runsapply(df[cols],function(x) grepl("quinoaquinoa|lupinelupine", x))
and you'll get a matrix with TRUE/FALSE value in it indicating whether the pattern is present in it or not. Now we dorowSums
and select only those rows which has no occurrence of the pattern at all in their row. (== 0
).
– Ronak Shah
Mar 10 at 8:25
add a comment |
You can loop over column which has "Pair" in the dataframe check if the required pattern in present or not, create a matrix of logical vectors and select rows which have no occurrence of the pattern.
cols <- grep("Pair", names(df))
df[rowSums(sapply(df[cols],function(x) grepl("quinoaquinoa|lupinelupine", x)))== 0, ]
You can loop over column which has "Pair" in the dataframe check if the required pattern in present or not, create a matrix of logical vectors and select rows which have no occurrence of the pattern.
cols <- grep("Pair", names(df))
df[rowSums(sapply(df[cols],function(x) grepl("quinoaquinoa|lupinelupine", x)))== 0, ]
answered Mar 9 at 9:44
Ronak ShahRonak Shah
50k104370
50k104370
Thanks, it works! Would you mind explaining a bit how this one works?
– BellaLin
Mar 10 at 7:41
@BellaLin Usinggrep
we first find out our columns which has "Pair" in it. Then we loop over those columns usingsapply
and check which of them have the required pattern in it. Just runsapply(df[cols],function(x) grepl("quinoaquinoa|lupinelupine", x))
and you'll get a matrix with TRUE/FALSE value in it indicating whether the pattern is present in it or not. Now we dorowSums
and select only those rows which has no occurrence of the pattern at all in their row. (== 0
).
– Ronak Shah
Mar 10 at 8:25
add a comment |
Thanks, it works! Would you mind explaining a bit how this one works?
– BellaLin
Mar 10 at 7:41
@BellaLin Usinggrep
we first find out our columns which has "Pair" in it. Then we loop over those columns usingsapply
and check which of them have the required pattern in it. Just runsapply(df[cols],function(x) grepl("quinoaquinoa|lupinelupine", x))
and you'll get a matrix with TRUE/FALSE value in it indicating whether the pattern is present in it or not. Now we dorowSums
and select only those rows which has no occurrence of the pattern at all in their row. (== 0
).
– Ronak Shah
Mar 10 at 8:25
Thanks, it works! Would you mind explaining a bit how this one works?
– BellaLin
Mar 10 at 7:41
Thanks, it works! Would you mind explaining a bit how this one works?
– BellaLin
Mar 10 at 7:41
@BellaLin Using
grep
we first find out our columns which has "Pair" in it. Then we loop over those columns using sapply
and check which of them have the required pattern in it. Just run sapply(df[cols],function(x) grepl("quinoaquinoa|lupinelupine", x))
and you'll get a matrix with TRUE/FALSE value in it indicating whether the pattern is present in it or not. Now we do rowSums
and select only those rows which has no occurrence of the pattern at all in their row. (== 0
).– Ronak Shah
Mar 10 at 8:25
@BellaLin Using
grep
we first find out our columns which has "Pair" in it. Then we loop over those columns using sapply
and check which of them have the required pattern in it. Just run sapply(df[cols],function(x) grepl("quinoaquinoa|lupinelupine", x))
and you'll get a matrix with TRUE/FALSE value in it indicating whether the pattern is present in it or not. Now we do rowSums
and select only those rows which has no occurrence of the pattern at all in their row. (== 0
).– Ronak Shah
Mar 10 at 8:25
add a comment |
There is no string containing "quinoaquinoa" or "lupinelupine" in your dataframe. I think the pattern you're using is inccorect. This works : filter_at(df, vars(contains("Pair")), any_vars(str_detect(., pattern = "quinoa|lupine")))
add a comment |
There is no string containing "quinoaquinoa" or "lupinelupine" in your dataframe. I think the pattern you're using is inccorect. This works : filter_at(df, vars(contains("Pair")), any_vars(str_detect(., pattern = "quinoa|lupine")))
add a comment |
There is no string containing "quinoaquinoa" or "lupinelupine" in your dataframe. I think the pattern you're using is inccorect. This works : filter_at(df, vars(contains("Pair")), any_vars(str_detect(., pattern = "quinoa|lupine")))
There is no string containing "quinoaquinoa" or "lupinelupine" in your dataframe. I think the pattern you're using is inccorect. This works : filter_at(df, vars(contains("Pair")), any_vars(str_detect(., pattern = "quinoa|lupine")))
answered Mar 9 at 9:39
Ismail MüllerIsmail Müller
1164
1164
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55075782%2ffilter-at-selected-columns-with-multiple-str-detect-patterns%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Please also format inline code for readability, I helped you this time.
– jay.sf
Mar 9 at 9:43