0

Selecting parts of variables Hi all. Maybe some of you may be able to help me.

How do I select parts of a variable?

Specifically, I have a column in a data frame (csv) that corresponds to race. 1 = Caucasian, 2 = African Amer. etc. for a range of 1-9 for the variable.

I was able to separate them individually like this:

black <- df[df[, "race"] == 2,]
white <- df[df[, "race"] == 1,]
hisp <- df[df[, "race"] == 6,]

but I want a new dataframe that includes all three of these segments. Or write a code that goes into my raw dataframe and selects just those three.

I unsuccessfully tried

races_used <- c(df[df[, "race"] == 1,],df[df[, "race"] == 2,],df[df[, "race"] == 6,])

and

race2 <- filter(df, [df$race == [1,2,6]])

Any help would be appreciated.

Cettt
  • 11,460
  • 7
  • 35
  • 58
  • I'm not exactly sure what you are trying to achieve. What have tried so far, what is the structure of "the variable", how the inputs are structured nor a representation of what you are expecting to get as output. Are you able to supply the code that you have currently got, indicate which bits are not working (along with any errors) and a representation of the output that you are trying to achieve? Please refer to [How do I ask a good question](https://stackoverflow.com/help/how-to-ask) – GMc May 10 '19 at 05:49
  • Hi Mark, you seem to be unfamiliar with R syntax. I suggest that you try a tutorial before you go further. Try https://r4ds.had.co.nz/ – Rohit May 10 '19 at 05:56
  • What an incredible resource! Thank you so much. – Mark Carroll May 10 '19 at 16:55

1 Answers1

0

You can combine them like this using base R:

df[df$race %in%  c(1, 2, 6) ,]
subset(df, race %in% c(1, 2, 6))

Or using dplyr:

df %>%
  filter(race %in% c(1, 2, 6))

the %in% will return any row where the race is either 1, 2 or 6.

Alternatively you can use the | (or) operand, but it's a bit more lengthy (e.g. race==1 | race==2 | race==6 as the filter condition.

morgan121
  • 2,213
  • 1
  • 15
  • 33