[R] Matching rows in a Data set? I'm Stuck!!

Liviu Andronic landronimirc at gmail.com
Wed Mar 3 14:22:32 CET 2010


On 3/3/10, BioStudent <s0975764 at sms.ed.ac.uk> wrote:
>  What i really want to do is write an R script that says "if the ID of
>  dataset1 and 2 match (2nd row), print out that whole row into a new
>  dataset3".
>

Would this do what you want?
> x1 <- iris[1:5,]
> x1
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
> x2 <- iris[4:7,]
> x2
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa
7          4.6         3.4          1.4         0.3  setosa
> x.ids <- intersect(rownames(x1), rownames(x2))
> x.ids
[1] "4" "5"
> x3 <- cbind(x1[x.ids, ], x2[x.ids, ])


Liviu



More information about the R-help mailing list