[R] Problems with apply
Luis Felipe Parra
felipe.parra at quantil.com.co
Mon May 31 17:16:39 CEST 2010
Hello I am tryin to use the apply functions with two data frames I've got
and I am getting the following error message
Error en HistRio$SecSte : $ operator is invalid for atomic vectors
I don't understand why. when I use the apply I am doing:
PromP <- function(HistRio,AnaQuim){
xx <- c(0,0,0)
if(length(which(AnaQuim$SecSte==HistRio$SecSte))>0){ xx[1]<-1 }
if(length(which(as.Date(AnaQuim$AÑO1)<=as.Date(HistRio$FinCorte)))>0){ xx[2]
<- 1}
if( length(which(as.Date(AnaQuim$AÑO1)>=as.Date(HistRio$FechaSiembra)))>0){
xx[3]<-1 }
if( length(which(as.Date(AnaQuim$AÑO1)>=as.Date(HistRio$FechaSiembra)))>0 &
length(which(as.Date(AnaQuim$AÑO1)<=as.Date(HistRio$FinCorte)))>0 ){ xx[4]
<- 2}
return(xx)
}
zz<- apply(HistRio,1,PromP,AnaQuim)
and if I do exactly the same with a for
xx <- matrix(0,nrow(HistRio),4)
for(i in 1:nrow(HistRio)){
if(length(which(AnaQuim$SecSte==HistRio$SecSte[i]))>0){ xx[1]<-1 }
if(length(which(as.Date(AnaQuim$AÑO1)<=as.Date(HistRio$FinCorte[i])))>0){
xx[2] <- 1}
if(
length(which(as.Date(AnaQuim$AÑO1)>=as.Date(HistRio$FechaSiembra[i])))>0){
xx[3]<-1 }
if( length(which(as.Date(AnaQuim$AÑO1)>=as.Date(HistRio$FechaSiembra[i])))>0
& length(which(as.Date(AnaQuim$AÑO1)<=as.Date(HistRio$FinCorte[i])))>0 ){
xx[4] <- 2}
}
I get no error message. Attached is the data I am using. Any idea of why
this is happening?
Thank you
Felipe Parra
More information about the R-help
mailing list