Hi, Try: Testcor <- read.table("Testcor.txt",sep="",header=TRUE,stringsAsFactors=FALSE) Testcor$DATE <- as.Date(Testcor$DATE) firm.names <- colnames(Testcor)[!grepl("DATE|Market",colnames(Testcor))] res <- sapply(firm.names, function(x) {cor.results <- cor.test(Testcor[,x],Testcor[,"Market"],na.action=na.exclude); cor.results$estimate}) # PAXN.cor PED.cor PEDP.cor PM.cor PRFN.cor PRWN.cor #-0.31113122 -0.09359550 -0.17056943 0.40025112 0.34385888 0.17935045 # ZG.cor ZUBN.cor ZURN.cor ZWM.cor #0.20762797 0.39238270 0.74336028 -0.09166795
A.K. On Tuesday, October 15, 2013 1:57 PM, rissa <riss...@gmx.ch> wrote: Hi together I'm pretty new to R, so excuse me if it is a basic question. I have a big dataset (extract of it found in the attachment) of returns from firms. I'd like to compute the Pearson correlation of each firm with the "Market" and the corresponding p-Value. So I thought of making a list of 'cor.test's and then extract the needed values with a for loop. What I did so far... dataset=Testcor dataset$DATE<-as.Date(dataset$DATE,"%Y-%m-%d") firm.names=colnames(dataset)[2:length(colnames(dataset))] cor.results1=lapply(firm.names,function(x)cor.test(as.formula(paste(firm.names[firm.names==x],Market)),data=dataset,na.action=na.exclude)) But I always get error messages like Error in paste(firm.names[firm.names == x], Market) : object 'Market' not found or (when I attach dataset) Error in parse(text = x) : <text>:1:6: unexpected numeric constant 1: PAXN NA (Next step would have been something like for(i in 1:ncol(cor.results1)){ newmatrix[1,i]= cor.results1 [[i]]$estimate} etc) Could you please help me? Thank you so much! Rissa Testcor.txt <http://r.789695.n4.nabble.com/file/n4678290/Testcor.txt> -- View this message in context: http://r.789695.n4.nabble.com/Problem-with-lapply-tp4678290.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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. ______________________________________________ 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.