Error: object of type 'closure' is not subsettable

rstudio
caret

#1

Hey so I've ran the following code from a dataset I loaded. I keep getting the same error on the last 2 lines and am not sure how to fix it. Any suggestions?

setwd("Downloads")
pitcher = read.csv("FanGraphs Leaderboard (27).csv",fileEncoding = "UTF-8-BOM")
attach(pitcher)
str(pitcher)
cor(subset(pitcher,select=-c(Season,Name,Team,playerid)))
goodColumns = c("W","L","IP","ER","HR","BB","SO","WAR")
library(caret)
inTrain = createDataPartition(pitcher$WAR,p=0.7,list=FALSE)
training <- data[inTrain,goodColumns]
testing = data[-inTrain,goodColumns]

Thanks


#2

Hi @mikeozga! Welcome!

Your code hasn't actually created an object named data, so R is falling back to the function data() (used for loading built-in datasets). A function is called a "closure" by R.

Were you maybe adapting this code from an example somewhere? I'm not quite sure what you're doing here and I can't run your code (it's not reproducible as-is, since it relies on files local to your system), but I suspect you need to have the name of your data frame pitcher in the place of data.

To learn more about how to make your code questions as easy as possible to answer, take a look at this FAQ:


#3

One small tip, unrelated to your question: using attach() on a data frame can create a lot of confusion, since it basically adds invisible variable names to your session. This code isn't even gaining any convenience from attach()-ing, since you're using subset() (which already lets you use bare column names) or fully qualifying your columns (e.g., pitcher$WAR). So I'd recommend skipping the attach(). It's likely to create more pain than it's worth.