Re: [R] removing NA from a data frame

From: Haifeng Xie <xieh_at_wmin.ac.uk>
Date: Sat 18 Mar 2006 - 10:25:34 EST

If I understand it correctly, something like this should do what you want

x[!apply(x, 1, function(y) any(is.na(y)), ]

where x is the dataframe in question.

Hope that helps.

Kevin

> Sam Steingold <sds <at> podval.org> writes:
>
>>
>> Hi,
>> It appears that deal does not support missing values (NA), so I need to
>> remove them (NAs) from my data frame.
>> how do I do this?
>> (I am very new to R, so a detailed step-by-step
>> explanation with code samples would be nice).
>
> If you wanted to remove rows with NAs from data frame X
> na.omit(X) would do it.
>
> In this case I think
>
> X[!sapply(X,function(z)any(is.na(z)))]
>
> should work, although I haven't tested it.
> function(z)any(is.na(z)) looks for any NA values
> sapply applies the function to each element
> in the list (= column in the data frame) and
> returns a vector
> ! negates the logical vector
> [] picks the appropriate elements (=columns) out
> of the list (=dataframe)
>
> I haven't tested it.
> Conceivably
>
> X[!sapply(is.na(X),any)]
>
> or
>
> X[sapply(!is.na(X),all)]
>
> would work too, although I'm not sure.
>
> Ben
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Sat Mar 18 10:45:12 2006

This archive was generated by hypermail 2.1.8 : Sat 18 Mar 2006 - 12:09:13 EST