根据第二个数据框中的匹配创建新列 [英] creating new column based on match in 2nd dataframe

查看:81
本文介绍了根据第二个数据框中的匹配创建新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有两个数据帧,则top3df:

If have two dataframes, top3df:

http://dpaste.com/1709875/

qw:

qw <- structure(list(id = structure(1:25, .Label = c("w01", "w02", "w03", "w04", "w05", "w06", "w07", "w08", "w09", "w10", "w11", "w12", "w13", "w14", "w15", "w16", "w17", "w18", "w19", "w20", "w21", "w22", "w23", "w24", "w25"), class = "factor"), link = structure(c(5L, 4L, 19L, 2L, 18L, 24L, 20L, 23L, 7L, 12L, 14L, 15L, 21L, 17L, 10L, 13L, 16L, 25L, 22L, 6L, 11L, 3L, 1L, 9L, 8L), .Label = c("http://gezondheid.blog.nl/overgewicht/2008/06/07/dik-zijn-heeft-veel-nadelen", "http://home.deds.nl/~obesitasinfo.nl/", "http://mens-en-gezondheid.infonu.nl/ziekten/18079-risicos-van-overgewicht-en-de-gevolgen-van-obesitas.html", "http://nl.wikipedia.org/wiki/Obesitas", "http://overgewicht.pilliewillie.nl/obesitas/behandeling.overgewicht.3.php", "http://www.afslankacademie.nl/page/2634/overgewicht.html", "http://www.afvallen-voeding.nl/", "http://www.erfelijkheid.nl/node/325", "http://www.gewoongezond.nl/", "http://www.gezondafvallen.net/", "http://www.gezonderafvallen.nl/page/938/overgewicht-als-gevolg-van-de-evolutie.html", "http://www.gr.nl/nl/adviezen/overgewicht-en-obesitas", "http://www.hely.net/oorzaken.html", "http://www.kiloafvallen.nl/", "http://www.nisb.nl/kennisplein-sport-bewegen/dossiers/bewegen-en-overgewicht/oorzaken-obesitas.html", "http://www.novarum.nl/eetproblemen/obesitas/signalen-en-gevolgen", "http://www.obesitas.azdamiaan.be/nl/index.aspx?n=280", "http://www.obesitaskliniek.nl/", "http://www.obesitasvereniging.nl/", "http://www.sagbmaagband.nl/minder-gewicht/morbideobesitas.html", "http://www.tipsbijafvallen.nl/", "http://www.tweestedenziekenhuis.nl/script/Template_SubsubMenu.asp?PageID=1144&SSMID=1247", "http://www.vgz.nl/zorg-en-gezondheid/ziektes-en-aandoeningen/obesitas", "http://www.volkskrant.nl/vk/nl/2672/Wetenschap-Gezondheid/article/detail/3143483/2012/01/30/Balanstop-in-Madurodam-mueslireep-tegen-obesitas.dhtml", "http://www.zuivelengezondheid.nl/?pageID=332"), class = "factor"), quality = c(3.875, 6.25, 7.875, 3.5, 6, 4.75, 3.625, 4.125, 2.375, 6, 2.125, 6.5, 2.5, 5.375, 2.5, 6.625, 5.125, 5, 6.875, 5.75, 6.125, 3.25, 1.75, 2.5, 7.375), q1 = c(0L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L), q2 = c(0L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 1L), q3 = c(0L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L)), .Names = c("id", "link", "quality", "q1", "q2", "q3"), class = "data.frame", row.names = c(NA, -25L))

我想在qw$link中寻找top3df$url的匹配项,然后创建一个新的变量top3df$id,将qw$id的值分配给该变量.

I want to look for a match in qw$link for top3df$url and then create a new variable top3df$id to which the value of qw$id is assigned.

直到现在,我都尝试过:

Untill now, I have tried:

if(top3df$url == qw$link){
  top3df$id <- qw$id
} else {
  top3df$id <- NA
}

top3df$id <- ifelse(match(top3df$url, qw$link), qw$id, NA)

但是两个都没有给出预期的结果.有什么建议可以解决这个问题吗?

But both did not give the intended result. Any suggestions how to solve this?

推荐答案

我无法读取您的top3df结构,因此无法验证我的答案,但这应该可以:

I can't read your top3df structure so can't verify my answer, but this should work:

top3df$id = qw$id[match(top3df$url,qw$link)]

这篇关于根据第二个数据框中的匹配创建新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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