Artur
Artur

Reputation: 695

List that contains some dataframes with NaN values. How to remove them?

I want to make a single dataframe out of a list dataframes that contains a single row. I tried to binding them together to remove the NaN values but you can only bind if there is no NaN values!

What I got:

[[1]]
[1]          NaN 9.840158e+17

[[2]]
[1]          NaN 9.838244e+17

[[3]]
[1] 6.842105e-01 9.837743e+17

[[4]]
[1] 2.527174e+00 9.837643e+17

[[5]]
[1] 1.168269e+00 9.836988e+17

[[6]]
[1]         NaN 9.83663e+17

What I want:

[1]
      impact           id
6.842105e-01 9.837743e+17
2.527174e+00 9.837643e+17
1.168269e+00 9.836988e+17

What I tried:

bind_rows(data, .id = "id")

Error: Argument 1 must have names

for reproduction of the problem:

list(c(NaN, 984015782521835008), c(NaN, 983824424532144000), 
c(0.684210526315789, 983774270886236032), c(2.52717391304348, 
983764328443796992), c(1.16826923076923, 983698762760704000
), c(NaN, 983662953894435968))

Any tips on how to solve this?

Upvotes: 2

Views: 239

Answers (2)

Arun kumar mahesh
Arun kumar mahesh

Reputation: 2359

When you want to remove NA's from dataframe you can use functions

data

DF <- data.frame(x = c(1, 2, 3), y = c(0, 10, NA))

na.omit(DF)

print (DF)
x  y
1  0
2 10

na.exclude(DF)

print(DF)
x  y
1  0
2 10

Upvotes: 0

akrun
akrun

Reputation: 886948

We remove the list element having NaN and then rbind the elements

out <- do.call(rbind, lst1[!sapply(lst1, function(x) any(is.nan(x)))])
colnames(out) <- c("impact", "id")
out
#       impact           id
#[1,] 0.6842105 9.837743e+17
#[2,] 2.5271739 9.837643e+17
#[3,] 1.1682692 9.836988e+17

Or another option is to rbind the elements and then use na.omit

na.omit(do.call(rbind, lst1))

Or with Filter

do.call(rbind, Filter(function(x) !any(is.nan(x)), lst1))

Or using discard (from purrr)

library(purrr)
discard(lst1, ~ any(is.nan(.x))) %>%
        do.call(rbind, .)

Upvotes: 1

Related Questions