[R] Help needed in fine tuning the sapply output

Girish A.R. garamach at gmail.com
Wed Feb 10 12:29:05 CET 2010


Hi All,

Say I have a data set with the following structure:

Lines <- "ID  ref_rank    R1  R2  R3  R4  R5  R6
A   6   3   NA NA        7   8   2
B   7   4    NA NA       9   5   10
C   3   7    NA NA       8   10  8
D   4   2    NA NA       4   8   1
E   2   5    NA NA       4   7   7
F   8   5    NA NA       7   10  9
G   5   9    NA NA      8   10  2
H   1   6    NA NA       7   10  10"
my.data <- read.table(con<-textConnection(Lines), header = TRUE) 
close(con)

I create a function to compute Spearman's rank correlation between a
reference column named "ref_rank" and columns R1--R6, as shown below:

my.func <- function(x)
{
   rc <- rcorr(my.data[,c("ref_rank")], x)
   data.frame(Spearman = rc$r[2],Pval=rc$P[2])
} 

I then apply the function on the desired columns as shown below:

a<-data.frame(t(sapply(my.data[, 3:8], my.func)))

I almost get what I intended to do, but I'm unable to export the data frame
"a" to CSV format. For that matter, I'm not able to remove NA's using
na.omit(a). Here's the log:

> class(a)
[1] "data.frame"
> write.csv(a,file="aranks.csv",na="")
Error in write.table(x, file, nrow(x), p, rnames, sep, eol, na, dec,
as.integer(quote),  : 
  unimplemented type 'list' in 'EncodeElement'

I'm not sure, but I'm suspecting the problem might be in the output
structure of the my.func. Any helpful hints in resolving this is highly
appreciated.

cheers,
-Girish

> sessionInfo()
R version 2.10.1 (2009-12-14) 
i386-pc-mingw32 

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United
States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] splines   grid      stats     graphics  grDevices utils     datasets 
methods  
[9] base     

other attached packages:
 [1] polycor_0.7-7   sfsmisc_1.0-10  mvtnorm_0.9-9   Hmisc_3.7-0    
survival_2.35-7
 [6] RWinEdt_1.8-2   ggplot2_0.8.5   digest_0.4.2    reshape_0.8.3  
plyr_0.1.9     
[11] proto_0.3-8    

loaded via a namespace (and not attached):
[1] cluster_1.12.1   Formula_0.2-0    kinship_1.1.0-23 lattice_0.17-26 
MASS_7.3-4      
[6] nlme_3.1-96      plm_1.2-1        sandwich_2.2-5   tools_2.10.1    


-- 
View this message in context: http://n4.nabble.com/Help-needed-in-fine-tuning-the-sapply-output-tp1475639p1475639.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list