如何合并具有相似但不相同的数据的两个数据框?

时间:2017-02-16 14:22:37

标签: r

我对R很新,偶然发现了一个问题。我目前有两个数据框,一个包含项目列表及其位置(list1),另一个包含与类似位置相关的数据(list2)

e.g。

# Uncomment this line to define a global platform for your project
platform :ios, '8.0'

target 'SCLAlertView2' do
pod 'SCLAlertView'

end

target 'IceCreamShopTests' do

end

我想知道list1中的哪些项目属于list2观察到的数据,以获得理想情况下的最终产品:

import SCLAlertView

所以,在伪代码中,我认为我正在做类似的事情......

list1
name    start    end
a        1        50
b        51       74
c        89       201
d        300      543


list2
data   start     end
-1      1         80
0       85       224
1       256      603

我已经找到了不同的方法来做到这一点,但到目前为止,我很难理解我如何在R中指定上述内容。

我试过了:

list3
data   start     end    name
-1      1         80    a,b
0       85       224    c
1       256      603    d

但是这会导致多个相同的错误:

for(each row in list2) {
     loop(each row in list2 {
         if((list1$start >= list2$start) & (list1$end <= list2$end)) {
                print (list1$name into new column)
              }
           }
       } 

我认为这是因为我用于for(i in 1:dim(list2)[1]) { for(j in 1:dim(list1)[1]) { if((list1$start >= list2$start) & (list1$end <= list2$end)) { print(list1$name) } } } 语句的数据帧不是长度为1的In if ((list1$start >= list2$start) & ... : the condition has length > 1 and only the first element will be used 向量,但是,我不确定如何执行查询。

任何建议都将不胜感激!!

祝福, 娜塔莉

0 个答案:

没有答案
相关问题