[R] Passing a list object to lapply

Uwe Ligges ligges at statistik.tu-dortmund.de
Tue Aug 25 13:08:22 CEST 2009



Fowler, Mark wrote:
> Hello,
> 
> I'm having difficulty passing an object name to a lapply function. Can
> somebody tell me the trick to make this work?

lapply() works on objects rather than names of objects, hence you need 
get(paste(...)).

Uwe Ligges




> 
> #Works
> T13702 <- TRACKDATA[["13702.xls"]][["data"]]
> min(unlist(lapply(list(T13702), function(x) mdy.date(x[1, 2], x[1, 1],
> x[1, 3]))))
> 16553
> 
> #Works
> d<-2
> assign(paste("T",substr(names(TRACKDATA)[d],1,(nchar(names(TRACKDATA)[d]
> )-4)),sep=""),TRACKDATA[[d]][["data"]],pos=1)
> min(unlist(lapply(list(T13702), function(x) mdy.date(x[1, 2], x[1, 1],
> x[1, 3]))))
> 16553
> 
> #Fails.
> d<-2
> assign(paste("T",substr(names(TRACKDATA)[d],1,(nchar(names(TRACKDATA)[d]
> )-4)),sep=""),TRACKDATA[[d]][["data"]],pos=1)
> min(unlist(lapply(list(paste("T",substr(names(TRACKDATA)[d],1,(nchar(nam
> es(TRACKDATA)[d])-4)),sep="")), function(x) mdy.date(x[1, 2], x[1, 1],
> x[1, 3]))))
> Error in x[1, 2] : incorrect number of dimensions
>> traceback()
> 4: mdy.date(x[1, 2], x[1, 1], x[1, 3])
> 3: FUN(X[[1L]], ...)
> 2: lapply(list(paste("T", substr(names(TRACKDATA)[d], 1,
> (nchar(names(TRACKDATA)[d]) - 
>        4)), sep = "")), function(x) mdy.date(x[1, 2], x[1, 1], x[1, 
>        3]))
> 1: unlist(lapply(list(paste("T", substr(names(TRACKDATA)[d], 1, 
>        (nchar(names(TRACKDATA)[d]) - 4)), sep = "")), function(x)
> mdy.date(x[1, 
>        2], x[1, 1], x[1, 3])))
> 
> #Fails (trying noquote).
> min(unlist(lapply(list(noquote(paste("T",substr(names(TRACKDATA)[d],1,(n
> char(names(TRACKDATA)[d])-4)),sep=""))), function(x) mdy.date(x[1, 2],
> x[1, 1], x[1, 3]))))
> Error in unclass(x)[...] : incorrect number of dimensions
>> traceback()
> 6: `[.noquote`(x, 1, 2)
> 5: x[1, 2]
> 4: mdy.date(x[1, 2], x[1, 1], x[1, 3])
> 3: FUN(X[[1L]], ...)
> 2: lapply(list(noquote(paste("T", substr(names(TRACKDATA)[d], 1, 
>        (nchar(names(TRACKDATA)[d]) - 4)), sep = ""))), function(x)
> mdy.date(x[1, 
>        2], x[1, 1], x[1, 3]))
> 1: unlist(lapply(list(noquote(paste("T", substr(names(TRACKDATA)[d], 
>        1, (nchar(names(TRACKDATA)[d]) - 4)), sep = ""))), function(x)
> mdy.date(x[1, 
>        2], x[1, 1], x[1, 3])))
> 
>> 	Mark Fowler
> 		Population Ecology Division
>> 	Bedford Inst of Oceanography
>> 	Dept Fisheries & Oceans
>> 	Dartmouth NS Canada
> 		B2Y 4A2
> 		Tel. (902) 426-3529
> 		Fax (902) 426-9710
> 		Email fowlerm at mar.dfo-mpo.gc.ca
> 		Home Tel. (902) 461-0708
> 		Home Email mark.fowler at ns.sympatico.ca
> 
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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