[R-sig-eco] How to index different datasets in the common period based on their same column?

Etienne Laliberté etiennelaliberte at gmail.com
Tue Jan 5 19:21:37 CET 2010


There is certainly no need for a loop. Here's one way of doing this,
though I'm sure others will come up with something more elegant.

# create dummy data
data1 <- matrix(c(1550:2005, rnorm(456)), 456, 2)
data2 <- matrix(c(1959:2008, rnorm(50)), 50, 2)

# identify common subsets
sub1 <- data1[,1] %in% data2[,1]
sub2 <- data2[,1] %in% data1[,1]

# compute correlation
cor(data1[sub1,2], data2[sub2,2])


> Date: Mon, 4 Jan 2010 07:49:17 -0800 (PST)
> From: Zongshan Li <zongshan_li at yahoo.com>
> Subject: [R-sig-eco] How to index different datasets in the common
> 	period	based on their same column?
> To: r-sig-ecology at r-project.org
> Message-ID: <587513.23865.qm at web114015.mail.gq1.yahoo.com>
> Content-Type: text/plain
> 
> Dear the friends of R-Ecology: 
>  
>  I have two datasets, for example, one is the tree.width, having two columns, year (A.. D.1550-2005) and ring width indices; another dataset is the clim.tem, having thirteen columns, year (A. D. 1959-2008) and monthly tem. 
> I want to calculate the correlation coefficients between the tree ring data and monthly tem. during the common period(A. D. 1959-2005), but I am confused how to index two datasets?
> So I have to give a fixed range of common period for the datasets in the for loop, just as following:
> 
> cor.std<-matrix(NA,nrow=25,ncol=12)
> for(i in 1:15)
> {
>  cor.std[i,1]=cor(mne.ylyns[267:313,2],dq.tmn.ex[1:47,i+1],method=c("pearson"))
>  cor.std[i,2]=cor(mne.ylys[361:407,2],dq.tmn.ex[1:47,i+1],method=c("pearson"))
>  cor.std[i, 3]=cor(mne.ylls[311:357,2],dq.tmn.ex[1:47,i+1],method=c("pearson"))
>  -------------
> }
> 
> 
> 
> Any help is strongly appreciated,
> thank you in advance,
> 
> ----------------------
> Zongshan, Li 
> 2010-01-04
> Research Center for Eco-Environmental Sciences,Chinese Academy of Sciences
> Shuangqing Road, Haidian street, Beijing, 100085
> Tel: 86-10-13699145748
> QQ: 1141958023
> email: ZongShan_Li at yahoo.com
> website: http://www.rcees.ac.cn/


-- 
Etienne Laliberté
================================
School of Forestry
University of Canterbury
Private Bag 4800
Christchurch 8140, New Zealand
Phone: +64 3 366 7001 ext. 8365
Fax: +64 3 364 2124
www.elaliberte.info



More information about the R-sig-ecology mailing list