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

Claus O'Rourke claus.orourke at gmail.com
Tue Mar 15 14:33:25 CET 2011


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



More information about the R-help mailing list