[英]R: Assigning values for matching IDs
Let's imagine I have a data set that has 3 different variable ids. 让我们假设我有一个包含3个不同变量id的数据集。
RowID , which is basically this: data$rownumber = 1:nrow(data). RowID ,基本上是这样的:data $ rownumber = 1:nrow(data)。 Then, ID , which is the company identifier.
然后, ID ,即公司标识符。 Finally, ID2 , which tells the user which RowID is the closest
最后, ID2 ,告诉用户哪个RowID最接近
RowID = c(1, 2, 3, 4, 5, 6)
ID = c(123, 456, 789, 712, 409, 587)
Price = c(200, 300, 400, 100, 50, 10)
ID2 = c(2, 1, 1, 1, 3, 3)
df = data.frame(RowID, ID, Price, ID2)
df
RowID ID Price ID2
1 123 200 2
2 456 300 1
3 789 400 1
4 712 100 1
5 409 50 3
6 587 10 3
I want to replace the values of ID2 by using the values of ID so that my data looks like this: 我想通过使用ID的值来替换ID2的值,以便我的数据如下所示:
RowID ID Price ID2 ID3
1 123 200 2 456
2 456 300 1 123
3 789 400 1 123
4 712 100 1 123
5 409 50 3 789
6 587 10 3 789
My data is really big, and of course this is just an example on how it looks. 我的数据真的很大,当然这只是它的外观的一个例子。
Thanks! 谢谢!
We can just use 'ID2' as numeric index 我们可以使用'ID2'作为数字索引
df$ID3 <- with(df, ID[ID2])
df
# RowID ID Price ID2 ID3
#1 1 123 200 2 456
#2 2 456 300 1 123
#3 3 789 400 1 123
#4 4 712 100 1 123
#5 5 409 50 3 789
#6 6 587 10 3 789
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.