我正在测试我在网上找到的一些R代码,如果有人能告诉我为什么会遇到这个错误,我会非常感激。
对于R来说,我是新手,所以请原谅我缺乏知识。
我得到的错误如下。
> temp <- lapply(unique(rossmann.df[!is.na(Competition_Start_Date)]$Store), beforeAndAfterComp)
Error in `[.data.frame`(rossmann.df, !is.na(Competition_Start_Date)) :
undefined columns selected
我的代码段如下
# Competition effect over time
library(car)
library(data.table)
library(zoo)
library(forecast)
library(ggplot2)
#Read dataset
rossmann.df<-read.csv(paste("TrainCluster.csv", sep=""))
View(rossmann.df)
dim(rossmann.df)
#Create a descriptive statistics (min, max, median etc) of each variable.
summary(rossmann.df)
timespan <- 100
# Days to collect before and after Opening of competition
beforeAndAfterComp <- function(s) {
x <- rossmann.df[Store == s]
daysWithComp <- x$CompetitionOpenSince >= x$DateYearmon
if (any(!daysWithComp)) {
compOpening <- head(which(!daysWithComp), 1) - 1
if (compOpening > timespan & compOpening < (nrow(x) - timespan)) {
x <- x[(compOpening - timespan):(compOpening + timespan), ]
x$Day <- 1:nrow(x)
return(x)
}
}
}
temp <- lapply(unique(rossmann.df[!is.na(Competition_Start_Date)]$Store), beforeAndAfterComp)
为什么我收到此错误?我确认 Competition_Start_Date存在于我的数据框中。
我的数据栏如下:
“存储”, “的storetype”, “拼盘”, “CompetitionDistance”, “CompetitionOpenSinceMonth”, “CompetitionOpenSinceYear” “Promo2”, “Promo2SinceWeek”, “Promo2SinceYear”, “nPromoInteval” “StoreType_Assortment”, “Promo2_Year_Begin”, “Day_Promo2_Start” “Date_Promo2_Start”, “CompetitionDistance_Impute”, “CompetitionOpen_Impute” “星期几”, “日期”, “DayofDate”, “MonthofDate”, “YearofDate”, “季”, “销售”, “客户”, “开放”, “促销”, “StateHoliday”, “SchoolHoliday” “的 Competition_Start_Date ”, “Day_bfat_Comp”, “Day_bfat_Promo2”, “Flag_Day_bf_Comp”, “Flag_day_bf_Promo2”, “簇”
答案 0 :(得分:0)
Competition_Start_Date出现在数据框中,但你没有告诉R看那里,所以它正在寻找全球环境。
rossman.df[!is.na(rossman.df$Competition_Start_Date)]$Store
可能是你想要的?