Ivan is -partly- right. However, in the details it says as well that : If X is not an array but has a dimension attribute, apply attempts to coerce it to an array via as.matrix if it is two-dimensional (e.g., data frames) or via as.array.
The main problem is the fact that what goes into the PromP function is not a dataframe, not even a matrix, but a vector. You can easily see where it goes wrong if you place print(str(HistRio)) as a first line in your function. You'll also see that (hopefully) it's a named vector, meaning you could try to rewrite your function like : if(length(which(AnaQuim$SecSte==HistRio["SecSte"]))>0){ xx[1]<-1 } etc... I didn't test it out though, but it should work. Cheers Joris On Mon, May 31, 2010 at 5:16 PM, Luis Felipe Parra < felipe.pa...@quantil.com.co> wrote: > 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 > > ______________________________________________ > 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. > > -- Joris Meys Statistical Consultant Ghent University Faculty of Bioscience Engineering Department of Applied mathematics, biometrics and process control Coupure Links 653 B-9000 Gent tel : +32 9 264 59 87 joris.m...@ugent.be ------------------------------- Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php [[alternative HTML version deleted]]
______________________________________________ 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.