Re: [R] How to delete an entire row, if a specific colum has the value of "#VALUE!"

From: David Winsemius <dwinsemius_at_comcast.net>
Date: Sat, 23 Apr 2011 03:00:13 -0400

On Apr 23, 2011, at 2:49 AM, David Winsemius wrote:

>
> On Apr 22, 2011, at 10:35 PM, empyrean wrote:
>
>> I am importing CSV data with thousands of rows, if any row contains
>> an error
>> from excel, the whole program crashes, so i need to delete all rows
>> with the
>> value of #VALUE!, all other values are non-numeric...
>>
>> I've tried a bunch of strategies, but nothing seems to work.
>
>
>
> A set of error targets:
> > lines <- textConnection("a,#NAME?,b
> + #DIV/0!,b,v
> + #VALUE!,bb,nn")
> > dat <- read.table(lines, sep="," , header=FALSE, comment="")
> > dat
> V1 V2 V3
> 1 a #NAME? b
> 2 #DIV/0! b v
> 3 #VALUE! bb nn
>
> # This will turn all the #VALUE!'s into <NA>'s
> > is.na(dat) <- dat == "#VALUE!"
> > dat
> V1 V2 V3
> 1 a #NAME? b
> 2 #DIV/0! b v
> 3 <NA> bb nn

If you still want to remove rows rather than use R's missing value facilities, here's a way to eliminate the rows in that result with "#NAME?", namely to only return the rows that don't have it and then to rbind the results:

 > do.call("rbind", apply(dat,1,function(x) if (!"#NAME?" %in% x) {x} ) )

      V1 V2 V3
[1,] "#DIV/0!" "b" "v"
[2,] NA "bb" "nn"

(You need to learn to read R code from the inside out.)

David Winsemius, MD
West Hartford, CT



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Sat 23 Apr 2011 - 07:02:12 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 23 Apr 2011 - 10:30:32 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive