Hi,
I am very new to R so I end up with errors all the time, apologies in advance if this seems like a very basic question.
I have a variable 'patient_yearofbirth' in my dataset. To calculate the age of patients, I thought I could simply subtract 'patient_yearofbirth' from the current year '2020' . I used the following codes:
AZH$current_year<-c('2020') ##created current year variable so that age could be calculated
AZH_with_Age<-AZH
AZH_with_Age$Age<-AZH$current_year-AZH$patient_yearofbirth ##subtracted the two years to get age
But I am getting the following error: "Error in AZH$current_year - AZH$patient_yearofbirth : non-numeric argument to binary operator"
How should I go about it? What does this error imply?
The reason you are getting the error, is because the patient_yearofbirth is of class character (i.e. text) and if you try to subtract text from text or even a number, R has no clue what to do, hence the error. By converting the column to the correct class (in this case integer), you can solve this problem.