[R] Removing -Inf values from all the colums in a dataframe

Peter Ehlers ehlers at ucalgary.ca
Thu Feb 24 12:46:11 CET 2011


On 2011-02-23 19:27, Ramya wrote:
>
> Hi there,
>
> b is my dataframe.
>
> I have a dataframe. I am trying to get rid of the -INF values but didnt have
> much luck
>
> b[which(is.finite(b))]
>
> I can get rid of it for a single column
>
> b[,1][which(is.finite(b[,2]))] but not for all dataframe I used it inside of
> a sapply but still no dice
>
> my guess is it might have some differing row numbers and just ignoring the
> columns itself.
>
> Thanks
> Ramya

Note that R does not have "-INF" values.

I'm not sure that I understand what you mean by 'get rid of';
a reproducible example would help.

   b <- data.frame(x = 1:5, y = 11:15)
   b[2, 1] <- b[3, 2] <- -1/0
   b

Here are a couple of ways to delete all cases that
include a -Inf:
1.
   idx <- apply(b, 1, function(x) all(is.finite(x)))
   b[idx, ]

2.
   b[is.finite(rowSums(b)), ]

This assumes that all of your variables are numeric; if not,
you'll have to subset appropriately but, as I said, a reproducible
example would ....

Peter Ehlers



More information about the R-help mailing list