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)
}