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?
Functions take pairlists as arguments. Generally lists are used to pass arguments to
latticegraphics, which was the dominant high-level graphics environment beforeggplotand descendants came along. Lists are also used to pass parameters to control arguments in many functions. In this case you would expect thexandylengths to be the same, so it could be a dataframe, but there is no particular reason to demand such a structure, and the base functionspointsandlineswill 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.