简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM