0

I'm having trouble wording what I want to do, so please bear with me.

I'm trying to extract information from lists and also use the data.table function. If I only have one data set, it is easy to just use the name of the elements, like so:

x = myList$first_val
y = myList$second_val

However, I want to write a function that is more generic. The problem is that R treats the variable itself as the name of the item to extract. Here is an example:

myFun <- function(data, independent_var, dependent_var)
{
  formula = paste(independent_var, "~", dependent_var)
  regression = lm(formula, data=data)

  # problem on the next two lines
  x = data$independent_var
  y = data$dependent_var

  plot(x,y)
  return(regression)
}

The data might look like this:

Market_Price   Sales_Price
10             12
11             11
14             19

An example function call might look like:

my_reg = myFun(prices, "Market_Value", "Sales_Price")

The problem is that R thinks that the variables, not their contents, are the names of the columns in the data. Similarly, I encounter a same problem when using data.frame.

myFun2 <- function(data, regression, variable, value, conf.level)
{
  # how the special case works
  # new = data.frame("Market_Price" = value)

  # how I want it to work
  new = data.frame(variable = value)

  CI = predict(regression, newdata = new, interval="confidence",level=conf.level)
  return(CI)
}
Alpha Bravo
  • 170
  • 12
  • Don't use `$`, use `[` or `[[`. – Gregor Thomas Aug 30 '16 at 20:45
  • 1
    R-Faq dupe: http://stackoverflow.com/q/18222286/903061 – Gregor Thomas Aug 30 '16 at 20:45
  • That solves the problem for accessing elements, thanks! However, that does not solve the problem for the data.frame. – Alpha Bravo Aug 31 '16 at 00:31
  • There are many ways to name a column of a data frame, `set.names`, `names(df) = ...`, or analogous to the dupe `df[, variable] = value` or `df[[variable]] = value` (requires first initializing to the correct number of rows). Basically the same thing. – Gregor Thomas Aug 31 '16 at 03:21
  • But I already have a data frame that has named columns. I am trying to select all values in a data frame column which equals some value. The problem is I can't choose the column from the variable. The methods you propose return errors. – Alpha Bravo Aug 31 '16 at 18:03
  • `column_name = "cyl"; value = 4; mtcars[mtcars[, column_name] == value]` – Gregor Thomas Aug 31 '16 at 18:11
  • And I don't see anything that looks like "*trying to select all values in a data frame column which equals some value*" in your question. – Gregor Thomas Aug 31 '16 at 18:13
  • In myFun2, the new = data.frame(variable = value) function call. – Alpha Bravo Aug 31 '16 at 18:29
  • `new = setNames(data.frame(value), variable)`. Or `new = data.frame(value); names(new) = variable`, as in my 3rd comment if you don't already have a data frame. If you do already have a data frame named `new`, then `new[, variable] = value` or `new[[variable]] = value` - also, as in my third comment. – Gregor Thomas Aug 31 '16 at 18:50
  • It's confusing because you say "*I already have a data frame that has named columns*", but the line you mention, `new = data.frame(...)` doesn't use an existing data frame with named columns, it creates a brand new data frame from scratch. – Gregor Thomas Aug 31 '16 at 18:54
  • Okay, this makes sense. Thanks, and I apologize for all of my incorrect statements and bad wording. – Alpha Bravo Aug 31 '16 at 20:18

0 Answers0