the lenght(prec_vec) gives 0 And it should give 366

Hello I am new user of R. I try to creat a df but the lenght here gives me 0 and it should give 366. I don't know why.

# file.exists("D:/Preparation data/weather data/P_D//file_name")
#### Precipitation

# Precipitation directory
setwd("D:/Preparation data/weather data/")
prec_dir <- "D:/Preparation data/weather data/P_D/era5_prec_2020/"

# Check number of days in the year
days_in_year(year)

# Load precipitation for the year
prec_csv_files <- list.files(prec_dir)
prec_all = list()
for (i in 1:length(prec_csv_files)) {
prec <- read.csv(paste0(prec_dir,"prec", year, ".csv"))
}

# Set the key
prec$key <- paste0(gsub( " ", "_", prec$Province), "_", prec$fid)

prec_colnames <- grep("P_", names(prec), value=TRUE)


# Filter by key
prec <- prec[prec$key == key, prec_colnames]
prec_vec <- as.vector(t(prec[, prec_colnames]))
length(prec)
#remove unwanted data
rm(prec_dir,prec)

Well, without looking at the data, it's hard to tell. The most obvious possibility is key, that you don't define in this code, but then you filter to keep only the rows where prec$key == key, so if key has the wrong value prec has 0 rows left after filtering.

Before filtering, take a look at head(prec) and key and ensure they make sense.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.