0

I'm very new to R Studio and am trying to write a code to create a reduced dataset of three fields from 70+. Example code to exclude one field I'm using is

education1.reduced <- filter(education, state != "DC")

How do I write this code to just pull 3 specific fields, rather than excluding 67?

camille
  • 16,432
  • 18
  • 38
  • 60
suzyd
  • 1
  • Maybe: `education1.reduced <- filter(education, state == "DC")` – TarJae Jan 03 '22 at 18:36
  • 1
    FYI: RStudio is an IDE for the R programming language. So there is no *RStudio data set*. And by 70+ fields, did you mean 70+ values in a single field (or column) like *state*. – Parfait Jan 03 '22 at 19:00
  • FYI field usually refers to columns, not rows – camille Jan 03 '22 at 19:05

2 Answers2

0
education1.reduced <- filter(education, state %in% c("DC", "some other state", "third value for state"))
camille
  • 16,432
  • 18
  • 38
  • 60
Lyndata
  • 1
  • 1
0

Another approach:

education1.reduced <- education[education$state == "state1" | education$state == "state2" | education$state == "state3", ]

It's unnecessarily verbose but helps clarify exactly what you're trying to do. Shorthand for the state meeting any of the three conditions, separated by | ("or") could be, as Lyndata wrote,

education1.reduced <- education[education$state %in% c("state1", "state2", "state3"), ]

It can be helpful to learn a language, as you seem to be doing with R, by using the more verbose methods instead of the shorthand substitutes, but I hope not to just assume you're already past this method of subsetting by using filter()!

camille
  • 16,432
  • 18
  • 38
  • 60