Dear R users: Here's a barebones example of what I can't make work. As you can see, regexpr() does not perform an exact string match, which only occurs in row 1 of these data frames. Instead, as it's supposed to do, it finds "b" in "bb" and "c" in "cc". Does anybody know what function I can use such that only the first rows would be matched (ie, exact string match?) I've also tried simply using the == operator, in which case i get the error:"level sets of factors are different" Thank you in advance, B> ## two toy data frames, containing character arrays > D1=as.data.frame(c("a","b","c")) > D2=as.data.frame(c("a","bb","cc")) > > ## loop through each comparing the strings in each row > i=1 #counter > while (regexpr(D1[i,1], D2[i,1]) == TRUE) {+ cat("identical match on row #", i, "\n") + i=i+1 + if (i>3) break + } identical match on row # 1 identical match on row # 2 identical match on row # 3 -- View this message in context: http://old.nabble.com/Exact-String-Compare-in-R--tp26160122p26160122.html Sent from the R help mailing list archive at Nabble.com.
On Tue, Nov 3, 2009 at 2:41 PM, bamsel <benamsel at gmail.com> wrote:> > Dear R users: > Here's a barebones example of what I can't make work. > As you can see, regexpr() does not perform an exact string match, which only > occurs in row 1 of these data frames. Instead, as it's supposed to do, it > finds "b" in "bb" and "c" in "cc". Does anybody know what function I can use > such that only the first rows would be matched (ie, exact string match?) > I've also tried simply using the == operator, in which case i get the > error:"level sets of factors are different"Because they are not strings, but factors. Convert them to strings with as.character and use "==". Best, Gabor> Thank you in advance, > B > >> ## two toy data frames, containing character arrays >> D1=as.data.frame(c("a","b","c")) >> D2=as.data.frame(c("a","bb","cc")) >> >> ## loop through each comparing the strings in each row >> i=1 #counter >> while (regexpr(D1[i,1], D2[i,1]) == TRUE) { > + ? ? ? ?cat("identical match on row #", i, "\n") > + ? ? ? ?i=i+1 > + ? ? ? ? if (i>3) break > + } > identical match on row # 1 > identical match on row # 2 > identical match on row # 3 > -- > View this message in context: http://old.nabble.com/Exact-String-Compare-in-R--tp26160122p26160122.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Gabor Csardi <Gabor.Csardi at unil.ch> UNIL DGM
Hi what about D2[,1] %in% D1[,1] Regards Petr r-help-bounces at r-project.org napsal dne 03.11.2009 14:41:02:> > Dear R users: > Here's a barebones example of what I can't make work. > As you can see, regexpr() does not perform an exact string match, whichonly> occurs in row 1 of these data frames. Instead, as it's supposed to do,it> finds "b" in "bb" and "c" in "cc". Does anybody know what function I canuse> such that only the first rows would be matched (ie, exact string match?) > I've also tried simply using the == operator, in which case i get the > error:"level sets of factors are different" > > Thank you in advance, > B > > > ## two toy data frames, containing character arrays > > D1=as.data.frame(c("a","b","c")) > > D2=as.data.frame(c("a","bb","cc")) > > > > ## loop through each comparing the strings in each row > > i=1 #counter > > while (regexpr(D1[i,1], D2[i,1]) == TRUE) { > + cat("identical match on row #", i, "\n") > + i=i+1 > + if (i>3) break > + } > identical match on row # 1 > identical match on row # 2 > identical match on row # 3 > -- > View this message in context:http://old.nabble.com/Exact-String-Compare-in-> R--tp26160122p26160122.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.