Subset a dataframe by multiple factor levels [duplicate]












16
















This question already has an answer here:




  • Select rows from a data frame based on values in a vector

    3 answers




How can I avoid using a loop to subset a dataframe based on multiple factor levels?



In the following example my desired output is a dataframe. The dataframe should contain the rows of the original dataframe where the value in "Code" equals one of the values in "selected".



Working example:



#sample data
Code<-c("A","B","C","D","C","D","A","A")
Value<-c(1, 2, 3, 4, 1, 2, 3, 4)
data<-data.frame(cbind(Code, Value))

selected<-c("A","B") #want rows that contain A and B

#Begin subsetting
result<-data[which(data$Code==selected[1]),]
s1<-2
while(s1<length(selected)+1)
{
result<-rbind(result,data[which(data$Code==selected[s1]),])
s1<-s1+1
}


This is a toy example of a much larger dataset, so "selected" may contain a great number of elements and the data a great number of rows. Therefore I would like to avoid the loop.










share|improve this question















marked as duplicate by Jaap r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
May 1 '17 at 18:00


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















    16
















    This question already has an answer here:




    • Select rows from a data frame based on values in a vector

      3 answers




    How can I avoid using a loop to subset a dataframe based on multiple factor levels?



    In the following example my desired output is a dataframe. The dataframe should contain the rows of the original dataframe where the value in "Code" equals one of the values in "selected".



    Working example:



    #sample data
    Code<-c("A","B","C","D","C","D","A","A")
    Value<-c(1, 2, 3, 4, 1, 2, 3, 4)
    data<-data.frame(cbind(Code, Value))

    selected<-c("A","B") #want rows that contain A and B

    #Begin subsetting
    result<-data[which(data$Code==selected[1]),]
    s1<-2
    while(s1<length(selected)+1)
    {
    result<-rbind(result,data[which(data$Code==selected[s1]),])
    s1<-s1+1
    }


    This is a toy example of a much larger dataset, so "selected" may contain a great number of elements and the data a great number of rows. Therefore I would like to avoid the loop.










    share|improve this question















    marked as duplicate by Jaap r
    Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    May 1 '17 at 18:00


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















      16












      16








      16


      6







      This question already has an answer here:




      • Select rows from a data frame based on values in a vector

        3 answers




      How can I avoid using a loop to subset a dataframe based on multiple factor levels?



      In the following example my desired output is a dataframe. The dataframe should contain the rows of the original dataframe where the value in "Code" equals one of the values in "selected".



      Working example:



      #sample data
      Code<-c("A","B","C","D","C","D","A","A")
      Value<-c(1, 2, 3, 4, 1, 2, 3, 4)
      data<-data.frame(cbind(Code, Value))

      selected<-c("A","B") #want rows that contain A and B

      #Begin subsetting
      result<-data[which(data$Code==selected[1]),]
      s1<-2
      while(s1<length(selected)+1)
      {
      result<-rbind(result,data[which(data$Code==selected[s1]),])
      s1<-s1+1
      }


      This is a toy example of a much larger dataset, so "selected" may contain a great number of elements and the data a great number of rows. Therefore I would like to avoid the loop.










      share|improve this question

















      This question already has an answer here:




      • Select rows from a data frame based on values in a vector

        3 answers




      How can I avoid using a loop to subset a dataframe based on multiple factor levels?



      In the following example my desired output is a dataframe. The dataframe should contain the rows of the original dataframe where the value in "Code" equals one of the values in "selected".



      Working example:



      #sample data
      Code<-c("A","B","C","D","C","D","A","A")
      Value<-c(1, 2, 3, 4, 1, 2, 3, 4)
      data<-data.frame(cbind(Code, Value))

      selected<-c("A","B") #want rows that contain A and B

      #Begin subsetting
      result<-data[which(data$Code==selected[1]),]
      s1<-2
      while(s1<length(selected)+1)
      {
      result<-rbind(result,data[which(data$Code==selected[s1]),])
      s1<-s1+1
      }


      This is a toy example of a much larger dataset, so "selected" may contain a great number of elements and the data a great number of rows. Therefore I would like to avoid the loop.





      This question already has an answer here:




      • Select rows from a data frame based on values in a vector

        3 answers








      r subset






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 20 '13 at 22:24







      Walter

















      asked Oct 20 '13 at 22:03









      WalterWalter

      2,33421623




      2,33421623




      marked as duplicate by Jaap r
      Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      May 1 '17 at 18:00


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by Jaap r
      Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      May 1 '17 at 18:00


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          3 Answers
          3






          active

          oldest

          votes


















          31














          You can use %in%



            data[data$Code %in% selected,]
          Code Value
          1 A 1
          2 B 2
          7 A 3
          8 A 4





          share|improve this answer

































            4














            Try this:



            > data[match(as.character(data$Code), selected, nomatch = FALSE), ]
            Code Value
            1 A 1
            2 B 2
            1.1 A 1
            1.2 A 1





            share|improve this answer

































              3














              Here's another:



              data[data$Code == "A" | data$Code == "B", ]


              It's also worth mentioning that the subsetting factor doesn't have to be part of the data frame if it matches the data frame rows in length and order. In this case we made our data frame from this factor anyway. So,



              data[Code == "A" | Code == "B", ]


              also works, which is one of the really useful things about R.






              share|improve this answer
































                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                31














                You can use %in%



                  data[data$Code %in% selected,]
                Code Value
                1 A 1
                2 B 2
                7 A 3
                8 A 4





                share|improve this answer






























                  31














                  You can use %in%



                    data[data$Code %in% selected,]
                  Code Value
                  1 A 1
                  2 B 2
                  7 A 3
                  8 A 4





                  share|improve this answer




























                    31












                    31








                    31







                    You can use %in%



                      data[data$Code %in% selected,]
                    Code Value
                    1 A 1
                    2 B 2
                    7 A 3
                    8 A 4





                    share|improve this answer















                    You can use %in%



                      data[data$Code %in% selected,]
                    Code Value
                    1 A 1
                    2 B 2
                    7 A 3
                    8 A 4






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Oct 20 '13 at 22:31

























                    answered Oct 20 '13 at 22:11









                    MetricsMetrics

                    11.3k43871




                    11.3k43871

























                        4














                        Try this:



                        > data[match(as.character(data$Code), selected, nomatch = FALSE), ]
                        Code Value
                        1 A 1
                        2 B 2
                        1.1 A 1
                        1.2 A 1





                        share|improve this answer






























                          4














                          Try this:



                          > data[match(as.character(data$Code), selected, nomatch = FALSE), ]
                          Code Value
                          1 A 1
                          2 B 2
                          1.1 A 1
                          1.2 A 1





                          share|improve this answer




























                            4












                            4








                            4







                            Try this:



                            > data[match(as.character(data$Code), selected, nomatch = FALSE), ]
                            Code Value
                            1 A 1
                            2 B 2
                            1.1 A 1
                            1.2 A 1





                            share|improve this answer















                            Try this:



                            > data[match(as.character(data$Code), selected, nomatch = FALSE), ]
                            Code Value
                            1 A 1
                            2 B 2
                            1.1 A 1
                            1.2 A 1






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Oct 20 '13 at 22:38

























                            answered Oct 20 '13 at 22:05









                            Jilber UrbinaJilber Urbina

                            42.2k479113




                            42.2k479113























                                3














                                Here's another:



                                data[data$Code == "A" | data$Code == "B", ]


                                It's also worth mentioning that the subsetting factor doesn't have to be part of the data frame if it matches the data frame rows in length and order. In this case we made our data frame from this factor anyway. So,



                                data[Code == "A" | Code == "B", ]


                                also works, which is one of the really useful things about R.






                                share|improve this answer






























                                  3














                                  Here's another:



                                  data[data$Code == "A" | data$Code == "B", ]


                                  It's also worth mentioning that the subsetting factor doesn't have to be part of the data frame if it matches the data frame rows in length and order. In this case we made our data frame from this factor anyway. So,



                                  data[Code == "A" | Code == "B", ]


                                  also works, which is one of the really useful things about R.






                                  share|improve this answer




























                                    3












                                    3








                                    3







                                    Here's another:



                                    data[data$Code == "A" | data$Code == "B", ]


                                    It's also worth mentioning that the subsetting factor doesn't have to be part of the data frame if it matches the data frame rows in length and order. In this case we made our data frame from this factor anyway. So,



                                    data[Code == "A" | Code == "B", ]


                                    also works, which is one of the really useful things about R.






                                    share|improve this answer















                                    Here's another:



                                    data[data$Code == "A" | data$Code == "B", ]


                                    It's also worth mentioning that the subsetting factor doesn't have to be part of the data frame if it matches the data frame rows in length and order. In this case we made our data frame from this factor anyway. So,



                                    data[Code == "A" | Code == "B", ]


                                    also works, which is one of the really useful things about R.







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Oct 23 '16 at 9:43

























                                    answered Oct 17 '16 at 15:00









                                    JoeJoe

                                    2,9991736




                                    2,9991736















                                        Popular posts from this blog

                                        404 Error Contact Form 7 ajax form submitting

                                        How to know if a Active Directory user can login interactively

                                        Refactoring coordinates for Minecraft Pi buildings written in Python