Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 799
Andrés Martínez Vargas Avatar asked Dec 30 '25 04:12

Andrés Martínez Vargas


1 Answers

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

like image 176
morgan121 Avatar answered Dec 31 '25 18:12

morgan121