R Language: Counting the Number of Completely Observed Cases in Air Quality Data

0

complete <- function(directory, id = 1:332) {

  # Get full path of the specsdata folder.

  directory <- paste(getwd(), "/", directory, "/", sep = "")


  # Initialize data frame.

  data <- data.frame()


  # For each id passed as parameter:

  for (i in id) {

    # Read the file.

    file_dir <- paste(directory, i, ".csv", sep = "")

    file_data <- read.csv(file_dir)


    # Count the number of complete cases.

    nobs <- sum(complete.cases(file_data))


    # Add data to data frame.

    data <- rbind(data, c(i, nobs))

  }


  # Rename columns.

  colnames(data) <- c("id", "nobs")


  # Return data frame.

  return(data)

}

This function takes two arguments: `directory` and `id`. The `directory` argument specifies the directory where the data files are located. The `id` argument specifies a vector of monitor ID numbers.

The function first gets the full path of the `specdata` folder. Then, it initializes a data frame. For each id in the `id` vector, the function reads the corresponding data file and counts the number of complete cases. Finally, the function adds the data to the data frame and returns it.

Here is an example output from the `complete` function:

complete("specdata", 1:10)

# id nobs

# 1  1 932

# 2  2 711

# 3  3 475

# 4  4 338

# 5  5 586

# 6  6 463

# 7  7 672

# 8  8 627

# 9  9 729

# 10 10 817

Post a Comment

0Comments
Post a Comment (0)