Creating one variable from a list of variables in R? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!R dplyr/tidyr: “mutate” new columns with data from other observationsFunction for Tidy chisq.test Output for Visualizing or Filtering P-ValuesShiny: Create reactive filter using different variables.Create a Table with Alternating Total Rows Followed by Sub-Rows Using Dplyr and TidyverseUsing switch statement within dplyr's mutateConditional Recoding - Using a Vector of Columns within Mutate_at Together with If_else and Dplyr::RecodeCreating and using new variables in function in R: NSE programing error in the tidyversedplyr mutate-ifelse combination not creating correct conditional variableTidyverse — integrating mutate select and case when to likert scalesCan I create a new numerical variable using dplyr and <= and >= operators to subset values from an existing vector?

Why did Europeans not widely domesticate foxes?

How is an IPA symbol that lacks a name (e.g. ɲ) called?

What is the evidence that custom checks in Northern Ireland are going to result in violence?

Can gravitational waves pass through a black hole?

When speaking, how do you change your mind mid-sentence?

Coin Game with infinite paradox

Combining list in a Cartesian product format with addition operation?

/bin/ls sorts differently than just ls

Why is ArcGIS Pro not symbolizing my entire range of values?

Why do C and C++ allow the expression (int) + 4*5?

"Destructive power" carried by a B-52?

Why do people think Winterfell crypts is the safest place for women, children & old people?

Is my guitar’s action too high?

Why does my GNOME settings mention "Moto C Plus"?

What is the ongoing value of the Kanban board to the developers as opposed to management

Short story about an alien named Ushtu(?) coming from a future Earth, when ours was destroyed by a nuclear explosion

How to mute a string and play another at the same time

What helicopter has the most rotor blades?

Reflections in a Square

A German immigrant ancestor has a "Registration Affidavit of Alien Enemy" on file. What does that mean exactly?

Why did Israel vote against lifting the American embargo on Cuba?

Weaponising the Grasp-at-a-Distance spell

Marquee sign letters

How do I overlay a PNG over two videos (one video overlays another) in one command using FFmpeg?



Creating one variable from a list of variables in R?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!R dplyr/tidyr: “mutate” new columns with data from other observationsFunction for Tidy chisq.test Output for Visualizing or Filtering P-ValuesShiny: Create reactive filter using different variables.Create a Table with Alternating Total Rows Followed by Sub-Rows Using Dplyr and TidyverseUsing switch statement within dplyr's mutateConditional Recoding - Using a Vector of Columns within Mutate_at Together with If_else and Dplyr::RecodeCreating and using new variables in function in R: NSE programing error in the tidyversedplyr mutate-ifelse combination not creating correct conditional variableTidyverse — integrating mutate select and case when to likert scalesCan I create a new numerical variable using dplyr and <= and >= operators to subset values from an existing vector?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








6















I have a sequence of variables in a dataframe (over 100) and I would like to create an indicator variable for if particular text patterns are present in any of the variables. Below is an example with three variables. One solution I've found is using tidyr::unite() followed by dplyr::mutate(), but I'm interested in a solution where I do not have to unite the variables.



c1<-c("T1", "X1", "T6", "R5")
c2<-c("R4", "C6", "C7", "X3")
c3<-c("C5", "C2", "X4", "T2")

df<-data.frame(c1, c2, c3)

c1 c2 c3
1 T1 R4 C5
2 X1 C6 C2
3 T6 C7 X4
4 R5 X3 T2

code.vec<-c("T1", "T2", "T3", "T4") #Text patterns of interest
code_regex<-paste(code.vec, collapse="|")

new<-df %>%
unite(all_c, c1:c3, remove=FALSE) %>%
mutate(indicator=if_else(grepl(code_regex, all_c), 1, 0)) %>%
select(-(all_c))

c1 c2 c3 indicator
1 T1 R4 C5 1
2 X1 C6 C2 0
3 T6 C7 X4 0
4 R5 X3 T2 1


Above is an example that produces the desired result, however I feel as if there should be a way of doing this in tidyverse without having to unite the variables. This is something that SAS handles very easily using an ARRAY statement and a DO loop, and I'm hoping R has a good way of handling this.



The real dataframe has many additional variables besides from the "c" fields to search, so a solution that involves searching every column would require subsetting the dataframe to first only contain the variables I want to search, and then joining the data back with the other variables.










share|improve this question
























  • You said you don't want to use unite, but it's worth noting that passing the argument remove = FALSE has unite create a column of the united variables leaving the others intact. Might be convenient in this case.

    – camille
    5 hours ago











  • Yes, it is convenient. And it does work. I just feel like there may be a simpler approach I'm missing that doesn't need to create a united variable.

    – patward5656
    5 hours ago

















6















I have a sequence of variables in a dataframe (over 100) and I would like to create an indicator variable for if particular text patterns are present in any of the variables. Below is an example with three variables. One solution I've found is using tidyr::unite() followed by dplyr::mutate(), but I'm interested in a solution where I do not have to unite the variables.



c1<-c("T1", "X1", "T6", "R5")
c2<-c("R4", "C6", "C7", "X3")
c3<-c("C5", "C2", "X4", "T2")

df<-data.frame(c1, c2, c3)

c1 c2 c3
1 T1 R4 C5
2 X1 C6 C2
3 T6 C7 X4
4 R5 X3 T2

code.vec<-c("T1", "T2", "T3", "T4") #Text patterns of interest
code_regex<-paste(code.vec, collapse="|")

new<-df %>%
unite(all_c, c1:c3, remove=FALSE) %>%
mutate(indicator=if_else(grepl(code_regex, all_c), 1, 0)) %>%
select(-(all_c))

c1 c2 c3 indicator
1 T1 R4 C5 1
2 X1 C6 C2 0
3 T6 C7 X4 0
4 R5 X3 T2 1


Above is an example that produces the desired result, however I feel as if there should be a way of doing this in tidyverse without having to unite the variables. This is something that SAS handles very easily using an ARRAY statement and a DO loop, and I'm hoping R has a good way of handling this.



The real dataframe has many additional variables besides from the "c" fields to search, so a solution that involves searching every column would require subsetting the dataframe to first only contain the variables I want to search, and then joining the data back with the other variables.










share|improve this question
























  • You said you don't want to use unite, but it's worth noting that passing the argument remove = FALSE has unite create a column of the united variables leaving the others intact. Might be convenient in this case.

    – camille
    5 hours ago











  • Yes, it is convenient. And it does work. I just feel like there may be a simpler approach I'm missing that doesn't need to create a united variable.

    – patward5656
    5 hours ago













6












6








6








I have a sequence of variables in a dataframe (over 100) and I would like to create an indicator variable for if particular text patterns are present in any of the variables. Below is an example with three variables. One solution I've found is using tidyr::unite() followed by dplyr::mutate(), but I'm interested in a solution where I do not have to unite the variables.



c1<-c("T1", "X1", "T6", "R5")
c2<-c("R4", "C6", "C7", "X3")
c3<-c("C5", "C2", "X4", "T2")

df<-data.frame(c1, c2, c3)

c1 c2 c3
1 T1 R4 C5
2 X1 C6 C2
3 T6 C7 X4
4 R5 X3 T2

code.vec<-c("T1", "T2", "T3", "T4") #Text patterns of interest
code_regex<-paste(code.vec, collapse="|")

new<-df %>%
unite(all_c, c1:c3, remove=FALSE) %>%
mutate(indicator=if_else(grepl(code_regex, all_c), 1, 0)) %>%
select(-(all_c))

c1 c2 c3 indicator
1 T1 R4 C5 1
2 X1 C6 C2 0
3 T6 C7 X4 0
4 R5 X3 T2 1


Above is an example that produces the desired result, however I feel as if there should be a way of doing this in tidyverse without having to unite the variables. This is something that SAS handles very easily using an ARRAY statement and a DO loop, and I'm hoping R has a good way of handling this.



The real dataframe has many additional variables besides from the "c" fields to search, so a solution that involves searching every column would require subsetting the dataframe to first only contain the variables I want to search, and then joining the data back with the other variables.










share|improve this question
















I have a sequence of variables in a dataframe (over 100) and I would like to create an indicator variable for if particular text patterns are present in any of the variables. Below is an example with three variables. One solution I've found is using tidyr::unite() followed by dplyr::mutate(), but I'm interested in a solution where I do not have to unite the variables.



c1<-c("T1", "X1", "T6", "R5")
c2<-c("R4", "C6", "C7", "X3")
c3<-c("C5", "C2", "X4", "T2")

df<-data.frame(c1, c2, c3)

c1 c2 c3
1 T1 R4 C5
2 X1 C6 C2
3 T6 C7 X4
4 R5 X3 T2

code.vec<-c("T1", "T2", "T3", "T4") #Text patterns of interest
code_regex<-paste(code.vec, collapse="|")

new<-df %>%
unite(all_c, c1:c3, remove=FALSE) %>%
mutate(indicator=if_else(grepl(code_regex, all_c), 1, 0)) %>%
select(-(all_c))

