Hello I wrote the following function "REPLACEE" to look for a specific value of a dataframe and change it with the input : Nom.
In the following replex when i run REPLACEE() on my piece of data it return the correct rows but leave the value unchanged when it should be replaced by "xyz". When I do it out of the function the same code work. How can I fix this ?
test<-rbind(c(2 , "AISNE" , 284 , "Erloy" , "MARTINACHE", "Francis")
,c(2 , "AISNE" , 284 , "Erloy" , "LIEZ", "Régine")
,c(2 , "AISNE" , 284 , "Erloy" , "LAMBERT", "Eric")
,c(2 , "AISNE" , 284 , "Erloy" , "PRETHOMME", "Raymond")
,c(2 , "AISNE" , 284 , "Erloy" , "MEURA", "Jacques")
,c(2 , "AISNE" , 284 , "Erloy" , "CAZE", "Raphaël"))
colnames(test)<-c("Code département" , "Département" , "Code commune" , "Libellé de la commune", "Nom"
,"Prénom" )
REPLACEE <- function(x,y,z,Nom) {
res<- which(test[,1]==x&
test[,3]==y&
test[,5]==z)
print(res)
test[res,5]<-Nom
}
# in function it doesn't work
test[4,5]
#> Nom
#> "PRETHOMME"
REPLACEE(2,284,"PRETHOMME","xyz")
#> [1] 4
test[4,5]
#> Nom
#> "PRETHOMME"
# out of the function it work
test[4,5]<-"xyz"
test[4,5]
#> Nom
#> "xyz"