How to build model from index instead of name (see inside)?

Let's say I have this sample data:

Month Spend Sales
1 1000 9914
2 4000 40487
3 5000 54324

If I want to build a model I would do something like this:

dataset = read.csv("data-marketing-budget-12mo.csv", header=T)
simple.fit = lm(Sales~Spend, data=dataset)
summary(simple.fit)

but I want to be able to do it like this:

dataset = read.csv("data-marketing-budget-12mo.csv", header=T)
simple.fit = lm(dataset[3]~dataset[2], data=dataset)
summary(simple.fit)

All you have to do is use [[ to call the column you want and there is no need to set the data parameter.

1 Like

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.