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

前端 未结 3 1592
[愿得一人]
[愿得一人] 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条回答
  •  梦毁少年i
    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.

提交回复
热议问题