Kevin Chang
2012-Aug-07 19:06 UTC
[R] What is this called? lapply(datum,"[[","ColumnName")
Hello R users I recently learned how to use this command: lapply(datum,"[[","ColumnName") Unfortunately, I don't know how exactly it works, what it's called (in particular the "[[" part], and what other things you can do with it (retrieve multiple columns?). Given datum is a list of dataframes with the same column, but different number of rows, lapply(datum,"[[","ColumnName"), Returns you a list of a particular column form each of your dataframes from datum. I think this is a really useful command, and would like to learn more about it. Unfortunately I don't know what subject or topic it is under and cannot look it up in a book. Thank you, Kevin Master of Science Student Department of Food, Resource and Agricultural Economics University of Guelph Office: 519-824-4120 ext. 58528 Mobile: 226-979-2813 <http://fare.uoguelph.ca/users/kchang01> http://fare.uoguelph.ca/users/kchang01 [[alternative HTML version deleted]]
R. Michael Weylandt
2012-Aug-07 21:07 UTC
[R] What is this called? lapply(datum,"[[","ColumnName")
On Tue, Aug 7, 2012 at 2:06 PM, Kevin Chang <kchang01 at uoguelph.ca> wrote:> Hello R users > > > > I recently learned how to use this command: > > > lapply(datum,"[[","ColumnName") > > > > Unfortunately, I don't know how exactly it works, what it's called (in > particular the "[[" part], and what other things you can do with it > (retrieve multiple columns?). > > > Given datum is a list of dataframes with the same column, but different > number of rows, > > > > lapply(datum,"[[","ColumnName"), > > > > Returns you a list of a particular column form each of your dataframes from > datum. > > > > I think this is a really useful command, and would like to learn more about > it. > Unfortunately I don't know what subject or topic it is under and cannot look > it up in a book. > >To see the help page, type ? `[[` at your R prompt. Long story short, when you use syntax like x[3], that's just sugar for `[`(x, 3) which tells R to apply the `[` function with arguments "x" and "3". So when you pass syntax like that to lapply(), it applies the function `[`( ??, colNames) to each element of your list datum in turn putting them in the slot I marked with double question marks. It's not so different than something like lapply(datum, `+`, 3) to add 3 to each element of datum. This could alternatively be written as lapply(datum, function(x) x + 3) and your example could just as easily (and I think more clearly) be written as lapply(datum, function(x) x[[colName]]) Best, Michael> > Thank you, > > > > Kevin > > > > Master of Science Student > > Department of Food, Resource and Agricultural Economics > > University of Guelph > Office: 519-824-4120 ext. 58528 > > Mobile: 226-979-2813 > > <http://fare.uoguelph.ca/users/kchang01> > http://fare.uoguelph.ca/users/kchang01 > > > > > > > [[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.
Bert Gunter
2012-Aug-07 21:23 UTC
[R] What is this called? lapply(datum,"[[","ColumnName")
"I... cannot look it up in a book." Baloney! Read the R Language Definition manual section on indexing in the "Evaluation of Expressions" Chapter. Then ?lapply and ?"[[" -- and the relevant sections in "The Introduction to R" manual. However, I will admit that it's tricky. The point to grasp -- described at various levels of detail in the above manuals and in numerous other resources that you can check out on the CRAN website -- is that y[["a"]] is just syntactic sugar for "[["(y,"a") . R is a "functional" language (mostly). So your lapply statement is just lapplying the "[[" function(extraction operator ) to the list of data frames. Cheers, Bert On Tue, Aug 7, 2012 at 12:06 PM, Kevin Chang <kchang01 at uoguelph.ca> wrote:> Hello R users > > > > I recently learned how to use this command: > > > lapply(datum,"[[","ColumnName") > > > > Unfortunately, I don't know how exactly it works, what it's called (in > particular the "[[" part], and what other things you can do with it > (retrieve multiple columns?). > > > Given datum is a list of dataframes with the same column, but different > number of rows, > > > > lapply(datum,"[[","ColumnName"), > > > > Returns you a list of a particular column form each of your dataframes from > datum. > > > > I think this is a really useful command, and would like to learn more about > it. > Unfortunately I don't know what subject or topic it is under and cannot look > it up in a book. > > > > Thank you, > > > > Kevin > > > > Master of Science Student > > Department of Food, Resource and Agricultural Economics > > University of Guelph > Office: 519-824-4120 ext. 58528 > > Mobile: 226-979-2813 > > <http://fare.uoguelph.ca/users/kchang01> > http://fare.uoguelph.ca/users/kchang01 > > > > > > > [[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.-- 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