Remove NA value within a list of dataframes

remove na in r
r remove rows with na in one column
r remove na from list
na omit list r
r replace missing values with na
dplyr remove na columns
na value in chemistry
r remove rows with all na

I'm sure there is a very easy answer to this but I can't find one. In a separate post, How do I remove empty data frames from a list? I have looked at removing an empty data frame from a list of data frames.

But how can you do this when one of the items in the list isn't classified as a data frame and is just a NA value? Modifying the parameters of the question above slightly, you have:

M1 <- data.frame(matrix(1:4, nrow = 2, ncol = 2))
M2 <- NA
M3 <- data.frame(matrix(9:12, nrow = 2, ncol = 2))
mlist <- list(M1, M2, M3)

I would like to remove M2 in this instance, but I have several examples of these empty data frames so I would like a function that removes them all simultaenously.

I have tried a couple of solutions to the question above which do not work:

mlist[sapply(mlist, function(x) dim(x)[1]) > 0]##Error message -
##Error: (list) object cannot be coerced to type 'double'

Filter(function(x) dim(x), mlist) ###Incorrect outputs

Thank you in advance for any help!


One option is to use Filter to check wheter the list elements are data.frames

Filter(is.data.frame, mlist)
#[[1]]
#  X1 X2
#1  1  3
#2  2  4

#[[2]]
#  X1 X2
#1  9 11
#2 10 12

How to remove NA from data frames of a list?, na.omit() can be used on data frames to remove any rows that contain NA values. We can use lapply() to apply it over my.list . lapply(my.list  remove data frame from list of data frames. I have a list of data frames like this: a<- data.frame (x=runif (10), y = runif (10), Acc = 1) b<- data.frame (x=runif (10), y = runif (10), Acc = 0) ls<- list (a,b) and I want to remove the data frames from ls that have Acc values other than 1. How do I do that?


Here's a slightly different way to get your result

library(tidyverse)

M1 <- data.frame(matrix(1:4, nrow = 2, ncol = 2))
M2 <- NA
M3 <- data.frame(matrix(9:12, nrow = 2, ncol = 2))
M4 <- NA
mlist <- list(M1, M2, M3,M4)

indexes <- tibble()

for (i in 1:length(mlist)) {

  if (is.na(mlist[[i]]) == TRUE) {

    new_index <- tibble(index = i)

    indexes <- bind_rows(new_index,indexes)

  }
}

indexnums <- indexes %>% pull(index)

mlist <- mlist[-indexnums]

With this, you check if each list element is NA or not, then add the index number to a table if it is, then you pull those index numbers out and subset the list. If you have a lot of these in your data set this should remove them all.

How to remove NA values with dplyr::filter(), Then provide the list of positions to filter function? answered Aug 5, 2019 by How to remove rows with missing values (NAs) in a data frame? With my HIBPwned package, I consume the HaveIBeenPwned API and return back a list object with an element for each email address. Each element holds a data.frame of breach data or a stub response with a single column data.frame containing NA. Elements are named with the email addresses they relate to. I had a list of data.frames and I wanted a consolidated data.frame (well, I always want a data


Hope to help you.

# Method 1
mlist[!is.na(mlist)]

# Method 2
replace(mlist, is.na(mlist), NULL)

Dealing with Missing Values · UC Business Analytics R , In R, missing values are often represented by NA or some other value that represents by NA . is.na() will work on vectors, lists, matrices, and data frames. Hello Everyone! Would you help solving the following problem. I want to create a list made up of several dataframes, access each dataframe through a loop and once a dataframe has been reached, use another loop to access the column(s) of that datafra


Exploring and cleaning dataframes, Removing rows with NA values​​ omit function to return just the rows that have complete cases. We will create a synthetic example to demonstrate the na. omit() function. Note the NA values in the dataframe. fillna() accepts a value, and will replace any empty cells it finds with that value instead of dropping rows: df = df.fillna(0) display(df) fillna() also accepts an optional subset argument, much like dropna(). Dropping Duplicate Rows. Another top-10 method for cleaning data is the dropduplicates() method.


Subsetting · Advanced R., A missing value in the index always yields a missing value in the output: Because data frames are lists of columns, you can use [[ to extract a column from any missing values will be put at the end of the vector; however, you can remove  Note the index values on the other axes are still respected in the join. keys: sequence, default None. Construct hierarchical index using the passed keys as the outermost level. If multiple levels passed, should contain tuples. levels: list of sequences, default None. Specific levels (unique values) to use for constructing a MultiIndex.


9 Subsetting R Objects, The $ operator is used to extract elements of a list or data frame by literal name. A common task in data analysis is removing missing values ( NA s). > x <- c(1  Many times while cleaning the data, we encounter NA values which are nothing but missing values in the dataset and can produce incorrect results. And most of the times we want to exlude NA values