[R] matching multiple fields from a matrix
arun
smartpink111 at yahoo.com
Tue Apr 16 00:26:40 CEST 2013
HI,
May be this helps:
dat1<- read.table(text="
site1 depth1 year1 site2 depth2 year2
10 30 1860 NA NA NA
NA NA NA 50 30 1860
10 20 1850 11 20 1850
11 25 1950 12 25 1960
10 NA 1870 12 30 1960
11 25 1880 15 22 1890
14 22 1890 14 25 1880
",sep="",header=TRUE,stringsAsFactors=FALSE)
res<-merge(dat1[,1:3],dat1[,4:6],by.x=c("depth1","year1"),by.y=c("depth2","year2"))
names(res)[1:2]<- gsub("\\d+","",names(res))[1:2]
na.omit(res)
# depth year site1 site2
#1 20 1850 10 11
#2 22 1890 14 15
#3 25 1880 11 14
#4 30 1860 10 50
A.K.
----- Original Message -----
From: jercrowley <jcrowley2 at mtech.edu>
To: r-help at r-project.org
Cc:
Sent: Monday, April 15, 2013 5:07 PM
Subject: [R] matching multiple fields from a matrix
I have been trying many ways to match 2 separate fields in a matrix. Here is
a simplified version of the matrix:
site1 depth1 year1 site2 depth2 year2
10 30 1860 NA NA NA
NA NA NA 50 30 1860
Basically I am trying to identify the sites which have a common year and
depth from 2 datasets. What I would like to do is match all of the year1
field to year2 field and the depth1 field and to depth2 field. Then I would
like to output site1, site2, depth, and year.
I have been trying if loops, which(), isTRUE(), etc. but I have not come up
with anything that works.
Any help would be greatly appreciated.
Jeremy
--
View this message in context: http://r.789695.n4.nabble.com/matching-multiple-fields-from-a-matrix-tp4664309.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.
More information about the R-help
mailing list