Create a new variable in data frame depending on two other variables












0















I have a large data frame and want to create a new variable which depends on two other variables.



Here is a short example:



v1 <- rep(c(1:5),each=3)
v2 <- c('X','A','Y','X','Y','B','X','Y','C','X','Y','C','X','Y','A')

dat <- data.frame(v1,v2)

#create a new var which contains either A,B, or C depending on what is found in v2


#desired output
v3 <- rep(c('A','B','C','C','A'),each=3)
data.frame(v1,v2,v3)


Any ideas on how to do this with a short code?



I tried this, but it's far from the solution. Too many missings. :(



dat$v3[dat$v2 %in% c('A','B','C')] <- dat$v2[dat$v2 %in% c('A','B','C')]









share|improve this question

























  • Something like with(dat, ave(v2, v1, FUN = function(i) tail(i, 1))) should do it given that the order is always as shown

    – Sotos
    Nov 23 '18 at 10:49













  • As far as I can see v3 only depends on one variable. The rule appears to be 1 -> A, 2 -> B and so on.

    – AkselA
    Nov 23 '18 at 10:50











  • It is more complicated. I will add another line ;)

    – SDahm
    Nov 23 '18 at 10:54











  • Can you also add your attempt that failed please?

    – Sotos
    Nov 23 '18 at 11:00






  • 1





    dplyr's case_when() or ifelse() statements fit perfectly here. However, since you did not provide the exact conditions, it's hard to write an example.

    – Ben T.
    Nov 23 '18 at 11:07
















0















I have a large data frame and want to create a new variable which depends on two other variables.



Here is a short example:



v1 <- rep(c(1:5),each=3)
v2 <- c('X','A','Y','X','Y','B','X','Y','C','X','Y','C','X','Y','A')

dat <- data.frame(v1,v2)

#create a new var which contains either A,B, or C depending on what is found in v2


#desired output
v3 <- rep(c('A','B','C','C','A'),each=3)
data.frame(v1,v2,v3)


Any ideas on how to do this with a short code?



I tried this, but it's far from the solution. Too many missings. :(



dat$v3[dat$v2 %in% c('A','B','C')] <- dat$v2[dat$v2 %in% c('A','B','C')]









share|improve this question

























  • Something like with(dat, ave(v2, v1, FUN = function(i) tail(i, 1))) should do it given that the order is always as shown

    – Sotos
    Nov 23 '18 at 10:49













  • As far as I can see v3 only depends on one variable. The rule appears to be 1 -> A, 2 -> B and so on.

    – AkselA
    Nov 23 '18 at 10:50











  • It is more complicated. I will add another line ;)

    – SDahm
    Nov 23 '18 at 10:54











  • Can you also add your attempt that failed please?

    – Sotos
    Nov 23 '18 at 11:00






  • 1





    dplyr's case_when() or ifelse() statements fit perfectly here. However, since you did not provide the exact conditions, it's hard to write an example.

    – Ben T.
    Nov 23 '18 at 11:07














0












0








0








I have a large data frame and want to create a new variable which depends on two other variables.



Here is a short example:



v1 <- rep(c(1:5),each=3)
v2 <- c('X','A','Y','X','Y','B','X','Y','C','X','Y','C','X','Y','A')

dat <- data.frame(v1,v2)

#create a new var which contains either A,B, or C depending on what is found in v2


#desired output
v3 <- rep(c('A','B','C','C','A'),each=3)
data.frame(v1,v2,v3)


Any ideas on how to do this with a short code?



I tried this, but it's far from the solution. Too many missings. :(



dat$v3[dat$v2 %in% c('A','B','C')] <- dat$v2[dat$v2 %in% c('A','B','C')]









share|improve this question
















I have a large data frame and want to create a new variable which depends on two other variables.



Here is a short example:



v1 <- rep(c(1:5),each=3)
v2 <- c('X','A','Y','X','Y','B','X','Y','C','X','Y','C','X','Y','A')

dat <- data.frame(v1,v2)

#create a new var which contains either A,B, or C depending on what is found in v2


#desired output
v3 <- rep(c('A','B','C','C','A'),each=3)
data.frame(v1,v2,v3)


Any ideas on how to do this with a short code?



I tried this, but it's far from the solution. Too many missings. :(



dat$v3[dat$v2 %in% c('A','B','C')] <- dat$v2[dat$v2 %in% c('A','B','C')]






r






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 11:06







SDahm

















asked Nov 23 '18 at 10:45









SDahmSDahm

2221516




2221516













  • Something like with(dat, ave(v2, v1, FUN = function(i) tail(i, 1))) should do it given that the order is always as shown

    – Sotos
    Nov 23 '18 at 10:49













  • As far as I can see v3 only depends on one variable. The rule appears to be 1 -> A, 2 -> B and so on.

    – AkselA
    Nov 23 '18 at 10:50











  • It is more complicated. I will add another line ;)

    – SDahm
    Nov 23 '18 at 10:54











  • Can you also add your attempt that failed please?

    – Sotos
    Nov 23 '18 at 11:00






  • 1





    dplyr's case_when() or ifelse() statements fit perfectly here. However, since you did not provide the exact conditions, it's hard to write an example.

    – Ben T.
    Nov 23 '18 at 11:07



















  • Something like with(dat, ave(v2, v1, FUN = function(i) tail(i, 1))) should do it given that the order is always as shown

    – Sotos
    Nov 23 '18 at 10:49













  • As far as I can see v3 only depends on one variable. The rule appears to be 1 -> A, 2 -> B and so on.

    – AkselA
    Nov 23 '18 at 10:50











  • It is more complicated. I will add another line ;)

    – SDahm
    Nov 23 '18 at 10:54











  • Can you also add your attempt that failed please?

    – Sotos
    Nov 23 '18 at 11:00






  • 1





    dplyr's case_when() or ifelse() statements fit perfectly here. However, since you did not provide the exact conditions, it's hard to write an example.

    – Ben T.
    Nov 23 '18 at 11:07

