c1 c2 c3 indicator
1 T1 R4 C5 1
2 X1 C6 C2 0
3 T6 C7 X4 0
4 R5 X3 T2 1


Above is an example that produces the desired result, however I feel as if there should be a way of doing this in tidyverse without having to unite the variables. This is something that SAS handles very easily using an ARRAY statement and a DO loop, and I'm hoping R has a good way of handling this.



The real dataframe has many additional variables besides from the "c" fields to search, so a solution that involves searching every column would require subsetting the dataframe to first only contain the variables I want to search, and then joining the data back with the other variables.







r dplyr tidyverse mutate






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 5 hours ago







patward5656

















asked 5 hours ago









patward5656patward5656

425




425












  • You said you don't want to use unite, but it's worth noting that passing the argument remove = FALSE has unite create a column of the united variables leaving the others intact. Might be convenient in this case.

    – camille
    5 hours ago











  • Yes, it is convenient. And it does work. I just feel like there may be a simpler approach I'm missing that doesn't need to create a united variable.

    – patward5656
    5 hours ago

















  • You said you don't want to use unite, but it's worth noting that passing the argument remove = FALSE has unite create a column of the united variables leaving the others intact. Might be convenient in this case.

    – camille
    5 hours ago











  • Yes, it is convenient. And it does work. I just feel like there may be a simpler approach I'm missing that doesn't need to create a united variable.

    – patward5656
    5 hours ago
















You said you don't want to use unite, but it's worth noting that passing the argument remove = FALSE has unite create a column of the united variables leaving the others intact. Might be convenient in this case.

– camille
5 hours ago





You said you don't want to use unite, but it's worth noting that passing the argument remove = FALSE has unite create a column of the united variables leaving the others intact. Might be convenient in this case.

– camille
5 hours ago













Yes, it is convenient. And it does work. I just feel like there may be a simpler approach I'm missing that doesn't need to create a united variable.

– patward5656
5 hours ago





Yes, it is convenient. And it does work. I just feel like there may be a simpler approach I'm missing that doesn't need to create a united variable.

– patward5656
5 hours ago












3 Answers
3






active

oldest

votes


















3














We can use tidyverse



library(tidyverse)
df %>%
mutate_all(str_detect, pattern = code_regex) %>%
reduce(`+`) %>%
mutate(df, indicator = .)
# c1 c2 c3 indicator
#1 T1 R4 C5 1
#2 X1 C6 C2 0
#3 T6 C7 X4 0
#4 R5 X3 T2 1



Or using base R



Reduce(`+`, lapply(df, grepl, pattern = code_regex))
#[1] 1 0 0 1





share|improve this answer























  • This tidyverse solution seems to only work in the scenario where all of the columns are being searched. I have other variables in my real dataset, and when using it for that the output is all NA. Does this have something to do with the reduce function?

    – patward5656
    4 hours ago











  • @patward5656 That is an easy fix. df %>% mutate_at(vars(starts_with("c")), str_detect, pattern = code_regex) %>% reduce("+") %>% mutate(df, indicator = .)

    – akrun
    4 hours ago












  • c1<-c("T1", "X1", "T6", "R5") c2<-c("R4", "C6", "C7", "X3") c3<-c("C5", "C2", "X4", "T2") z1<-c("C5", "C2", "X4", "T2") df<-data.frame(c1, c2, c3, z1) df %>% mutate_at(vars(starts_with("c")), str_detect, pattern = code_regex) %>% reduce(+) %>% mutate(df, indicator = .) c1 c2 c3 z1 indicator 1 T1 R4 C5 C5 NA 2 X1 C6 C2 C2 NA 3 T6 C7 X4 X4 NA 4 R5 X3 T2 T2 NA Warning message: In Ops.factor(.x, .y) : ‘+’ not meaningful for factors This produced NAs, it seems.

    – patward5656
    4 hours ago







  • 1





    @patward5656 I would use transmute_at instead of mutate_at df %>% transmute_at(vars(starts_with("c")), str_detect, pattern = code_regex) %>% reduce(+)

    – akrun
    4 hours ago







  • 1





    Thanks. I believe transmute_at() solves it perfectly.

    – patward5656
    4 hours ago



















6














Using base R, we can use sapply and use grepl to find pattern in every column and assign 1 to rows where there is more than 0 matches.



df$indicator <- as.integer(rowSums(sapply(df, grepl, pattern = code_regex)) > 0)

df
# c1 c2 c3 indicator
#1 T1 R4 C5 1
#2 X1 C6 C2 0
#3 T6 C7 X4 0
#4 R5 X3 T2 1


