[R] Can't seem to get lapply to work.

Keith S Weintraub kw1958 at gmail.com
Fri Sep 26 12:51:59 CEST 2014


Jeff,
Of course a data.frame is a list of columns. Duh!

Sorry about that. Was working about 10 hours straight.

Also sorry about the excess attributes. They were created by via expand.grid.

Here is my current solution which seems to be working:

    lapply(1:nrow(tstSet), function(x, y, z) oneRun(y[x,],z), tstSet, inp)

Thanks so much for your timely help,
Best,
KW


--

On Sep 26, 2014, at 12:23 AM, Jeff Newmiller <jdnewmil at dcn.davis.CA.us> wrote:

> On suggestion is to tell us what you want to accomplish, because that error makes perfect sense to me and your intent is rather opaque. Data frames are not lists of rows, they are lists of columns.
> 
> One solution could be
> 
> tstSet[["term"]] + 1
> 
> Another might be
> 
> tstSet <- within(tstSet,{term <- term +1})
> 
> ---------------------------------------------------------------------------
> Jeff Newmiller                        The     .....       .....  Go Live...
> DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
>                                      Live:   OO#.. Dead: OO#..  Playing
> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
> /Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
> --------------------------------------------------------------------------- 
> Sent from my phone. Please excuse my brevity.
> 
> On September 25, 2014 8:25:44 PM PDT, Keith S Weintraub <kw1958 at gmail.com> wrote:
>> Folks,
>> 
>> I have the following problem.
>> 
>> tstSet<-structure(list(corr = c(0.59, 0.62), term = c(7, 7), am =
>> c("AmYes", 
>> "AmNo"), prem = c(19.5, 14.75)), .Names = c("corr", "term", "am", 
>> "prem"), out.attrs = structure(list(dim = structure(c(3L, 2L, 
>> 2L, 41L), .Names = c("corr", "term", "am", "prem")), dimnames =
>> structure(list(
>>  corr = c("corr=0.59", "corr=0.62", "corr=0.65"), term = c("term=5", 
>>   "term=7"), am = c("am=AmYes", "am=AmNo"), prem = c("prem=10.00", 
>> "prem=10.25", "prem=10.50", "prem=10.75", "prem=11.00", "prem=11.25", 
>> "prem=11.50", "prem=11.75", "prem=12.00", "prem=12.25", "prem=12.50", 
>> "prem=12.75", "prem=13.00", "prem=13.25", "prem=13.50", "prem=13.75", 
>> "prem=14.00", "prem=14.25", "prem=14.50", "prem=14.75", "prem=15.00", 
>> "prem=15.25", "prem=15.50", "prem=15.75", "prem=16.00", "prem=16.25", 
>> "prem=16.50", "prem=16.75", "prem=17.00", "prem=17.25", "prem=17.50", 
>> "prem=17.75", "prem=18.00", "prem=18.25", "prem=18.50", "prem=18.75", 
>>  "prem=19.00", "prem=19.25", "prem=19.50", "prem=19.75", "prem=20.00"
>>   )), .Names = c("corr", "term", "am", "prem"))), .Names = c("dim", 
>> "dimnames")), row.names = c(460L, 239L), class = "data.frame")
>> 
>>> tstSet
>>   corr term    am  prem
>> 460 0.59    7 AmYes 19.50
>> 239 0.62    7  AmNo 14.75
>> 
>> A function:
>> 
>> twoRun<-function(aRow, inp) {
>> aRow[["term"]] + inp
>> }
>> 
>> When I try the following:
>> lapply(tstSet, twoRun, 1)
>> 
>> I get the following error:
>> Error in aRow[["term"]] : subscript out of bounds
>> 
>> Any suggestions?
>> 
>> Thanks much for your time,
>> KW
>> 
>> 
>> 
>> 
>> --
>> 
>> ______________________________________________
>> R-help at 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.
> 



More information about the R-help mailing list