0

I'm trying to run a k-fold cross-validation using data from an Excel spreadsheet. Every time I get to the "train" function, it breaks providing this error:

"Can't find column `x` in `.data`."

I'll provide my code below, but if someone could at least inform me on what exactly data is supposed to equal at least that would be a great start. In previous code, I just had data equal my Excel spreadsheet data.

setwd("C:\\Users\\Gerlitz\\Documents")

library(readxl)
x<-read_excel("test_data(copy).xlsx", sheet = "R", range = "B1:I481")
data("x")

set.seed(123)
train.control<- trainControl(method = "cv", number = 10)
model<-train(x$Ndif~0+x$`flmint soybean^2`+x$`spmaxt soybean`+x$`spprecip(mar)`+x$`flmint*harv`+x$`cyield^1.9`+x$`NrateA-50/1.11`+x$`spprecip*til`
             ,data = x, method = "lm", trControl = train.control)
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Ignore data("x"), it doesn't work but I forgot to comment it out for the purpose of this post. – Jade Gerlitz Aug 17 '19 at 19:21
  • [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making an R question that folks can help with. That includes a sample of data and all necessary code – camille Aug 17 '19 at 20:08

1 Answers1

0

You don't need to precede your variables with "x$". This sample code below works for the mtcars data set and should give you a sense of the proper format.

library(caret)
train.control<- trainControl(method = "cv", number = 10)
model<-train(mpg ~ cyl + wt + hp, data = mtcars, method = "lm", trControl = train.control)
model
Monk
  • 407
  • 3
  • 8