Jonathan Greenberg
2010-Jan-28 21:03 UTC
[R] Data frame of different sized lists in a function call
I'm hoping to get some "best practice" feedback for constructing a function call which takes an undefined set of DIFFERENT length vectors -- e.g. say we have two lists: list1=c(1:10) list2=c(2:4) lists = data.frame(list1,list2) coerces those two to be the same length (recycling list2 to fill in the missing rows) -- what is a quick way of having each of those lists retain their original lengths? my function ultimately should look like: myfunction = function(lists) { ... } I'm hoping this can be done with a single line, so the user doesn't have to pre-construct the data.frame before running the function, if at all possible. Thanks! --j -- Jonathan A. Greenberg, PhD Postdoctoral Scholar Center for Spatial Technologies and Remote Sensing (CSTARS) University of California, Davis One Shields Avenue The Barn, Room 250N Davis, CA 95616 Phone: 415-763-5476 AIM: jgrn307, MSN: jgrn307 at hotmail.com, Gchat: jgrn307
David Winsemius
2010-Jan-28 21:10 UTC
[R] Data frame of different sized lists in a function call
On Jan 28, 2010, at 4:03 PM, Jonathan Greenberg wrote:> list1=c(1:10) # neither of which really are lists > list2=c(2:4) > > lists = list(list1,list2) $ a list of two vectors.David Winsemius, MD Heritage Laboratories West Hartford, CT
Greg Snow
2010-Jan-28 21:10 UTC
[R] Data frame of different sized lists in a function call
If you understand the differences between R lists and R vectors then this should be easy:> vec1 <- 1:10 > vec2 <- 2:4> myListOfVectors <- list( vec1, vec2 )Now you can pass the single list of 2 different sized vectors to your function. For more details on working with lists (and vectors and functions and ... ) read "An Introduction to R" which is worth a lot more than you pay for it. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Jonathan Greenberg > Sent: Thursday, January 28, 2010 2:03 PM > To: r-help > Subject: [R] Data frame of different sized lists in a function call > > I'm hoping to get some "best practice" feedback for constructing a > function call which takes an undefined set of DIFFERENT length vectors > -- e.g. say we have two lists: > > list1=c(1:10) > list2=c(2:4) > > lists = data.frame(list1,list2) coerces those two to be the same length > (recycling list2 to fill in the missing rows) -- what is a quick way of > having each of those lists retain their original lengths? my function > ultimately should look like: > > > myfunction = function(lists) { > > ... > > } > > I'm hoping this can be done with a single line, so the user doesn't > have > to pre-construct the data.frame before running the function, if at all > possible. > > Thanks! > > --j > > -- > > Jonathan A. Greenberg, PhD > Postdoctoral Scholar > Center for Spatial Technologies and Remote Sensing (CSTARS) > University of California, Davis > One Shields Avenue > The Barn, Room 250N > Davis, CA 95616 > Phone: 415-763-5476 > AIM: jgrn307, MSN: jgrn307 at hotmail.com, Gchat: jgrn307 > > ______________________________________________ > 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.