0
     # 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")]
               
        }

When running the above code I get:

Error in [.data.frame(x, by) : undefined columns selected

Can someone explain to me what is going on and why it isn't working?

Kyle
  • 1
  • 1
  • 1
    please provide a minimal reproducible example of your problem - there is a guide [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). In particular, please show the line which the errors occur on so we don't have to spend ages digging through your code to find the error – user438383 May 27 '21 at 17:40

1 Answers1

0

You're missing a comma.

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

Should be

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

But I think the problem is you're trying to choose a column that doesn't exist. Can you check the names of your data?