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

前端 未结 3 1569
[愿得一人]
[愿得一人] 2021-01-05 07:56

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 vecto

相关标签:
3条回答
  • 2021-01-05 08:17

    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")
    
    0 讨论(0)
  • 2021-01-05 08:20

    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.

    0 讨论(0)
  • 2021-01-05 08:36

    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.

    0 讨论(0)
提交回复
热议问题