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:" 
oguz ismail
  • 1
  • 16
  • 47
  • 69
Eric Wang
  • 83
  • 1
  • 3
  • 10
  • 1
    Please provide some or all of `B17022` in a plain text format to make this question [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – neilfws Mar 28 '19 at 21:22

2 Answers2

0

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.

Leo Brueggeman
  • 2,241
  • 1
  • 9
  • 5
0

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")