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
Hi, Here is what ?apply says: "Returns a vector or array or list of values obtained by applying a function to margins of an array." So apply() works on arrays, not on dataframes! Maybe lapply() would do what you're looking for (don't have time to look more into it) And you don't do "exactly the same with a for" because xx is a matrix (a matrix is a 2-dimensional array, dataframes are lists) HTH, Ivan PS: I don't see any data attached. Think about using dput(), it's great ;) Le 5/31/2010 17:16, Luis Felipe Parra a écrit :> 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. >-- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. Säugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calandra@uni-hamburg.de ********** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php [[alternative HTML version deleted]]
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.parra@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.Meys@Ugent.be ------------------------------- Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php [[alternative HTML version deleted]]