【问题标题】:R Code is getting undefined columns selected error but data column is presentR代码正在选择未定义的列错误,但存在数据列
【发布时间】:2018-03-26 22:59:34
【问题描述】:

我正在测试我在网上找到的一些 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

我的代码sn-p如下

    # 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 存在于我的数据框中。

我的数据列如下:

"Store","StoreType","Assortment","CompetitionDistance", "CompetitionOpenSinceMonth","CompetitionOpenSinceYear", "Promo2","Promo2SinceWeek","Promo2SinceYear","nPromoInteval", "StoreType_Assortment","Promo2_Year_Begin","Day_Promo2_Start", "Date_Promo2_Start","CompetitionDistance_Impute","CompetitionOpen_Impute", "DayOfWeek","Date","DayofDate","MonthofDate","YearofDate","Quarter","Sales", “客户”、“开放”、“促销”、“州假日”、“学校假日”、 "Competition_Start_Date","Day_bfat_Comp","Day_bfat_Promo2", "Flag_Day_bf_Comp","Flag_day_bf_Promo2","cluster"

【问题讨论】:

  • R# 是 C# 重构工具 Resharper 的快捷方式。你的意思可能是R,统计语言
  • 至于错误,已经很清楚了。您正在尝试使用文件中不存在的列名。您尚未发布文件示例,因此无法准确说出问题所在。 summary(rossman.df) 到底说了什么?什么是列?
  • Competition_Start_Date 出现在我的列名中,我现在已经提供了。
  • 您很可能正在寻找类似 @​​987654326@ 的东西,但如果没有可重现的示例,就很难判断。

标签: r validation


【解决方案1】:

Competition_Start_Date 存在于数据框中,但您没有告诉 R 去那里看,所以它在全局环境中寻找。

rossman.df[!is.na(rossman.df$Competition_Start_Date)]$Store

大概是你想要的吧?

【讨论】:

    猜你喜欢
    • 2014-07-03
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 2019-06-16
    • 2020-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多