[R] Newbie-ish question on iteratively applying function to dataframe

From: Claus O'Rourke <claus.orourke_at_gmail.com>
Date: Tue, 15 Mar 2011 13:33:25 +0000


Hi,
I am trying to recursively apply a function to a selection of columns in a dataframe. I've had a look around and from what I have read, I should be using some version of the apply function, but I'm really having some headaches with it.

Let me be more specific with an example.

Say I have a data frame similar to the following

A x y z r1 r2 r3 r4

0.1  0.2  0.1 ...
0.1  0.3 ...
0.2 ...

i.e., a number of columns, each of the same length, and all containing real numbers. Of these columns, I want to model one variable, say A, as a function of other variables, say x, y, z, and any one of my r1, r2, r3, ... variables.

i.e., I want to model
A ~ x + y + z + r1
A ~ x + y + z + r2
....
A ~ x + y + z + rn

But where the number of 'r' variables I will have will be large, and I don't know the specific number of these variables in advance.

My question first is, how can I select all the columns in a dataframe that have a heading that matches a string pattern?

And then related to this, what would be the best way of repeatedly applying my modelling function to the result?

Many thanks for any help for this occasional R armature.

Claus



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. Received on Tue 15 Mar 2011 - 13:46:54 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 15 Mar 2011 - 16:10:22 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive