It makes no sense to me that approx
would return a list, since - if I understand it correctly - the two elements of the list that it returns are guaranteed to be numeric and of equal length. Given that, it would seem to make more sense to return an array or a data frame.
I'm writing some functions using approx
, and this makes me thing that I don't fully understand how it works. Is there a situation in which a list is necessary, or better?
Lists can have components of the same type or mode, or components of different types or modes. They can hence combine different components (numeric, logical…) in a single object. A Data frame is simply a List of a specified class called “data.
Data frame columns can contain lists You can also create a data frame having a list as a column using the data. frame function, but with a little tweak. The list column has to be wrapped inside the function I.
One difference is that if we try to get a single row of the data frame, we get back a data frame with one row, rather than a vector. This is because the row may contain data of different types, and a vector can only hold elements of all the same type. Internally, a data frame is a list of column vectors.
Therefore, we can use lapply function for this extraction. For example, if we have a list called LIST that store two data frames then column 3 of each data frame can be extracted by using the command lapply(LIST,"[",3).
Functions take pairlists as arguments. Generally lists are used to pass arguments to lattice
graphics, which was the dominant high-level graphics environment before ggplot
and descendants came along. Lists are also used to pass parameters to control arguments in many functions. In this case you would expect the x
and y
lengths to be the same, so it could be a dataframe, but there is no particular reason to demand such a structure, and the base functions points
and lines
will accept named lists and properly allocate them to their argument lists.
(I do not think a matrix could get properly distributed to the x and y arguments of the base plotting routines even of there were row or column names that matched. Matrices are really folded vectors.)
So I think the answer is that passing lists is most consistent with how functional programming is implemented in R.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With