mercredi 29 novembre 2017

choosing data frames from within a list of data frames

I am trying to select data frames from within a long list of data frames, based on whether certain columns are empty. Here is a reproducible example, along with the code I have written to try to solve this problem:

d1 <- data.frame(a=rnorm(5), b=1:5, c=rnorm(5))
d2 <- data.frame(a=1:5, b=rnorm(5), c = c(NA, NA, NA, NA, NA))
d3 <- data.frame(a=1:5, b=c(NA, NA, NA, NA, NA), c=c(1:5))

my_test_data <- list(d1, d2, d3)
group_1 <- list()
group_2 <- list()

for (i in 1:length(my_test_data)) {
if(!is.nan(my_test_data[[i]]$b)) {
group_1[i] <- my_test_data[i]
}
else if (!is.nan(my_test_data[[i]]$c)) {
group_2[i] <- my_test_data[i]
}
else NULL
}

I get warning messages saying:

Warning messages: 1: In if (!is.nan(my_test_data[[i]]$b)) { : the condition has length > 1 and only the first element will be used

and group 1 and group 2 are identical to my_test_data

All help greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire