[R] compare strings

Peter Dalgaard p.dalgaard at biostat.ku.dk
Sat Dec 1 09:55:58 CET 2007


Bernd Jagla wrote:
> It helps writing down these question, you are then getting much closer to an
> answer...
>
> summary(as.integer(t3[,2]) == as.integer(t3[,4]) & as.integer(t3[,3]) ==
> as.integer(t3[,5]))
>
> will compare two pairs of column pairs and give a count of flase and true
> rows...
>
>   
If they really are factors with different level sets, I think you might 
prefer as.character() there.

 > x <- factor(c("a","b"))
 > y <- factor(c("b","c"))
 > x==y
Error in Ops.factor(x, y) : level sets of factors are different
 > as.integer(x)==as.integer(y)
[1] TRUE TRUE
 > as.character(x)==as.character(y)
[1] FALSE FALSE

Also, extending the above slightly:
 > d <- as.character(x)==as.character(y)
 > table(d)
d
FALSE  TRUE
    2     1
 > which(!d)
[1] 1 2



> -B
>
> |-----Original Message-----
> |From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
> |Behalf Of Bernd Jagla
> |Sent: Friday, November 30, 2007 10:25 PM
> |To: r-help at r-project.org
> |Subject: [R] compare strings
> |
> |Sorry for the question, but I really cannot find the right search terms to
> |find an answer..
> |
> |
> |
> |I have a data frame with strings in some of the columns.
> |
> |I want to know all the rows where the strings in both columns are equal.
> |
> |
> |
> |How do I do this?
> |
> |
> |
> |Thanks,
> |
> |
> |
> |Bernd
> |
> |
> |	[[alternative HTML version deleted]]
> |
> |______________________________________________
> |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.
>
> ______________________________________________
> 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.
>   


-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-help mailing list