Dgnn
2010-Mar-08 02:29 UTC
[R] lapply and list indexing basics (after realizing I wasn't previously subscribed...sorry)
I have split my original dataframe to generate a list of dataframes each of which has 3 columns of factors and a 4th column of numeric data. I would like to use lapply to apply the fitdistr() function to only the 4th column (x$isi) of the dataframes in the list. Is there a way to do this or am I misusing lapply? As a second solution I tried splitting only the numeric data column to yield a list of vectors and then using lapply(myList, fitdistr, densfun='gamma',start=list(scale=1, shape=2)) returns the error: Error in optim(x = c(305, 290, 283, 363, 331, 293, 304, 312, 286, 339, : non-finite finite-difference value [2] In addition: Warning message: In dgamma(x, shape, scale, log) : NaNs produced However, if I use fitdistr(myList[[i]]) on each element of the list, there are no errors. Thanks in advance for any comments. -jason -- View this message in context: http://n4.nabble.com/lapply-and-list-indexing-basics-after-realizing-I-wasn-t-previously-subscribed-sorry-tp1584053p1584053.html Sent from the R help mailing list archive at Nabble.com.
Simon Knapp
2010-Mar-08 07:03 UTC
[R] lapply and list indexing basics (after realizing I wasn't previously subscribed...sorry)
library(MASS) dat <- data.frame( col1=as.factor(sample(1:4, 100, T)), col2=as.factor(sample(1:4, 100, T)), col3=as.factor(sample(1:4, 100, T)), isi=rnorm(100) ) dat <- split(dat, as.factor(sample(1:3, 100, T))) lapply(dat, function(x, densfun) fitdistr(x$isi, densfun), 'normal') Not used fitdistr before, and without data, I can't help with the error you note below (which may well still occur) Hope this helps. On Mon, Mar 8, 2010 at 1:29 PM, Dgnn <sharkbrainpdx@gmail.com> wrote:> > I have split my original dataframe to generate a list of dataframes each of > which has 3 columns of factors and a 4th column of numeric data. > I would like to use lapply to apply the fitdistr() function to only the 4th > column (x$isi) of the dataframes in the list. > > Is there a way to do this or am I misusing lapply? > > As a second solution I tried splitting only the numeric data column to > yield > a list of vectors and then using > lapply(myList, fitdistr, densfun='gamma',start=list(scale=1, shape=2)) > returns the error: > Error in optim(x = c(305, 290, 283, 363, 331, 293, 304, 312, 286, 339, : > non-finite finite-difference value [2] > In addition: Warning message: > In dgamma(x, shape, scale, log) : NaNs produced > > However, if I use fitdistr(myList[[i]]) on each element of the list, there > are no errors. > > Thanks in advance for any comments. > > -jason > -- > View this message in context: > http://n4.nabble.com/lapply-and-list-indexing-basics-after-realizing-I-wasn-t-previously-subscribed-sorry-tp1584053p1584053.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >[[alternative HTML version deleted]]