Something like with(dat, ave(v2, v1, FUN = function(i) tail(i, 1))) should do it given that the order is always as shown

– Sotos
Nov 23 '18 at 10:49







Something like with(dat, ave(v2, v1, FUN = function(i) tail(i, 1))) should do it given that the order is always as shown

– Sotos
Nov 23 '18 at 10:49















As far as I can see v3 only depends on one variable. The rule appears to be 1 -> A, 2 -> B and so on.

– AkselA
Nov 23 '18 at 10:50





As far as I can see v3 only depends on one variable. The rule appears to be 1 -> A, 2 -> B and so on.

– AkselA
Nov 23 '18 at 10:50













It is more complicated. I will add another line ;)

– SDahm
Nov 23 '18 at 10:54





It is more complicated. I will add another line ;)

– SDahm
Nov 23 '18 at 10:54













Can you also add your attempt that failed please?

– Sotos
Nov 23 '18 at 11:00





Can you also add your attempt that failed please?

– Sotos
Nov 23 '18 at 11:00




1




1





dplyr's case_when() or ifelse() statements fit perfectly here. However, since you did not provide the exact conditions, it's hard to write an example.

– Ben T.
Nov 23 '18 at 11:07





dplyr's case_when() or ifelse() statements fit perfectly here. However, since you did not provide the exact conditions, it's hard to write an example.

– Ben T.
Nov 23 '18 at 11:07












1 Answer
1






active

oldest

votes


















2














library(tidyverse)
dat %>% group_by(v1) %>% mutate(v3 = intersect(v2, c("A", "B", "C")))
# A tibble: 15 x 3
# Groups: v1 [5]
# v1 v2 v3
# <int> <fct> <chr>
# 1 1 X A
# 2 1 A A
# 3 1 Y A
# 4 2 X B
# 5 2 Y B
# 6 2 B B
# 7 3 X C
# 8 3 Y C
# 9 3 C C
# 10 4 X C
# 11 4 Y C
# 12 4 C C
# 13 5 X A
# 14 5 Y A
# 15 5 A A


This is assuming that only one of A, B, C can appear in a group given by v1.






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%2f53445186%2fcreate-a-new-variable-in-data-frame-depending-on-two-other-variables%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    library(tidyverse)
    dat %>% group_by(v1) %>% mutate(v3 = intersect(v2, c("A", "B", "C")))
    # A tibble: 15 x 3
    # Groups: v1 [5]
    # v1 v2 v3
    # <int> <fct> <chr>
    # 1 1 X A
    # 2 1 A A
    # 3 1 Y A
    # 4 2 X B
    # 5 2 Y B
    # 6 2 B B
    # 7 3 X C
    # 8 3 Y C
    # 9 3 C C
    # 10 4 X C
    # 11 4 Y C
    # 12 4 C C
    # 13 5 X A
    # 14 5 Y A
    # 15 5 A A


    This is assuming that only one of A, B, C can appear in a group given by v1.






    share|improve this answer




























      2














      library(tidyverse)
      dat %>% group_by(v1) %>% mutate(v3 = intersect(v2, c("A", "B", "C")))
      # A tibble: 15 x 3
      # Groups: v1 [5]
      # v1 v2 v3
      # <int> <fct> <chr>
      # 1 1 X A
      # 2 1 A A
      # 3 1 Y A
      # 4 2 X B
      # 5 2 Y B
      # 6 2 B B
      # 7 3 X C
      # 8 3 Y C
      # 9 3 C C
      # 10 4 X C
      # 11 4 Y C
      # 12 4 C C
      # 13 5 X A
      # 14 5 Y A
      # 15 5 A A


      This is assuming that only one of A, B, C can appear in a group given by v1.






      share|improve this answer


























        2












        2








        2







        library(tidyverse)
        dat %>% group_by(v1) %>% mutate(v3 = intersect(v2, c("A", "B", "C")))
        # A tibble: 15 x 3
        # Groups: v1 [5]
        # v1 v2 v3
        # <int> <fct> <chr>
        # 1 1 X A
        # 2 1 A A
        # 3 1 Y A
        # 4 2 X B
        # 5 2 Y B
        # 6 2 B B
        # 7 3 X C
        # 8 3 Y C
        # 9 3 C C
        # 10 4 X C
        # 11 4 Y C
        # 12 4 C C
        # 13 5 X A
        # 14 5 Y A
        # 15 5 A A


        This is assuming that only one of A, B, C can appear in a group given by v1.






        share|improve this answer













        library(tidyverse)
        dat %>% group_by(v1) %>% mutate(v3 = intersect(v2, c("A", "B", "C")))
        # A tibble: 15 x 3
        # Groups: v1 [5]
        # v1 v2 v3
        # <int> <fct> <chr>
        # 1 1 X A
        # 2 1 A A
        # 3 1 Y A
        # 4 2 X B
        # 5 2 Y B
        # 6 2 B B
        # 7 3 X C
        # 8 3 Y C
        # 9 3 C C
        # 10 4 X C
        # 11 4 Y C
        # 12 4 C C
        # 13 5 X A
        # 14 5 Y A
        # 15 5 A A


        This is assuming that only one of A, B, C can appear in a group given by v1.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 11:09









        Julius VainoraJulius Vainora

        38.3k76786




        38.3k76786
































            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%2f53445186%2fcreate-a-new-variable-in-data-frame-depending-on-two-other-variables%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







            這個網誌中的熱門文章

            Hercules Kyvelos

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud