indexing in data frames
On Aug 10, 2012, at 01:36 , William Dunlap wrote:
And if you are extremely concerned with speed, do not compute a$b and a$c in every iteration of the loop. E.g., change lapply(seq_along(a$c),function(x) a$b[x]-a$c[[x]]) to something like with(a, lapply(seq_along(c), function(x)b[x] - c[[x]]))
This seems to be working fine as well:
a <- data.frame(b = 1:2, c = I(list(2, 1:2)))
a <- within(a, dif <- mapply("-", b, c))
a
b c dif 1 1 2 -1 2 2 1, 2 1, 0
Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of R. Michael Weylandt Sent: Thursday, August 09, 2012 4:22 PM To: arun Cc: R help; jimi adams Subject: Re: [R] indexing in data frames On Thu, Aug 9, 2012 at 5:30 PM, arun <smartpink111 at yahoo.com> wrote:
lapply(1:length(a$c),function(x) a$b[x]-a$c[[x]])
Arun, I've seen you use this idiom a few times lately and I'd just like to note that seq_along() is an (underutilized) primitive and a safer and faster alternative (avoiding the pathological length(x) = 0 case). Cheers, Michael
______________________________________________ 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.
______________________________________________ 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.
Peter Dalgaard, Professor Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com