[R] applying cor.test in two different but matched dataframes

Mark Lyman mark.lyman at gmail.com
Fri Nov 17 22:40:26 CET 2006


Gabriele Stocco <stoccog <at> units.it> writes:

> 
> Dear R help,
> I have two dataframes with the same number of rows and columns. Each
> column is for a patient, each row for a variable. The columns and rows
> are matched in the two dataframes, so that each patient (column) and
> each variable (row) has the same position in the two dataframes. The
> values are different since each dataframe reports data from a
> different way to measure the same variables in the sampe patients.
> 
> How can I make a cor.test, applying the function row by row in the two
> different dataframes, possibly without merging them?
> 
> Thanks,
> 
> Gabriele Stocco
> University of Trieste

Try:

# Create Data Frames With Rows as Variables
x<-as.data.frame(t(as.data.frame(matrix(rnorm(100),10))))
y<-as.data.frame(t(as.data.frame(matrix(rnorm(100),10))))

# Convert to Lists So that Each Element of List is a variable
t.x<-as.list(as.data.frame(t(x)))
t.y<-as.list(as.data.frame(t(y)))

# Use mapply to apply function to the two lists; see ?mapply
mapply(cor.test,t.x,t.y)


Mark Lyman



More information about the R-help mailing list