I'm adding a set of dummy variables as columns and trying to reference them in a regression later on. Below is the code, that I believe is giving me trouble. Any help is appreciated.
data = read.csv("path_to_file.csv")
data = data[!(data["Column1"]==-1),]
# This is done a few times dropping bad rows from the data
data$new_column1 = data$old_column*2
# I add some new columns based on old ones for use later on
data = cbind(data, dummy(factor(data$year), sep="_"))
# This is where I believe the problem is, but I don't know what it could be
data$column1_dmean = data$column1 - mean(data$column1)
lm_reg = lm(formula = dep_var ~ controls+new_column1+column1_dmean+data_year_1
+ data_year_2 ... + data_year_17, data=data)
The code will run fine and is able to run in sequence one block after another, however, when I attempt to knit this it halts attempting to run the lm function. Interestingly it has no issue finding the new_column1 or the column1_dmean variable in data, but it is unable to find the data_year_n variables.
This is what leads me to believe that the cbind function is my issue, but I've not been able to find any issues with this function either. I'm fairly new to R and haven't seen this sort of issue posted before where I have the data in the dataset and it runs in sequence fine. When I run in sequence and look at the data it can find the variable and I see it in the data, but knitting will not run.
Any help is appreciated, thank you.
I've added reproducible code here, per request of a comment! I hope this helps.
library(dummies)
data = data.frame(A = c(1,2,3,4,5,1,2,3,4,5),
B = c(1979, 1979, 1979, 1979, 1979, 1980, 1980, 1980, 1980, 1980),
C = c(0.1,0.2,-1,0.4,0.5,0.1,0.2,0.3,0-1,0.5),
D = c(2,3,8,4,5,6,1,3,5,4))
data = data[!(data["C"]==-1),]
data$new_C = data$C*2
data = cbind(data, dummy(factor(data$B), sep='_'))
lm_reg = lm(formula = D ~ B+new_C+data_1979+data_1980, data=data)
summary(lm_reg)
This code runs fine if I just run the block, but will not knit giving the following error message.
Quitting from lines 15-31 (test_file_will_delete.Rmd) Error in eval(predvars, data, env) : object 'data_1979' not found Calls: ... eval -> <Anonymous> -> model.frame.default -> eval -> eval Execution halted
dput()Please also share the exact error message you get when knitting. - stragu