【问题标题】:Error in `[.data.frame`(x, by) : undefined columns selected`[.data.frame`(x, by) 中的错误:选择了未定义的列
【发布时间】:2021-05-27 17:31:30
【问题描述】:
     # Read reference data for normalization purposes
    setwd("../database")
    r_status <- read.csv("r_status.csv", stringsAsFactors = FALSE, na.strings = c("NA",""))
    r_status <- subset(r_status, select = c(status_id, status, status_datasource))
    
    t_location <- read.csv("t_location.csv", stringsAsFactors = FALSE, na.strings = c("NA",""))
    t_location$location <- t_location$location_description
    t_location$location <- as.numeric(gsub("^.*-", "", t_location$location))
    t_location <- t_location[!is.na(t_location$location),]
    t_location <- subset(t_location, select = c(location_id, location_description, location))
    
    # Read the database tables that store daily report data
    setwd("../database")
    t_dailyoutage <- read.csv("t_dailyoutage.csv", stringsAsFactors = FALSE, na.strings = c("NA",""))
    t_dailysensor <- read.csv("t_dailysensor.csv", stringsAsFactors = FALSE, na.strings = c("NA",""))  
     # Denormalize
        if(nrow(t_dailyoutage) > 0) {
                t_dailyoutage  <- join(t_dailyoutage, r_status, by = "status_id",) %>% 
                        join(t_location, by="location_id")
t_dailyoutage <-  t_dailyoutage[c("location", "status", "report_time", 
                                  "dailyoutage_inserted", "dailyoutage_inserted_by", "dailyoutage_updated")]
               
        }

运行上述代码时,我得到:

[.data.frame(x, by) 中的错误:选择了未定义的列

有人可以向我解释发生了什么以及为什么它不起作用吗?

【问题讨论】:

  • 请提供您的问题的最小可重现示例 - 有一个指南 here。特别是,请显示错误发生的行,这样我们就不必花费很长时间来挖掘您的代码来查找错误

标签: r


【解决方案1】:

你少了一个逗号。

t_dailyoutage <-  
  t_dailyoutage[c("location", "status", "report_time", 
                  "dailyoutage_inserted", "dailyoutage_inserted_by", "dailyoutage_updated")]

应该是

t_dailyoutage <-  
  t_dailyoutage[,c("location", "status", "report_time", 
                   "dailyoutage_inserted", "dailyoutage_inserted_by", "dailyoutage_updated")]

但我认为问题在于您试图选择一个不存在的列。你能检查你的数据的名称吗?

【讨论】:

    猜你喜欢
    • 2021-04-13
    • 1970-01-01
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-21
    • 1970-01-01
    相关资源
    最近更新 更多