Sunday, February 18, 2018

r - creating new column based on match in 2nd dataframe



If have two dataframes, top3df:



http://dpaste.com/1709875/



and 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))


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
}


and



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?


Answer



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)]

No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...