Skip to content
Prev 275642 / 398506 Next

help with paste

Hi Sharad,

See ?paste, particularly the 'collapse' argument:
[1] "LEPTIN + SAA + PTH"

If this is going to ultimately end up in regression (my spidey sense
is tingling), the follow up question you will have is something like:

"I have a bunch of string variables that I am trying to use in a
formula in lm() but it isn't work".

then there will be a series of possible hacks to shoehorn character
data into the formula.  Here is a pre-emptive suggestion (of many
possibilities) using the mtcars dataset in R.  Suppose 'mpg' is always
the outcome, and then you want all sets of 2 predictors:

## make a data frame where each column represents all the variables
for one model
varset <- data.frame(rbind("mpg", combn(colnames(mtcars)[-1], 2)),
stringsAsFactors = FALSE)

## looks like this:
X1  X2   X3
1  mpg mpg  mpg
2  cyl cyl  cyl
3 disp  hp drat

## fit a separate model of mpg regressed on every other variable in the dataset
## but subset mtcars by each column of varset
## store results in one big list

allmodels <- lapply(varset, function(i) {
  lm(mpg ~ ., data = mtcars[, i])
})

## extract all coefficients into a matrix
sapply(allmodels, coef)

note that with this general method I am proposing, all models need not
have the same number of predictors.  I just did it for convenience so
I could use the combn() function to generate a bunch of combinations.

Cheers,

Josh
On Wed, Oct 26, 2011 at 6:03 PM, 1Rnwb <sbpurohit at gmail.com> wrote: