I am trying to replace values from two columns with values from another two columns. This is a rather basic question and has been asked by python users, however I am using R.
I have a df that looks like this (only on a much larger scale [>20,000]):
squirrel_id    locx    locy    dist
6391           17.5    10.0    50.0
6391           17.5    10.0    20.0
6391           17.5    10.0    15.5
8443           20.5    1.0     800
6025           -5.0    -0.5    0.0
I need to, for 63 squirrels, replace their locx and locy values.
I normally replace values with the following code:
library(dplyr)    
df <- df %>%
   mutate(locx = ifelse (squirrel_id=="6391", "12.5", locx),
         locy = ifelse (squirrel_id=="6391", "15.5", locy),
         locx = ifelse (squirrel_id=="8443", "2.5", locx),
         locy = ifelse (squirrel_id=="8443", "80", locy)) #etc for 63 squirrels
Which would give me:
squirrel_id    locx    locy    dist
6391           12.5    10.0    50.0
6391           12.5    10.0    20.0
6391           12.5    10.0    15.5
8443           2.5     80.0    800
6025           -5.0    -0.5    0.0
But this is creating an extra 126 lines of code and I suspect there is a simpler way to do this.
I do have all the new locx and locy values in a separate df, but I do not know how to join the two dataframes by squirrel_id without it messing up the data.
df with the values that need to replace the ones in the old df:
squirrel_id    new_locx    new_locy   
6391           12.5        15.5 
8443           2.5         80
6025           -55.0       0.0
How can I do this more efficiently?
 
     
    