lundi 2 février 2015

How do I overwrite a column of characters in R with 1s and 0s?

I need to convert one of the columns of my data to binary values. I am trying to insert 0 if there is a blank space and 1 if there is a string of characters.


My data currently looks like this:



Name Code
Bob
Joe f59tuc
Anna
Kylie
Jill hyt618
Ben skt310


And I am trying to convert it to this:



name code
Bob 0
Joe 1
Anna 0
Kylie 0
Jill 1
Ben 1


I tried this if statement, but get errors. I am new to R, and have very little experience with if-statements, while-loops, for-loops, etc -- so there may be some obvious mistakes in the code that I am not thinking of. Any help would be most appreciated.



if (df$code[df$code == ""]) {
df$code[df$code] <- 0
}
else {
df$code[df$code] <- 1
}

Aucun commentaire:

Enregistrer un commentaire