0

I have an R script which computes several linear models but also ggplots etc. Now I want to run it with changing variables.

Depending on my specifications df$targetX should be interpreted as:

df$target_37

and

lm(targetX ~ kelvin, df) should compute a linear model like I called:

lm(target_37 ~ kelvin, df)

I experimented with formula(), paste(), parse() and eval() without success. Is there a way I can specify targetX to achieve this and without having to touch the analysis script?

targetX <- smartCommandIdontKnow("target_37")
florian
  • 604
  • 8
  • 31
  • 2
    R is not a macro based language. It is a functional language and ideally you would use functions where you can pass parameters to get the behavior you want. A find and replace type solution would not work for both indexing and formulas because they are very different language structures/functions. – MrFlick Nov 07 '17 at 16:50
  • Partial duplicate: https://stackoverflow.com/questions/18222286/dynamically-select-data-frame-columns-using-and-a-vector-of-column-names – MrFlick Nov 07 '17 at 16:52
  • 1
    Partial duplicate: https://stackoverflow.com/questions/17024685/how-to-use-reference-variables-by-character-string-in-a-formula – MrFlick Nov 07 '17 at 16:54
  • Hmm in my case I would have to define many functions as I want to return so many objects. I would prefer sourcing my script.R file instead. – florian Nov 07 '17 at 17:02
  • also, can you post what error you're getting? also, sample data is helpful so we can try and replicate – ike Nov 07 '17 at 17:03
  • I rewrote the above post trying to clarify my problem. – florian Nov 07 '17 at 17:20
  • Possible duplicate of [How to use reference variables by character string in a formula?](https://stackoverflow.com/questions/17024685/how-to-use-reference-variables-by-character-string-in-a-formula) – Rui Barradas Nov 07 '17 at 19:47

3 Answers3

0

How you might solve this in base R and the tidyverse (ggplot2) are generally different. Without knowing what you're trying to do, one option is to look up the column number using which(colnames(df)==targetX) and rename the column in the function to a generic one used by the function. e.g. (long hand to help comprehension):

smartCommandIdontKnow = function(colN){
  x=df
  id = which(colnames(x)==colN)
  colnames(x)[id] = "col_X"

  lm(col_X ~ kelvin, x)
}
MikeRSpencer
  • 1,276
  • 10
  • 24
0

Here is one minimal suggestion using a for-loop. I think that you may be getting hung up on the magic $-notation way of referring to variables. Remember that df[["variable"]] is equivalent to df$variable.

df <- data.frame('target_37'= 1:5, 
                 'target_38' = 2*(1:5),
                 'target_39'= 3*(1:5),
                 'kelvin'= 4*(1:5))

# Independent vars
var_list = c("target_37", "target_38", "target_39") 

for(var in var_list){
  print(df[[var]])  # Do whatever it was you wanted with your df
  lm(df[[var]] ~ kelvin, df) 
}

My answer is sort of a guess since you didn't exactly provide enough code to know exactly why you're mentally stuck. Despite my answer using a for-loop I would also strongly recommend that you take MrFlick's advice for future scripts. R's functional approach if very powerful and for-loops can get pretty kludgy/unreadable, pretty fast.

HFBrowning
  • 2,196
  • 3
  • 23
  • 42
-2

You're making targetX a variable, yes, but you're not making it a part of the data table. you have to join it to that table for your call to work. something like

df$target_37 <- 'target_37'

will do.

ike
  • 342
  • 3
  • 17
  • I see what you're trying to say, but that doesn't solve the problem the OP is having – KenHBS Nov 07 '17 at 16:56
  • oh? seems like it should. there are no error messages or sample data so I don't see how you can say it doesn't. either way I don't see how that deserves a down vote... – ike Nov 07 '17 at 17:04
  • I didn't down vote. In your answer, you are proposing to regress `df$kelvin` (which contain numeric data) on a string `"target_37"`. You cannot regress some numeric values on a *string*. The problem in the question, is that the somehow `R` needs to be told to not interpret `target_37` as a *string*, but as the column `df$target_37`, which contains numeric data – KenHBS Nov 07 '17 at 17:11
  • for the record, I wasn't proposing anything about the regression: the question (now modified to remove the relevant parts) asked how to assign the text 'target_37' to the variable name. I answered that accurately. – ike Nov 07 '17 at 21:26
  • No, you didn't. He's asking for `lm(target_37 ~ kelvin, df)`, you are giving him `lm("target_37" ~ kelvin, df)`, that's something completely different – KenHBS Nov 07 '17 at 21:29
  • again, in the deleted part of the question he said he wanted to assign the value to a column. it's been modified, but my answer was to what was originally asked. – ike Nov 08 '17 at 20:49