Search code examples
rdataframemultiple-columnsbind

How to select only the rows that have a certain value in one column in R?


I am doing an exploratory analysis of a dataset that includes the amount of games and how much they sold per platform in the last 20 years.

I want to select all the games that were released on a Nintendo platform, what I've done to achieve this is:

dfNintendo <- dfNintendo[dfNintendo$Platform=="GBA", ]

It works to extract only the games that were published on the Nintendo GBA, but I don't know how to extract multiple rows with tags different than GBA at the same time, I've tried with:

dfNintendo <- dfNintendo[dfNintendo$Platform=="GBA" | 
                         dfNintendo$Platform=="Wii" | 
                         dfNintendo$Platform=="WiiU", ]

But it doesn't work, I end up with an empty data.frame.


Solution

  • There are a few ways to do this:

    Base R

    dfNintendo[dfNintendo$Platform %in% c("GBA", "Wii", "WiiU"), ]
    

    or

    subset(dfNintendo, Platform %in% c("GBA", "Wii", "WiiU"))
    

    dplyr package

    dplyr::filter(dfNintendo, Platform %in% c("GBA", "Wii", "WiiU"))
    

    These should do what you want