[R] R's list data structure
Given dayOfWeekName-c(Mon,Tue,Wed,Thu,Fri,Sat,Sun); dayOfWeekOrdinal-c(1,2,3,4,5,6,0); dayOfWeekWorkDay-c(TRUE,TRUE,TRUE,TRUE,TRUE,FALSE,FALSE); weekProfile-list(dow=dayOfWeekName,dowI=dayOfWeekOrdinal,dowW=dayOfWeekWorkDay) 1. How can I conditionally get dow, dowI, and dowW from weekProfile? If another 'arrangement' of this list object will make this task easier, please advise. 2. What is the point of the list object? I know that when mixed data types need to be held together, then the only option is to use the list data structure. If I were to hold recurring (Name, Salary, DateOfBirth) (i.e. character, integer and date values) in a list object, what would be the 'optimal' arrangement? Would that be as the components of weekProfile above? Or will this be better. Either: personalDetail- list(rbind(c(Name,Salary,DateOfBirth),c(Name,Salary,DateOfBirth))); Thanks for sharing your insight. [[alternative HTML version deleted]] __ 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.
Re: [R] R's list data structure
HI Ajay, On Fri, Feb 17, 2012 at 3:20 PM, Ajay Askoolum aa2e...@yahoo.co.uk wrote: Given dayOfWeekName-c(Mon,Tue,Wed,Thu,Fri,Sat,Sun); dayOfWeekOrdinal-c(1,2,3,4,5,6,0); dayOfWeekWorkDay-c(TRUE,TRUE,TRUE,TRUE,TRUE,FALSE,FALSE); weekProfile-list(dow=dayOfWeekName,dowI=dayOfWeekOrdinal,dowW=dayOfWeekWorkDay) 1. How can I conditionally get dow, dowI, and dowW from weekProfile? If another 'arrangement' of this list object will make this task easier, please advise. 2. What is the point of the list object? I know that when mixed data types need to be held together, then the only option is to use the list data structure. In your particular case, where all list components are the same length and are associated with each other in order, a special type of list called a data frame is easier to work with. weekProfile- data.frame(dow=dayOfWeekName,dowI=dayOfWeekOrdinal,dowW=dayOfWeekWorkDay) weekProfile dow dowI dowW 1 Mon1 TRUE 2 Tue2 TRUE 3 Wed3 TRUE 4 Thu4 TRUE 5 Fri5 TRUE 6 Sat6 FALSE 7 Sun0 FALSE I'm not sure what kind of conditional you want, but this can easily be done with subset() or [ weekProfile[weekProfile$dowW ,] dow dowI dowW 1 Mon1 TRUE 2 Tue2 TRUE 3 Wed3 TRUE 4 Thu4 TRUE 5 Fri5 TRUE A regular list is excellent for holding diverse kinds of data, for example 10 lm() objects, or a series of data frames. In a list, the third element of component 1 may not have anything whatsoever to do with the third element of component 2. In a data frame, rows are related. If I were to hold recurring (Name, Salary, DateOfBirth) (i.e. character, integer and date values) in a list object, what would be the 'optimal' arrangement? Data frame. Would that be as the components of weekProfile above? Or will this be better. Either: personalDetail- list(rbind(c(Name,Salary,DateOfBirth),c(Name,Salary,DateOfBirth))); -- Sarah Goslee http://www.functionaldiversity.org __ 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.
Re: [R] R's list data structure
Hi Sarah, Thanks you for the clarifications; I had worked round the problem by switching to a data.frame. However, I am still unclear about 'list': as it exists, it must have a purpose. When is the use of the list data structure appropriate? [[alternative HTML version deleted]] __ 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.
Re: [R] R's list data structure
On Fri, Feb 17, 2012 at 3:37 PM, Ajay Askoolum aa2e...@yahoo.co.uk wrote: Hi Sarah, Thanks you for the clarifications; I had worked round the problem by switching to a data.frame. However, I am still unclear about 'list': as it exists, it must have a purpose. When is the use of the list data structure appropriate? I gave one example: storing lm() objects. Here's another: I'm doing a lot of spatial processing, and I read a single multispectral image into a list. Each list component is a SpatialGridDataFrame. That way each band from a single image is part of the same R object, and I can use lapply() to perform an operation on each band in turn. Using lists for things is a very Rish way of working, but it may take a while to get the hang of it. -- Sarah Goslee http://www.functionaldiversity.org __ 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.
Re: [R] R's list data structure
FWIW: Lists are a fundamental, universal, recursive data structure. All other data structures (i.e. r.e. sets) can be represented as lists. Indeed, one of the earliest high level (non-machine instructions) computer languages, McCarthy's LISP = List Processing, is based on lists. R was designed to be LISP-like (= a functional programming language) in some fundamentals ways. So it is no surprise that lists are widely used within R. Cheers, Bert On Fri, Feb 17, 2012 at 12:37 PM, Ajay Askoolum aa2e...@yahoo.co.uk wrote: Hi Sarah, Thanks you for the clarifications; I had worked round the problem by switching to a data.frame. However, I am still unclear about 'list': as it exists, it must have a purpose. When is the use of the list data structure appropriate? [[alternative HTML version deleted]] __ 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. -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm __ 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.