Hi, Try: res<-sapply(seq_len(ncol(dat1)),function(i) setNames(((1-coef(lm(dat1[,i]~dat2[,i]))[2])^2)*var(dat2[,i]),NULL)) res #[1] 21.00000 16.11842 18.69231 A.K.
Thank you for your answer. But further calculations will be much more difficult, like (1-b)^2 * Var(V1) for all matching columns where b is the slope from a regression V1 (from datset 1) on V1 (dataset 2) and Var(V1) the variance from V1(from dataset2). So what I'm looking for is something like a loop function... ----- Original Message ----- From: arun <smartpink...@yahoo.com> To: R help <r-help@r-project.org> Cc: Sent: Thursday, August 29, 2013 3:49 PM Subject: Re: calculate with different columns from different datasets Hi, Try: dat1<- read.table(text=" V1 V2 V3 2 6 8 4 3 4 1 9 8 ",sep="",header=TRUE) dat2<- read.table(text=" V1 V2 V3 6 8 4 2 0 7 8 1 3 ",sep="",header=TRUE) res1<- as.matrix(dat1-dat2) res1 # V1 V2 V3 #[1,] -4 -2 4 #[2,] 2 3 -3 #[3,] -7 8 5 res2<-t(t(dat1)-colMeans(dat2)) res2 # V1 V2 V3 #[1,] -3.333333 3 3.3333333 #[2,] -1.333333 0 -0.6666667 #[3,] -4.333333 6 3.3333333 A.K. Hi there I've got two datasets of the following form (just an example, the real dataset got a lot more columns) dataset1 V1 V2 V3 2 6 8 4 3 4 1 9 8 and dataset 2 V1 V2 V3 6 8 4 2 0 7 8 1 3 First, I'd like to calculate the following: V1 from dataset1 minus V1 from dataset2, than V2 from dataset1 minus V2 from dataset2 ... and so on (always Vn-Vn, where n=1,2,....n) and safe the solution-vectors in a new matrix. Second I'd like to run other functions over the two matching columns (for example: V1 from dataset1 minus mean(V1) from dataset2, V2 from dataset1 minus mean(V2) from dataset2,...). So I'm looking for a simple solution that always takes the matching columns from the different datasets and than I can just change the function for the two. Thank you for your help! Kind regards ______________________________________________ R-help@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.