I have a list of more than 80 csv files, all with the same row headings and number of columns. This is the head of one of my csv files within the list:
Date Tx2m Tn2m Pr
1 1/1/1988 NA NA 0.0
2 1/2/1988 NA NA 0.0
3 1/3/1988 NA NA 0.0
4 1/4/1988 NA NA 0.0
5 1/5/1988 NA NA 0.0
I want to delete both the "Tx2m" and Tn2m" columns which in all of my files have Null values.
I tried running this code:
lapply(myList, function(x) { x["Tx2m"] <- NULL; x })
But I get the following error message: Error in x["Tx2m"] <- NULL : replacement has length zero.
How can I delete these columns and save them again as CSV files within my working directory?
It may be better to do this automatically by finding at least one non-NA element in a column and Filter
only those columns as list
elements could potentially differ in the NA contents
myList <- lapply(myList, function(x) Filter(function(u) any(!is.na(u)) u))
Or another option in tidyverse
library(dplyr)
library(purrr)
myList <- map(myList, ~ .x %>%
select(where(~ any(!is.na(.)))))