If there are few other columns and we are interested to apply it only for columns which start with "c" we can use grep to filter them.



cols <- grep("^c", names(df))
as.integer(rowSums(sapply(df[cols], grepl, pattern = code_regex)) > 0)



Using dplyr we can do



library(dplyr)

df$indicator <- as.integer(df %>%
mutate_at(vars(c1:c3), ~grepl(code_regex, .)) %>%
rowSums() > 0)





share|improve this answer

























  • This is a good solution, but in the real data there are additional variables that I do not want to pattern search, so this would require me to index the dataframe to include only the columns I want to search first. Will edit my original post to include this information.

    – patward5656
    5 hours ago











  • The purr solution looks like what I was looking for--one line of code that doesn't involve uniting the variables.

    – patward5656
    5 hours ago











  • @patward5656 I think the purrr solution would not give you the expected output. I changed it to use mutate_at which should work on range of columns. Moreover, you can use column numbers directly in cols for sapply ., say columns 3:5 or 1:3 to find pattern in those column.

    – Ronak Shah
    5 hours ago



















1














Base R with apply



apply(df[cols], 1, function(x) sum(grepl(code_regex, x)))
# [1] 1 0 0 1





share|improve this answer























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



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55795925%2fcreating-one-variable-from-a-list-of-variables-in-r%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    We can use tidyverse



    library(tidyverse)
    df %>%
    mutate_all(str_detect, pattern = code_regex) %>%
    reduce(`+`) %>%
    mutate(df, indicator = .)
    # c1 c2 c3 indicator
    #1 T1 R4 C5 1
    #2 X1 C6 C2 0
    #3 T6 C7 X4 0
    #4 R5 X3 T2 1



    Or using base R



    Reduce(`+`, lapply(df, grepl, pattern = code_regex))
    #[1] 1 0 0 1





    share|improve this answer























    • This tidyverse solution seems to only work in the scenario where all of the columns are being searched. I have other variables in my real dataset, and when using it for that the output is all NA. Does this have something to do with the reduce function?

      – patward5656
      4 hours ago











    • @patward5656 That is an easy fix. df %>% mutate_at(vars(starts_with("c")), str_detect, pattern = code_regex) %>% reduce("+") %>% mutate(df, indicator = .)

      – akrun
      4 hours ago












    • c1<-c("T1", "X1", "T6", "R5") c2<-c("R4", "C6", "C7", "X3") c3<-c("C5", "C2", "X4", "T2") z1<-c("C5", "C2", "X4", "T2") df<-data.frame(c1, c2, c3, z1) df %>% mutate_at(vars(starts_with("c")), str_detect, pattern = code_regex) %>% reduce(+) %>% mutate(df, indicator = .) c1 c2 c3 z1 indicator 1 T1 R4 C5 C5 NA 2 X1 C6 C2 C2 NA 3 T6 C7 X4 X4 NA 4 R5 X3 T2 T2 NA Warning message: In Ops.factor(.x, .y) : ‘+’ not meaningful for factors This produced NAs, it seems.

      – patward5656
      4 hours ago







    • 1





      @patward5656 I would use transmute_at instead of mutate_at df %>% transmute_at(vars(starts_with("c")), str_detect, pattern = code_regex) %>% reduce(+)

      – akrun
      4 hours ago







    • 1





      Thanks. I believe transmute_at() solves it perfectly.

      – patward5656
      4 hours ago
















    3














    We can use tidyverse



    library(tidyverse)
    df %>%
    mutate_all(str_detect, pattern = code_regex) %>%
    reduce(`+`) %>%
    mutate(df, indicator = .)
    # c1 c2 c3 indicator
    #1 T1 R4 C5 1
    #2 X1 C6 C2 0
    #3 T6 C7 X4 0
    #4 R5 X3 T2 1



    Or using base R



    Reduce(`+`, lapply(df, grepl, pattern = code_regex))
    #[1] 1 0 0 1





    share|improve this answer























    • This tidyverse solution seems to only work in the scenario where all of the columns are being searched. I have other variables in my real dataset, and when using it for that the output is all NA. Does this have something to do with the reduce function?

      – patward5656
      4 hours ago











    • @patward5656 That is an easy fix. df %>% mutate_at(vars(starts_with("c")), str_detect, pattern = code_regex) %>% reduce("+") %>% mutate(df, indicator = .)

      – akrun
      4 hours ago












    • c1<-c("T1", "X1", "T6", "R5") c2<-c("R4", "C6", "C7", "X3") c3<-c("C5", "C2", "X4", "T2") z1<-c("C5", "C2", "X4", "T2") df<-data.frame(c1, c2, c3, z1) df %>% mutate_at(vars(starts_with("c")), str_detect, pattern = code_regex) %>% reduce(+) %>% mutate(df, indicator = .) c1 c2 c3 z1 indicator 1 T1 R4 C5 C5 NA 2 X1 C6 C2 C2 NA 3 T6 C7 X4 X4 NA 4 R5 X3 T2 T2 NA Warning message: In Ops.factor(.x, .y) : ‘+’ not meaningful for factors This produced NAs, it seems.

      – patward5656
      4 hours ago







    • 1





      @patward5656 I would use transmute_at instead of mutate_at df %>% transmute_at(vars(starts_with("c")), str_detect, pattern = code_regex) %>% reduce(+)

      – akrun
      4 hours ago







    • 1





      Thanks. I believe transmute_at() solves it perfectly.

      – patward5656
      4 hours ago














    3












    3








    3







    We can use tidyverse



    library(tidyverse)
    df %>%
    mutate_all(str_detect, pattern = code_regex) %>%
    reduce(`+`) %>%
    mutate(df, indicator = .)
    # c1 c2 c3 indicator
    #1 T1 R4 C5 1
    #2 X1 C6 C2 0
    #3 T6 C7 X4 0
    #4 R5 X3 T2 1



    Or using base R



    Reduce(`+`, lapply(df, grepl, pattern = code_regex))
    #[1] 1 0 0 1





    share|improve this answer













    We can use tidyverse



    library(tidyverse)
    df %>%
    mutate_all(str_detect, pattern = code_regex) %>%
    reduce(`+`) %>%
    mutate(df, indicator = .)
    # c1 c2 c3 indicator
    #1 T1 R4 C5 1
    #2 X1 C6 C2 0
    #3 T6 C7 X4 0
    #4 R5 X3 T2 1



    Or using base R



    Reduce(`+`, lapply(df, grepl, pattern = code_regex))
    #[1] 1 0 0 1






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 5 hours ago









    akrunakrun

    424k13209287




    424k13209287












    • This tidyverse solution seems to only work in the scenario where all of the columns are being searched. I have other variables in my real dataset, and when using it for that the output is all NA. Does this have something to do with the reduce function?

      – patward5656
      4 hours ago











    • @patward5656 That is an easy fix. df %>% mutate_at(vars(starts_with("c")), str_detect, pattern = code_regex) %>% reduce("+") %>% mutate(df, indicator = .)

      – akrun
      4 hours ago












    • c1<-c("T1", "X1", "T6", "R5") c2<-c("R4", "C6", "C7", "X3") c3<-c("C5", "C2", "X4", "T2") z1<-c("C5", "C2", "X4", "T2") df<-data.frame(c1, c2, c3, z1) df %>% mutate_at(vars(starts_with("c")), str_detect, pattern = code_regex) %>% reduce(+) %>% mutate(df, indicator = .) c1 c2 c3 z1 indicator 1 T1 R4 C5 C5 NA 2 X1 C6 C2 C2 NA 3 T6 C7 X4 X4 NA 4 R5 X3 T2 T2 NA Warning message: In Ops.factor(.x, .y) : ‘+’ not meaningful for factors This produced NAs, it seems.

      – patward5656
      4 hours ago







    • 1





      @patward5656 I would use transmute_at instead of mutate_at df %>% transmute_at(vars(starts_with("c")), str_detect, pattern = code_regex) %>% reduce(+)

      – akrun
      4 hours ago







    • 1





      Thanks. I believe transmute_at() solves it perfectly.

      – patward5656
      4 hours ago


















    • This tidyverse solution seems to only work in the scenario where all of the columns are being searched. I have other variables in my real dataset, and when using it for that the output is all NA. Does this have something to do with the reduce function?

      – patward5656
      4 hours ago











    • @patward5656 That is an easy fix. df %>% mutate_at(vars(starts_with("c")), str_detect, pattern = code_regex) %>% reduce("+") %>% mutate(df, indicator = .)

      – akrun
      4 hours ago












    • c1<-c("T1", "X1", "T6", "R5") c2<-c("R4", "C6", "C7", "X3") c3<-c("C5", "C2", "X4", "T2") z1<-c("C5", "C2", "X4", "T2") df<-data.frame(c1, c2, c3, z1) df %>% mutate_at(vars(starts_with("c")), str_detect, pattern = code_regex) %>% reduce(+) %>% mutate(df, indicator = .) c1 c2 c3 z1 indicator 1 T1 R4 C5 C5 NA 2 X1 C6 C2 C2 NA 3 T6 C7 X4 X4 NA 4 R5 X3 T2 T2 NA Warning message: In Ops.factor(.x, .y) : ‘+’ not meaningful for factors This produced NAs, it seems.

      – patward5656
      4 hours ago







    • 1





      @patward5656 I would use transmute_at instead of mutate_at df %>% transmute_at(vars(starts_with("c")), str_detect, pattern = code_regex) %>% reduce(+)

      – akrun
      4 hours ago







    • 1





      Thanks. I believe transmute_at() solves it perfectly.

      – patward5656
      4 hours ago

















    This tidyverse solution seems to only work in the scenario where all of the columns are being searched. I have other variables in my real dataset, and when using it for that the output is all NA. Does this have something to do with the reduce function?

    – patward5656
    4 hours ago





    This tidyverse solution seems to only work in the scenario where all of the columns are being searched. I have other variables in my real dataset, and when using it for that the output is all NA. Does this have something to do with the reduce function?

    – patward5656
    4 hours ago













    @patward5656 That is an easy fix. df %>% mutate_at(vars(starts_with("c")), str_detect, pattern = code_regex) %>% reduce("+") %>% mutate(df, indicator = .)

    – akrun
    4 hours ago






    @patward5656 That is an easy fix. df %>% mutate_at(vars(starts_with("c")), str_detect, pattern = code_regex) %>% reduce("+") %>% mutate(df, indicator = .)

    – akrun
    4 hours ago














    c1<-c("T1", "X1", "T6", "R5") c2<-c("R4", "C6", "C7", "X3") c3<-c("C5", "C2", "X4", "T2") z1<-c("C5", "C2", "X4", "T2") df<-data.frame(c1, c2, c3, z1) df %>% mutate_at(vars(starts_with("c")), str_detect, pattern = code_regex) %>% reduce(+) %>% mutate(df, indicator = .) c1 c2 c3 z1 indicator 1 T1 R4 C5 C5 NA 2 X1 C6 C2 C2 NA 3 T6 C7 X4 X4 NA 4 R5 X3 T2 T2 NA Warning message: In Ops.factor(.x, .y) : ‘+’ not meaningful for factors This produced NAs, it seems.

    – patward5656
    4 hours ago






    c1<-c("T1", "X1", "T6", "R5") c2<-c("R4", "C6", "C7", "X3") c3<-c("C5", "C2", "X4", "T2") z1<-c("C5", "C2", "X4", "T2") df<-data.frame(c1, c2, c3, z1) df %>% mutate_at(vars(starts_with("c")), str_detect, pattern = code_regex) %>% reduce(+) %>% mutate(df, indicator = .) c1 c2 c3 z1 indicator 1 T1 R4 C5 C5 NA 2 X1 C6 C2 C2 NA 3 T6 C7 X4 X4 NA 4 R5 X3 T2 T2 NA Warning message: In Ops.factor(.x, .y) : ‘+’ not meaningful for factors This produced NAs, it seems.

    – patward5656
    4 hours ago





    1




    1





    @patward5656 I would use transmute_at instead of mutate_at df %>% transmute_at(vars(starts_with("c")), str_detect, pattern = code_regex) %>% reduce(+)

    – akrun
    4 hours ago






    @patward5656 I would use transmute_at instead of mutate_at df %>% transmute_at(vars(starts_with("c")), str_detect, pattern = code_regex) %>% reduce(+)

    – akrun
    4 hours ago





    1




    1





    Thanks. I believe transmute_at() solves it perfectly.

    – patward5656
    4 hours ago






    Thanks. I believe transmute_at() solves it perfectly.

    – patward5656
    4 hours ago














    6














    Using base R, we can use sapply and use grepl to find pattern in every column and assign 1 to rows where there is more than 0 matches.



    df$indicator <- as.integer(rowSums(sapply(df, grepl, pattern = code_regex)) > 0)

    df
    # c1 c2 c3 indicator
    #1 T1 R4 C5 1
    #2 X1 C6 C2 0
    #3 T6 C7 X4 0
    #4 R5 X3 T2 1


    If there are few other columns and we are interested to apply it only for columns which start with "c" we can use grep to filter them.



    cols <- grep("^c", names(df))
    as.integer(rowSums(sapply(df[cols], grepl, pattern = code_regex)) > 0)



    Using dplyr we can do



    library(dplyr)

    df$indicator <- as.integer(df %>%
    mutate_at(vars(c1:c3), ~grepl(code_regex, .)) %>%
    rowSums() > 0)





    share|improve this answer

























    • This is a good solution, but in the real data there are additional variables that I do not want to pattern search, so this would require me to index the dataframe to include only the columns I want to search first. Will edit my original post to include this information.

      – patward5656
      5 hours ago











    • The purr solution looks like what I was looking for--one line of code that doesn't involve uniting the variables.

      – patward5656
      5 hours ago











    • @patward5656 I think the purrr solution would not give you the expected output. I changed it to use mutate_at which should work on range of columns. Moreover, you can use column numbers directly in cols for sapply ., say columns 3:5 or 1:3 to find pattern in those column.

      – Ronak Shah
      5 hours ago
















    6














    Using base R, we can use sapply and use grepl to find pattern in every column and assign 1 to rows where there is more than 0 matches.



    df$indicator <- as.integer(rowSums(sapply(df, grepl, pattern = code_regex)) > 0)

    df
    # c1 c2 c3 indicator
    #1 T1 R4 C5 1
    #2 X1 C6 C2 0
    #3 T6 C7 X4 0
    #4 R5 X3 T2 1


    If there are few other columns and we are interested to apply it only for columns which start with "c" we can use grep to filter them.



    cols <- grep("^c", names(df))
    as.integer(rowSums(sapply(df[cols], grepl, pattern = code_regex)) > 0)



    Using dplyr we can do



    library(dplyr)

    df$indicator <- as.integer(df %>%
    mutate_at(vars(c1:c3), ~grepl(code_regex, .)) %>%
    rowSums() > 0)





    share|improve this answer

























    • This is a good solution, but in the real data there are additional variables that I do not want to pattern search, so this would require me to index the dataframe to include only the columns I want to search first. Will edit my original post to include this information.

      – patward5656
      5 hours ago











    • The purr solution looks like what I was looking for--one line of code that doesn't involve uniting the variables.

      – patward5656
      5 hours ago











    • @patward5656 I think the purrr solution would not give you the expected output. I changed it to use mutate_at which should work on range of columns. Moreover, you can use column numbers directly in cols for sapply ., say columns 3:5 or 1:3 to find pattern in those column.

      – Ronak Shah
      5 hours ago














    6












    6








    6







    Using base R, we can use sapply and use grepl to find pattern in every column and assign 1 to rows where there is more than 0 matches.



    df$indicator <- as.integer(rowSums(sapply(df, grepl, pattern = code_regex)) > 0)

    df
    # c1 c2 c3 indicator
    #1 T1 R4 C5 1
    #2 X1 C6 C2 0
    #3 T6 C7 X4 0
    #4 R5 X3 T2 1


    If there are few other columns and we are interested to apply it only for columns which start with "c" we can use grep to filter them.



    cols <- grep("^c", names(df))
    as.integer(rowSums(sapply(df[cols], grepl, pattern = code_regex)) > 0)



    Using dplyr we can do



    library(dplyr)

    df$indicator <- as.integer(df %>%
    mutate_at(vars(c1:c3), ~grepl(code_regex, .)) %>%
    rowSums() > 0)





    share|improve this answer















    Using base R, we can use sapply and use grepl to find pattern in every column and assign 1 to rows where there is more than 0 matches.



    df$indicator <- as.integer(rowSums(sapply(df, grepl, pattern = code_regex)) > 0)

    df
    # c1 c2 c3 indicator
    #1 T1 R4 C5 1
    #2 X1 C6 C2 0
    #3 T6 C7 X4 0
    #4 R5 X3 T2 1


    If there are few other columns and we are interested to apply it only for columns which start with "c" we can use grep to filter them.



    cols <- grep("^c", names(df))
    as.integer(rowSums(sapply(df[cols], grepl, pattern = code_regex)) > 0)



    Using dplyr we can do



    library(dplyr)

    df$indicator <- as.integer(df %>%
    mutate_at(vars(c1:c3), ~grepl(code_regex, .)) %>%
    rowSums() > 0)






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 5 hours ago

























    answered 5 hours ago









    Ronak ShahRonak Shah

    49k104370




    49k104370












    • This is a good solution, but in the real data there are additional variables that I do not want to pattern search, so this would require me to index the dataframe to include only the columns I want to search first. Will edit my original post to include this information.

      – patward5656
      5 hours ago











    • The purr solution looks like what I was looking for--one line of code that doesn't involve uniting the variables.

      – patward5656
      5 hours ago











    • @patward5656 I think the purrr solution would not give you the expected output. I changed it to use mutate_at which should work on range of columns. Moreover, you can use column numbers directly in cols for sapply ., say columns 3:5 or 1:3 to find pattern in those column.

      – Ronak Shah
      5 hours ago


















    • This is a good solution, but in the real data there are additional variables that I do not want to pattern search, so this would require me to index the dataframe to include only the columns I want to search first. Will edit my original post to include this information.

      – patward5656
      5 hours ago











    • The purr solution looks like what I was looking for--one line of code that doesn't involve uniting the variables.

      – patward5656
      5 hours ago











    • @patward5656 I think the purrr solution would not give you the expected output. I changed it to use mutate_at which should work on range of columns. Moreover, you can use column numbers directly in cols for sapply ., say columns 3:5 or 1:3 to find pattern in those column.

      – Ronak Shah
      5 hours ago

















    This is a good solution, but in the real data there are additional variables that I do not want to pattern search, so this would require me to index the dataframe to include only the columns I want to search first. Will edit my original post to include this information.

    – patward5656
    5 hours ago





    This is a good solution, but in the real data there are additional variables that I do not want to pattern search, so this would require me to index the dataframe to include only the columns I want to search first. Will edit my original post to include this information.

    – patward5656
    5 hours ago













    The purr solution looks like what I was looking for--one line of code that doesn't involve uniting the variables.

    – patward5656
    5 hours ago





    The purr solution looks like what I was looking for--one line of code that doesn't involve uniting the variables.

    – patward5656
    5 hours ago













    @patward5656 I think the purrr solution would not give you the expected output. I changed it to use mutate_at which should work on range of columns. Moreover, you can use column numbers directly in cols for sapply ., say columns 3:5 or 1:3 to find pattern in those column.

    – Ronak Shah
    5 hours ago






    @patward5656 I think the purrr solution would not give you the expected output. I changed it to use mutate_at which should work on range of columns. Moreover, you can use column numbers directly in cols for sapply ., say columns 3:5 or 1:3 to find pattern in those column.

    – Ronak Shah
    5 hours ago












    1














    Base R with apply



    apply(df[cols], 1, function(x) sum(grepl(code_regex, x)))
    # [1] 1 0 0 1





    share|improve this answer



























      1














      Base R with apply



      apply(df[cols], 1, function(x) sum(grepl(code_regex, x)))
      # [1] 1 0 0 1





      share|improve this answer

























        1












        1








        1







        Base R with apply



        apply(df[cols], 1, function(x) sum(grepl(code_regex, x)))
        # [1] 1 0 0 1





        share|improve this answer













        Base R with apply



        apply(df[cols], 1, function(x) sum(grepl(code_regex, x)))
        # [1] 1 0 0 1






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 5 hours ago









        nsinghsnsinghs

        1,262621




        1,262621



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55795925%2fcreating-one-variable-from-a-list-of-variables-in-r%23new-answer', 'question_page');

            );

            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







            Popular posts from this blog

            Era Viking Índice Início da Era Viquingue | Cotidiano | Sociedade | Língua | Religião | A arte | As primeiras cidades | As viagens dos viquingues | Viquingues do Oeste e Leste | Fim da Era Viquingue | Fontes históricas | Referências Bibliografia | Ligações externas | Menu de navegação«Sverige då!»«Handel I vikingetid»«O que é Nórdico Antigo»Mito, magia e religião na volsunga saga Um olhar sobre a trajetória mítica do herói sigurd«Bonden var den verklige vikingen»«Vikingatiden»«Vikingatiden»«Vinland»«Guerreiras de Óðinn: As Valkyrjor na Mitologia Viking»1519-9053«Esculpindo símbolos e seres: A arte viking em pedras rúnicas»1679-9313Historia - Tema: VikingarnaAventura e Magia no Mundo das Sagas IslandesasEra Vikinge

            What's the metal clinking sound at the end of credits in Avengers: Endgame?What makes Thanos so strong in Avengers: Endgame?Who is the character that appears at the end of Endgame?What happens to Mjolnir (Thor's hammer) at the end of Endgame?The People's Ages in Avengers: EndgameWhat did Nebula do in Avengers: Endgame?Messing with time in the Avengers: Endgame climaxAvengers: Endgame timelineWhat are the time-travel rules in Avengers Endgame?Why use this song in Avengers: Endgame Opening Logo Sequence?Peggy's age in Avengers Endgame

            Are there legal definitions of ethnicities/races? The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Legal definitions in the United StatesAre there truly legal limits on US interest rates?Are gender identity and sexual orientation federally protected?Why is there an apparent legal bias against digital services?What limits are there to the powers of individual judges in the United States legal system?Are women only scholarships legal under Irish / EU law?Is the term “race” defined by Public Law enacted by Congress of the United StatesIs there a legal definition of race in the US?Neighbors are spying for landlord on Renters is it legal?Are Protected Classes Bi-directional?