0

I have a dataframe in R which has a long list of company names. I want to select 4 of these companies. They are apple, microsoft, google, amazon. The column name of the company names is "name"

I am creating a subset with subset(companies, name == c("apple", "microsoft", "google", "amazon")) But this returns 0 rows. Not sure what I am doing wrong.

This however works and returns one row. subset(companies, name == "apple")

vacoder169
  • 17
  • 4

1 Answers1

2

You could try subset(companies, name %in% c("apple", "microsoft", "google", "amazon")).

The %in% operator works like SQL where is will look at each of your options provided c("apple", "microsoft", "google", "amazon") and compare each record to that list of options.

I ran a test sample with just changed to the %in% operator ,and it worked.

Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
Bryan Jenks
  • 161
  • 1
  • 8