-1

I have a dataframe called SCWB from 2001. The variable YR_IMM captures the year of immigration for each individual (=observation).

1) I would like to delete the "don't know" (=9998) and "refuse" (=9999) observations. How should I go about this? I tried the dplyr package, but I can't figure out how to work an "continuous" variable (immigration years go from 1920 to 2000)

2) I would like to recode YR_IMM into "years spent in the US". Would this code be correct?

YRSinUS <- transform(SCWB, YR_IMM = 2001 - YR_IMM)

1 Answers1

1

Delete (filter out) "don't know" and "refuse" and recode YR_IMM:

library(dplyr)

SCWB %>% 
  filter(YR_IMM != 9998 & YR_IMM != 9999) %>% 
  mutate(YR_IMM = 2001 - YR_IMM)

Ahorn
  • 3,686
  • 1
  • 10
  • 17
  • 1
    Think that should be an `&` not an `|` – David T May 25 '20 at 16:29
  • 1
    Steffi, Please edit your post to make a Minimal Reproducible Example. See https://stackoverflow.com/questions/5963269 . You _must_ include some sample data; 4-10 lines is usually enough. In this case, use `dput(head(SCWB, 6))`. Include a sample of what you want the output to look like. Don't include images or links to outside data source. We want to help you, but you've got to work with us. – David T May 25 '20 at 16:30
  • David thanks for the notice - I copied the code and output I get is at least 5 pages long. What part of the output exactly is needed? – Steffi Garrett May 25 '20 at 16:49
  • Also Dominik, I tried to run the code, but I get this notification "Error in is.character(subclass) : argument "subclass" is missing, with no default" – Steffi Garrett May 25 '20 at 16:56