[英]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: 我需要一个data.frame输出,将每个向量值与特定的id匹配,从而产生:
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? 那么你想为每个不同的
id
列一个唯一的整数?
This is called a factor in R, and your id
column is one. 这在R中称为因子 ,您的
id
列为1。
To convert to a numeric representation, use as.numeric
: 要转换为数字表示,请使用
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 . 这是有效的,因为
data$id
不是一列字符串,而是一列因子 。
Here's an answer I found that follows the "mathematical.coffee" tip: 这是我在“mathematical.coffee”提示后面找到的答案:
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. 我们唯一需要注意的是,在
factor(vector1,...)
函数中, vector1
将被排序,标签必须正确地遵循该顺序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.