Select columns based on multiple conditions on column namesHow to make a great R reproducible exampleHow to sort a dataframe by multiple column(s)Drop data frame columns by nameChanging column names of a data frameFlexible subseting data.table in Rconditional random matching from one data frame into another data frameDouble match in rHow to produce a binary column in R from an existing numerical column?Summing in R with multiple conditionsCreating new column based on row values of multiple data subsetting conditionsCounting the rows in a data frame that satisfy some criteria and grouping them by the unique values in the first column of the data frame
Digital Bananas
Could the Queen overturn the UK Supreme Court ruling regarding prorogation of Parliament?
The answer is a girl's name (my future granddaughter) - can anyone help?
Do jackscrews suffer from blowdown?
Looking for circuit board material that can be dissolved
Using RECURSIVE in Virtual Layer
Did Joe Biden "stop a prosecution" into his son in Ukraine? And did he brag about stopping the prosecution?
Avoiding dust scattering when you drill
How is this situation not a checkmate?
What did the Federation give the Prophets in exchange for access to the wormhole in DS9?
Is "weekend warrior" derogatory?
Notation clarity question for a conglomerate of accidentals
Is spot metering just an EV compensation?
What is the point of impeaching Trump?
Why the first octet of a MAC address always end with a binary 0?
French license plates
Re-entering the UK after overstaying in 2008
PhD Length: are shorter PhD degrees (from different countries) valued differently in other counter countries where PhD Is a longer process?
The difference of Prime in Solve doesn't work
What is the difference between increasing volume and increasing gain?
How to level a picture frame hung on a single nail?
How to say "respectively" in German when listing (enumerating) things
What are one's options when facing religious discrimination at the airport?
Did the Soviet army intentionally send troops (e.g. penal battalions) running over minefields?
Select columns based on multiple conditions on column names
How to make a great R reproducible exampleHow to sort a dataframe by multiple column(s)Drop data frame columns by nameChanging column names of a data frameFlexible subseting data.table in Rconditional random matching from one data frame into another data frameDouble match in rHow to produce a binary column in R from an existing numerical column?Summing in R with multiple conditionsCreating new column based on row values of multiple data subsetting conditionsCounting the rows in a data frame that satisfy some criteria and grouping them by the unique values in the first column of the data frame
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
I'm trying to sum over columns containing certain characters, and i don't know how to achieve it by using grep-.
I want to sum the following columns. they all have "Under 1.30" and "Under 5 years only" in the names:
"Under 1.30: - Married-couple family: - With related children of the householder under 18 years: - Under 5 years only"
"Under 1.30: - Other family: - Male householder, no wife present: - With related children of the householder under 18 years: - Under 5 years only"
"Under 1.30: - Other family: - Female householder, no husband present: - With related children of the householder under 18 years: - Under 5 years only"
I tried the following code, but it returns more columns in addition to the 3 shown above.
names(B17022[,grep("^Under 1.30.[Under 5 years only]", names(B17022))])
For instance, it returns too:
"Under 1.30: - Married-couple family:"
r grep
add a comment
|
I'm trying to sum over columns containing certain characters, and i don't know how to achieve it by using grep-.
I want to sum the following columns. they all have "Under 1.30" and "Under 5 years only" in the names:
"Under 1.30: - Married-couple family: - With related children of the householder under 18 years: - Under 5 years only"
"Under 1.30: - Other family: - Male householder, no wife present: - With related children of the householder under 18 years: - Under 5 years only"
"Under 1.30: - Other family: - Female householder, no husband present: - With related children of the householder under 18 years: - Under 5 years only"
I tried the following code, but it returns more columns in addition to the 3 shown above.
names(B17022[,grep("^Under 1.30.[Under 5 years only]", names(B17022))])
For instance, it returns too:
"Under 1.30: - Married-couple family:"
r grep
1
Please provide some or all ofB17022
in a plain text format to make this question reproducible.
– neilfws
Mar 28 at 21:22
add a comment
|
I'm trying to sum over columns containing certain characters, and i don't know how to achieve it by using grep-.
I want to sum the following columns. they all have "Under 1.30" and "Under 5 years only" in the names:
"Under 1.30: - Married-couple family: - With related children of the householder under 18 years: - Under 5 years only"
"Under 1.30: - Other family: - Male householder, no wife present: - With related children of the householder under 18 years: - Under 5 years only"
"Under 1.30: - Other family: - Female householder, no husband present: - With related children of the householder under 18 years: - Under 5 years only"
I tried the following code, but it returns more columns in addition to the 3 shown above.
names(B17022[,grep("^Under 1.30.[Under 5 years only]", names(B17022))])
For instance, it returns too:
"Under 1.30: - Married-couple family:"
r grep
I'm trying to sum over columns containing certain characters, and i don't know how to achieve it by using grep-.
I want to sum the following columns. they all have "Under 1.30" and "Under 5 years only" in the names:
"Under 1.30: - Married-couple family: - With related children of the householder under 18 years: - Under 5 years only"
"Under 1.30: - Other family: - Male householder, no wife present: - With related children of the householder under 18 years: - Under 5 years only"
"Under 1.30: - Other family: - Female householder, no husband present: - With related children of the householder under 18 years: - Under 5 years only"
I tried the following code, but it returns more columns in addition to the 3 shown above.
names(B17022[,grep("^Under 1.30.[Under 5 years only]", names(B17022))])
For instance, it returns too:
"Under 1.30: - Married-couple family:"
r grep
r grep
edited Mar 28 at 21:25
Eric Wang
asked Mar 28 at 21:18
Eric WangEric Wang
181 silver badge8 bronze badges
181 silver badge8 bronze badges
1
Please provide some or all ofB17022
in a plain text format to make this question reproducible.
– neilfws
Mar 28 at 21:22
add a comment
|
1
Please provide some or all ofB17022
in a plain text format to make this question reproducible.
– neilfws
Mar 28 at 21:22
1
1
Please provide some or all of
B17022
in a plain text format to make this question reproducible.– neilfws
Mar 28 at 21:22
Please provide some or all of
B17022
in a plain text format to make this question reproducible.– neilfws
Mar 28 at 21:22
add a comment
|
2 Answers
2
active
oldest
votes
How about:
names(B17022[grep("^Under.*Under 5 years only", names(B17022))])
edit: explanation
The .*
matches zero or more of any character except newline. So it will basically match anything in between the two "Under"s.
It worked, thank you so much. could you explain what * does here?
– Eric Wang
Mar 28 at 21:29
add a comment
|
You could use grepl, which gives you a vector with TRUE or FALSE, what makes it easy to be applied for multiple conditions.
names(B17022)[grepl("Under 1.30", names(B17022)) &
grepl("Under 5 years only", names(B17022))]
Data used:
B17022 <- data.frame(matrix(rnorm(3), ncol= 3))
names(B17022) <- c("Under 1.30: - Married-couple family: - With related children of the householder under 18 years: - Under 5 years only", "Under 1.30: - Other family: - Male. householder, no wife present: - With related children of the householder under 18 years: - Under 5 years only", "Under 1.30: - Other family: - Female householder, no husband present: - With related children of the householder under 18 years: - Under 5 years only")
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/4.0/"u003ecc by-sa 4.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%2f55406984%2fselect-columns-based-on-multiple-conditions-on-column-names%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
How about:
names(B17022[grep("^Under.*Under 5 years only", names(B17022))])
edit: explanation
The .*
matches zero or more of any character except newline. So it will basically match anything in between the two "Under"s.
It worked, thank you so much. could you explain what * does here?
– Eric Wang
Mar 28 at 21:29
add a comment
|
How about:
names(B17022[grep("^Under.*Under 5 years only", names(B17022))])
edit: explanation
The .*
matches zero or more of any character except newline. So it will basically match anything in between the two "Under"s.
It worked, thank you so much. could you explain what * does here?
– Eric Wang
Mar 28 at 21:29
add a comment
|
How about:
names(B17022[grep("^Under.*Under 5 years only", names(B17022))])
edit: explanation
The .*
matches zero or more of any character except newline. So it will basically match anything in between the two "Under"s.
How about:
names(B17022[grep("^Under.*Under 5 years only", names(B17022))])
edit: explanation
The .*
matches zero or more of any character except newline. So it will basically match anything in between the two "Under"s.
edited Mar 28 at 21:35
answered Mar 28 at 21:24
Leo BrueggemanLeo Brueggeman
3531 silver badge4 bronze badges
3531 silver badge4 bronze badges
It worked, thank you so much. could you explain what * does here?
– Eric Wang
Mar 28 at 21:29
add a comment
|
It worked, thank you so much. could you explain what * does here?
– Eric Wang
Mar 28 at 21:29
It worked, thank you so much. could you explain what * does here?
– Eric Wang
Mar 28 at 21:29
It worked, thank you so much. could you explain what * does here?
– Eric Wang
Mar 28 at 21:29
add a comment
|
You could use grepl, which gives you a vector with TRUE or FALSE, what makes it easy to be applied for multiple conditions.
names(B17022)[grepl("Under 1.30", names(B17022)) &
grepl("Under 5 years only", names(B17022))]
Data used:
B17022 <- data.frame(matrix(rnorm(3), ncol= 3))
names(B17022) <- c("Under 1.30: - Married-couple family: - With related children of the householder under 18 years: - Under 5 years only", "Under 1.30: - Other family: - Male. householder, no wife present: - With related children of the householder under 18 years: - Under 5 years only", "Under 1.30: - Other family: - Female householder, no husband present: - With related children of the householder under 18 years: - Under 5 years only")
add a comment
|
You could use grepl, which gives you a vector with TRUE or FALSE, what makes it easy to be applied for multiple conditions.
names(B17022)[grepl("Under 1.30", names(B17022)) &
grepl("Under 5 years only", names(B17022))]
Data used:
B17022 <- data.frame(matrix(rnorm(3), ncol= 3))
names(B17022) <- c("Under 1.30: - Married-couple family: - With related children of the householder under 18 years: - Under 5 years only", "Under 1.30: - Other family: - Male. householder, no wife present: - With related children of the householder under 18 years: - Under 5 years only", "Under 1.30: - Other family: - Female householder, no husband present: - With related children of the householder under 18 years: - Under 5 years only")
add a comment
|
You could use grepl, which gives you a vector with TRUE or FALSE, what makes it easy to be applied for multiple conditions.
names(B17022)[grepl("Under 1.30", names(B17022)) &
grepl("Under 5 years only", names(B17022))]
Data used:
B17022 <- data.frame(matrix(rnorm(3), ncol= 3))
names(B17022) <- c("Under 1.30: - Married-couple family: - With related children of the householder under 18 years: - Under 5 years only", "Under 1.30: - Other family: - Male. householder, no wife present: - With related children of the householder under 18 years: - Under 5 years only", "Under 1.30: - Other family: - Female householder, no husband present: - With related children of the householder under 18 years: - Under 5 years only")
You could use grepl, which gives you a vector with TRUE or FALSE, what makes it easy to be applied for multiple conditions.
names(B17022)[grepl("Under 1.30", names(B17022)) &
grepl("Under 5 years only", names(B17022))]
Data used:
B17022 <- data.frame(matrix(rnorm(3), ncol= 3))
names(B17022) <- c("Under 1.30: - Married-couple family: - With related children of the householder under 18 years: - Under 5 years only", "Under 1.30: - Other family: - Male. householder, no wife present: - With related children of the householder under 18 years: - Under 5 years only", "Under 1.30: - Other family: - Female householder, no husband present: - With related children of the householder under 18 years: - Under 5 years only")
answered Mar 28 at 21:43
user9992957
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%2f55406984%2fselect-columns-based-on-multiple-conditions-on-column-names%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 provide some or all of
B17022
in a plain text format to make this question reproducible.– neilfws
Mar 28 at 21:22