简体   繁体   中英

Combine vector and data.frame matching column values and vector values

I have

vetor <- c(1,2,3)
data <- data.frame(id=c('a', 'b', 'a', 'c', 'a'))

I need a data.frame output that match each vector value to a specific id, resulting:

  id vector1
1  a       1
2  b       2
3  a       1
4  c       3
5  a       1

Here are two approaches I often use for similar situations:

vetor <- c(1,2,3)
key <- data.frame(vetor=vetor, mat=c('a', 'b', 'c'))
data <- data.frame(id=c('a', 'b', 'a', 'c', 'a'))

data$vector1 <- key[match(data$id, key$mat), 'vetor']
#or with merge
merge(data, key, by.x = "id", by.y = "mat")

So you want one unique integer for each different id column?

This is called a factor in R, and your id column is one.

To convert to a numeric representation, use as.numeric :

data <- data.frame(id=c('a', 'b', 'a', 'c', 'a'))
data$vector1 <- as.numeric(data$id)

This works because data$id is not a column of strings, but a column of factors .

Here's an answer I found that follows the "mathematical.coffee" tip:

vector1 <- c('b','a','a','c','a','a')  # 3 elements to be labeled: a, b and c
labels <- factor(vector1, labels= c('char a', 'char b', 'char c') )
data.frame(vector1, labels)

The only thing we need to observe is that in the factor(vector1,...) function, vector1 will be ordered and the labels must follow that order correctly.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM