lundi 25 juin 2018

If else ladder not working in R

I have this in my dataframe after reading and rearranging multiple csv files. Basically I want an if else ladder to refer to the ID column and if it matches a number from the list of concatenates then place a word in a new "group" column

    # of int.   int.     not.int.  ID
1      50      218.41     372.16    1
3      33      134.94     158.17    3

I then made these concatenates to refer to.

veh = as.character(c('1', '5'))
thc1 = as.character(c('2', '6'))
thc2 = as.character(c('3', '7'))
thc3 = as.character(c('4', '8')) 

Then I made an if else ladder to list the corresponding values.

social.dat$group =  if (social.dat$ID == veh) {
     social.dat$group == "veh"
     } else if (social.dat$group == thc1) {
     social.dat$group == "thc1"
     } else if (social.dat$group == thc2) {
     social.dat$group == "thc2"
     } else {
     social.dat$group == "thc3"
     }

However, I then get this warning message.

Warning message:
In if (social.dat$ID == veh) { :
the condition has length > 1 and only the first element will be used

I have looked up this warning message in multiple different variations and have not found anything that really helped. Any help for this would be much appreciated or and alternate options would be good as well. I apologize in advance if there was a solution on stack already if I missed it.

Aucun commentaire:

Enregistrer un commentaire