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.calan...@uni-hamburg.de ********** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.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.