R中的MATCH功能 [英] MATCH function in r

查看:130
本文介绍了R中的MATCH功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有列表,第一个(列表1)具有ID,名称,年龄,另一个(列表2,列表3,..)具有ID和测试值(唯一).

have lists, the first one (list1) has id,name,age and the other ones (list2,list3,..) has ids and test value (unique).

列表1:

id  age name    bio-test    
1   40  danny
2   16  nora            
3   35  james
4   21  ben

列表2(生物测试):

id  test passed year   
1   100   yes   1
5   80    yes   n/a      
4   55    no    2

我试图将每个ID的测试值添加到list1(并非每个ID都有一个测试值).

I am trying to add to list1 the test value to each id (not every id have a test value).

这是代码的一部分:

for (i in 1:length(list1)) { 
list1$test1value <- list2$test[match(list1$id[i], list2$id[i]),
nomatch = NA_integer_, incomparables = NULL)] }

但是通过id查找测试值,它仅复制了list2中的第一个测试值并将其复制到200个单元格中,而其他3000个则为N/A.

but instead looking up the test value by id ,it copied just the first test value from list2 and copied it to 200 cells and the other 3000 are N/A.

怎么了?

推荐答案

首先,您的示例中有错别字.其次,对"list1 $ test1value"的赋值应添加一个"[i]",以免在每个回合中都保存.还应该在list2$id中不添加'[i]',因为您要搜索整个矢量以进行查找.

First you have typos in your example. Secondly, the assignment of 'list1$test1value' should have an '[i]' added to it to not save over each round. There should also not be an '[i]' added to list2$id since you want to search the entire vector for the lookup.

for (i in 1:length(list1)) { 
  list1$test1value[i] <- list2$test[match(list1$id[i], list2$id,
                             nomatch = NA_integer_, incomparables = NULL)] }

代码有效,但是这里没有任何循环的理由.您对R的运行方式缺乏了解.以下代码可以更快地完成完全相同的操作.

The code works, but there is no reason for any loops here. You are showing a lack of understanding in how R operates. The below code does the exact same thing much faster.

list1$test1value <- list2$test[match(list1$id, list2$id)]

构建

R使得您不必握住它的手并指示它如何遍历向量的每个元素. match将自动一个一个地遍历每个成员,并在另一个向量中为您查找.还将在数据集中按顺序分配结果.

R is built so that you do not have to hold its hand and instruct it how to go through each element of the vector. match will automatically iterate through each member one by one and look it up in the other vector for you. It will also assign the result in an orderly way in the dataset.

我将其作为副本关闭,因为正如其他人所建议的,merge对此非常适合.

I will close this as a duplicate because as others suggested, merge is perfect for this.

merge(list1, list2[c("id", "test")], all.x=TRUE)
#  id age  name test
#1  1  40 danny  100
#2  2  16  nora   NA
#3  3  35 james   NA
#4  4  21   ben   55

这篇关于R中的MATCH功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