Looks like a pretty simple problem, but I haven't been able to find any solution so far.
Consider the following data frame:
dat <- data.frame(id=LETTERS[1:5],
land.use=c(3,4,9,34,39))
I need to replace the numbers in the land.use column with strings. The problem is: I have distinct strings for the numbers 3, 4 and 34.
However, R insists in replacing 34 with the concatenated strings for 3 and 4.
For example:
dat$land.use <- gsub("3","Bare soil", dat$land.use)
dat$land.use <- gsub("4","Primary Forest", dat$land.use)
dat$land.use <- gsub("9","Secondary Forest", dat$land.use)
dat$land.use <- gsub("34","Wheat", dat$land.use)
dat$land.use <- gsub("39","Soybean", dat$land.use)
> dat
id land.use
1 A Bare soil # This is OK
2 B Primary Forest # This is OK
3 C Secondary Forest # This is OK
4 D Bare soilPrimary Forest # This should be Wheat
5 E Bare soilSecondary Forest # This should be Soybean
What am I doing wrong?