-1

I'm fairly new to R, and I'm trying to write a formula for a new column 'w_1yr' (within 1yr), using two other columns in my dataframe: enroll_yr (year of enrollment) and lab_yr (year of lab drawn done). Basically, I'm trying to say that if the lab_yr is within 1 yr of the enroll_yr then w_1yr=1, otherwise, 0.

The following is what I have so far, but still it is not correct.

df$w_1yr <- ifelse(df$lab_yr <= 'enroll_yr', 1, ifelse(df$lab_yr > 'enroll_yr', 0, NA)) (sorry for some reason I can't copy/paste from the console, so had to write it out)

enter image description here

If anyone can help me out, that would be great! Thanks!

  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jan 03 '19 at 21:07
  • `df$lab_yr <= 'enroll_yr'` is testing if the value in `lab_yr` is less than the string `'enroll_yr'`. Maybe you have a column named `enroll_yr` and you want `df$lab_yr <= df$enroll_yr`? (And please edit an example into your question, don't bury it in comments.) – Gregor Thomas Jan 03 '19 at 21:11
  • Providing sample input data and desired output is required for an answer that doesn't involve guessing exactly what you want. This could probably be accomplished something like `abs(df$enroll_yr - df$lab_yr) <= 1` – manotheshark Jan 03 '19 at 21:12
  • @Gregor, thanks for the headsup! I added the example in my original post. I don't want "<=", but don't know exactly how to write out that I want values only 'within 1 yr', so I started off with that function. – Labiba Shere Jan 03 '19 at 21:23

1 Answers1

0

The following solution is assuming that your lab_yr is always going to be after the enroll_yr

obs<-c(1,2,3)
enroll_yr<-c('2011','2011','2015')
lab_yr<-c('2013','2011','2016')
enroll_yr<-as.numeric(enroll_yr)
lab_yr<-as.numeric(lab_yr)


df<-data.frame(obs,enroll_yr,lab_yr)


df$w_1yr<-ifelse(lab_yr-enroll_yr==1|lab_yr-enroll_yr==0,1,0)
df
Nathan123
  • 763
  • 5
  • 18
  • Thank you! This worked, except I had to also use the as.numeric() function because of the error message "non-numeric argument to binary operator" that kept popping up. – Labiba Shere Jan 03 '19 at 23:28
  • @LabibaShere if the `lab_yr` and `enroll_yr` were in a character format an error message would pop up. It would be best to provide a reproducible example with the correct formats when you post a question. Use `str()` to find out what format your data has. I have updated the answer. If this solves your problem, remember to accept it. – Nathan123 Jan 04 '19 at 17:12