1

I select some specific variables using grep() to do some other calculations that failed. I then created a new variable consisting of variables name and "+", not sum of the value.

# create a df
test <- data.frame(I60_freq_t = 1,
                   I60_freq_man = 1,
                   I60_freq_woman = 1,
                   I60_freq_lo65 = 1,
                   I60_freq_hi65 = 1,
                   I61_freq_t = 1,
                   I61_freq_man = 1,
                   I61_freq_woman = 1,
                   I61_freq_lo65 = 1,
                   I61_freq_hi65 = 1,
                   I62_freq_t = 1,
                   I62_freq_man = 1,
                   I62_freq_woman = 1,
                   I62_freq_lo65 = 1,
                   I62_freq_hi65 = 1
                   )

# extract variables with different end words and use " + " to concatenate
end_with_t <- grep('t$', names(test), value = T) %>% paste(collapse = '+')
end_with_man <- grep('[^a-z]man$', names(test), value = T) %>% paste(collapse = '+')
end_with_woman <- grep('woman$', names(test), value = T) %>% paste(collapse = '+')
end_with_lo65 <- grep('lo65$', names(test), value = T) %>% paste(collapse = '+')
end_with_hi65 <- grep('hi65$', names(test), value = T) %>% paste(collapse = '+')

# sum the value 
test2 <- test %>% mutate(t = end_with_t,
                         man = end_with_man,
                         woman = end_with_woman,
                         lo65 = end_with_lo65,
                         hi65 = end_with_hi65) 
# **** What I want is sum the value not sum the variables names *********

My questions are:

1.How can I revise my code to get what I want?

2.Are there better ways to do this?

Any help will be highly appreciated!!!

3
  • 2
    Take a look at mutate_at and use contains/ends/starts_with or write a function and map it to your data. What is your expected output, does collapse really sum? Commented Jun 19, 2019 at 14:56
  • 1
    @NelsonGon, no paste with collapse = "+" just creates strings with a plus character "+" between elements. Commented Jun 19, 2019 at 15:02
  • @NelsonGon, I want to sum the value. For example , new variable should like this t = 3 Commented Jun 20, 2019 at 2:43

1 Answer 1

2

Here is one idea using map_dfc to loop through the variable names and add them all together using rowSums. ends_with is a way to select the variables based on the end of a string.

library(tidyverse)

variables <- c("_t", "_man", "_woman", "_lo65", "_hi65")

test2 <- map_dfc(variables, ~test %>% 
          select(ends_with(.x)) %>%
          rowSums()) %>%
  setNames(str_remove(variables, fixed("_")))

test2
# A tibble: 1 x 5
      t   man woman  lo65  hi65
  <dbl> <dbl> <dbl> <dbl> <dbl>
1     3     3     3     3     3
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.