0

I am learning R for the first time. not sure how I can assign a "0" or 1 to the data I have. I have a list of states and home prices info data frame. how can I assign 0 or 1 to the states?

states    HOMEPRICE
 CA          200000  
 AZ         1000000
 SD         230000
 IL         30000  

For this data ,CA should show as 1 and everything else as 0

states    HOMEPRICE    newcolumn 
 CA          200000       1
 AZ         1000000       0
 SD         230000        0
 IL         30000         0

appreciate any help

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
Krisha
  • 63
  • 5
  • `df$new_col <- as.integer(df$states == "CA")` – Ronak Shah Jun 12 '20 at 02:31
  • @RonakShah Thanks for your reply, sometimes it is possible to have multiple sates at same time. like df$new_col <- as.integer(df$states == "CA" , "IL") . that is where the problem i am facing. not sure how to handle if i have more than one state. – Krisha Jun 12 '20 at 02:50
  • You did not include that in your question. Please make sure your question is complete and includes what you want to do. For more than one variable you can use `df$new_col <- as.integer(df$states %in% c("CA", "IL"))` – Ronak Shah Jun 12 '20 at 02:59

0 Answers0