0

I have a dataset with number of positive cases. I want to change the data in each column so if it is greater then 0 it becomes 1 and if it equals 0 it stays zero. enter image description here

Willow
  • 1
  • 1
  • `dat <- ifelse(dat > 0, 1, 0)` – Phil Jul 05 '21 at 04:52
  • Thank you so much! This worked well, but I do have one question. When I use this line of code it also changes my first column which is location strings to ones. Is there anyway to make it so the words stay but the rest of the data becomes 1s and 0s? – Willow Jul 06 '21 at 22:07
  • You can do `newdat <- ifelse(dat > 0, 1, 0)` and then `newdat$var <- dat$var` to replace the transformed variable back to its original state. – Phil Jul 06 '21 at 22:10
  • When I do this it I get an error that the "$" operator is invalid for atomic vectors. I added a picture to the first question of the issue – Willow Jul 06 '21 at 23:13
  • Please do not post a picture of your code. https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example Your last 3 lines in the script file don't make sense because you are applying the same steps in both data frames, and then substituting a variable in one to the other, which achieves nothing given what you did the line above. As for the error your mentioned here, it appears you are getting it for doing something different. – Phil Jul 07 '21 at 02:50

0 Answers0