[R] R enquiry: Replace blank spaces with a character value
Marco Geraci
marcodoc75 at yahoo.com
Mon Sep 18 19:07:10 CEST 2006
Dear Bob,
It's not clear to me what you want to do. You provided
my example, which I already know, and you didn't
provide yours.
Marco
--- Bob Green <bgreen at dyson.brisnet.org.au> wrote:
> Marco,
>
> I saw your January e-mail to an enquiry about
> filling in missing values
> with a zero I tried adapting the various syntax
> suggestions without much luck.
>
> If I have a dataframe called test, which contains 2
> variables (var1 & var2)
> which contain blank spaces. I want replace blank
> spaces with the value
> 'n'. How can I do this using the syntax you provided
> (see below):
>
>
> Any assistance you can offer is appreciated,
>
> Bob
>
>
> foo <- data.frame(x=1:3,y=letters[1:3],z=4:6,
> w=as.Date(c("02/27/92",
> "02/27/92", "01/14/92"), "%m/%d/%y")) foo[2,] <- NA
>
> foo
> x y z w
> 1 1 a 4 1992-02-27
> 2 NA <NA> NA <NA>
> 3 3 c 6 1992-01-14
>
> class(foo$w)
> [1] "Date"
>
> mode(foo$w)
> [1] "numeric"
>
> a <- sapply(foo, is.numeric)
> b <- !sapply(foo, class)=="Date"
>
> foo[!complete.cases(foo),a & b] <- 0
>
> foo
> x y z w
> 1 1 a 4 1992-02-27
> 2 0 <NA> 0 <NA>
> 3 3 c 6 1992-01-14
>
>
More information about the R-help
mailing list