[R] Problem with lapply

arun smartpink111 at yahoo.com
Tue Oct 15 21:56:35 CEST 2013


No problem.
Regarding your code:

#changed name from 'dataset' to "Testcor"
#Your code

# [1] "PAXN"   "PED"    "PEDP"   "PM"     "PRFN"   "PRWN"   "ZG"     "ZUBN"  
 #[9] "ZURN"   "ZWM"    "Market" #####includes "Market"

#If you look my code:
firm.names <- colnames(Testcor)[!grepl("DATE|Market",colnames(Testcor))]
# [1] "PAXN" "PED"  "PEDP" "PM"   "PRFN" "PRWN" "ZG"   "ZUBN" "ZURN" "ZWM" 

If you wanted to use the "formula" method:
res1 <- sapply(firm.names,function(x) { cor.results <- cor.test(as.formula(paste("~", paste(x,"Market", sep="+")) ),data=Testcor,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 
#[1] TRUE

#or just
res2 <- sapply(firm.names,function(x) { cor.test(~get(x) + Market,data=Testcor,na.action=na.exclude)$estimate })
#[1] TRUE


On Tuesday, October 15, 2013 3:15 PM, rissa <rissa_r at gmx.ch> wrote:
Thank you! Worked perfectly!

By the way: What was wrong with my original code?

View this message in context: http://r.789695.n4.nabble.com/Problem-with-lapply-tp4678290p4678305.html

Sent from the R help mailing list archive at Nabble.com.

R-help at r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list