Sebastian Zeki
Sebastian Zeki

Reputation: 6874

Recode with a variable number of cases in R

I am creating a function that takes a list of user-specified words and then labels them as a number depending on the order of the number in the list. The user can specify different list lengths.

For example:

myNotableWords<-c("No_IM","IM","LGD","HGD","T1a")

aa<-c("No_IM","IM","No_IM","HGD","T1a","HGD","T1a","IM","LGD")
aa<-data.frame(aa,stringsAsFactors=FALSE)

Intended Output

new<-(1,2,1,4,5,4,5,2,3)

Is there a way of maybe getting the index of the original list and then looking up where the each element of the target list is in that index and replacing it with the index number?

Upvotes: 0

Views: 41

Answers (3)

R Yoda
R Yoda

Reputation: 8770

Why not just use the factor functionality of R?

A "factor data type" stores an integer that references a "level" (= character string) via the index number:

myNotableWords<-c("No_IM","IM","LGD","HGD","T1a")
aa<-c("No_IM","IM","No_IM","HGD","T1a","HGD","T1a","IM","LGD")

aa <- as.integer(factor(aa, myNotableWords, ordered = TRUE))

aa
# [1] 1 2 1 4 5 4 5 2 3

Upvotes: 1

Arturo Sbr
Arturo Sbr

Reputation: 6333

You can do this using data.frame; the syntax shouldn't change. I prefer using data.table though.

library(data.table)
myWords <- c("No_IM","IM","LGD","HGD","T1a")
myIndex <- data.table(keywords = myWords, word_index = seq(1, length(myWords)))

The third line simply adds an index to the vector myWords.

aa <- data.table(keywords = c("No_IM","IM","No_IM","HGD","T1a",
                         "HGD","T1a","IM","LGD"))
aa <- merge(aa, myIndex, by = "keywords", all.x = TRUE)

And now you have a table that shows the keyword and its unique number.

Upvotes: 1

12b345b6b78
12b345b6b78

Reputation: 1015

new <- c()
for (item in aa) {
  new <- c(new, which(myNotableWords == item))
}
print(new)
#[1] 1 2 1 4 5 4 5 2 3

Upvotes: 1

Related Questions