Reputation: 397
I have a big data frame which one of its columns has name tags of countries:
GBR
GBR
GBR
GBR
GBR
GBR
JPT
FIN
..
I wanna change (in this example i put here) the GBRs (Great Britain) for two name tags regarding more specific info (IRE and ENG, for instance). The first half (first three) I want them to be IRE, and the second half ENG. So, this is the desired output:
IRE
IRE
IRE
ENG
ENG
ENG
JPT
FIN
..
The solutions that i've found so far is to substitute GBR for something, but I want to separate it into two "somethings" (IRE and ENG), not just replace every GBR for the same thing. Any thoughts? Thanks !
Upvotes: 0
Views: 37
Reputation: 4283
you may use the following code if you are willing to use row indexes to do the trick:
# mimicking your data
df <- data.frame(MyColumn = c(rep("GBR", 6), "JPT", "FIN"))
# add levels to factor
levels(df$MyColumn) <- c(levels(df$MyColumn), "IRE", "ENG")
# change by row number
df$MyColumn[1:3] <- "IRE"
df$MyColumn[4:6] <- "ENG"
Upvotes: 1
Reputation: 7153
Here's an example to replace half the "setosa" species as "set1" and the other half as "set2":
dat <- iris
dat$Species <- as.character(dat$Species)
dat[dat$Species=="setosa", "Species"] <- c(rep("set1", 25), rep("set2", 25))
Upvotes: 1