[R] enter "missing" into missing fields

Jim Lemon jim at bitwrit.com.au
Thu Nov 5 03:54:10 CET 2009


On 11/05/2009 06:40 AM, frenchcr wrote:
> if ive got an incomplete data set thats got thousands of rows and 80 columns
> with random missing fields...like this say...
>
> 3 b 3
> 4    1
> 1 x 2
>
> ? how do i turn it into....
>
> 3    b       3
> 4 missing  1
> 1    x       2
>
> ...i.e., i want to insert the word "missing" into the fields that are empty?
>    
Hi frenchcr,
It is easy to replace fields, but you have to know whether they are 
blank (as in a character field with nothing in it), NULL (nothing there) 
or NA (data Not Available).

x[nchar(x)==0]<-"missing"
x[is.null(x)]<-"missing"
x[is.na(x)]<-"missing"

will handle those three respectively. However, your example looks like 
you have whitespace in the "empty" fields. If that is so, you may have 
to work out what is in the field and then match it with something like:

x[x == "  "]<-"missing"

I am very glad that you didn't want to insert

"This space intentionally left blank"

as in those pages in books that might have delighted ancient Greek 
philosophers but merely annoy the logical reader.

Jim




More information about the R-help mailing list