I have a list of non-unique names in a CSV file. I want to assign an id to each unique name in this list. So if the data is something like this:
Stacy
Adam
Donald
Adam
Greg
Donald
I want the output to be something like this:
1 Stacy
2 Adam
3 Donald
2 Adam
5 Greg
3 Donald
I have tried using the match() function, but that does not seem to be working. Any help would be greatly appreciated.
data <- read.csv(file = "mock_data.csv", header = TRUE)
uniqueFirstNames <- unique(data["first_name"])
paste('Number of unique first names: ', nrow(uniqueFirstNames))
indices <- match(x = uniqueFirstNames, table = data["first_name"])
indices above currently gives me a NA
df <- data.frame(names = c("Stacy", "Adam","Donald","Adam","Greg","Donald"))
##using factor
df$flag <- with(df, as.numeric(factor(names,levels=unique(names) )))
##Using match
df$flag2 <- with(df, match(names, unique(names)))
names flag flag2
Stacy 1 1
Adam 2 2
Donald 3 3
Adam 2 2
Greg 4 4
Donald 3 3
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.