Re: [R] R enquiry: Replace blank spaces with a character value

From: Marco Geraci <marcodoc75_at_yahoo.com>
Date: Tue 19 Sep 2006 - 03:07:10 EST


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

> 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
>
>



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 and provide commented, minimal, self-contained, reproducible code. Received on Tue Sep 19 03:13:18 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 19 Sep 2006 - 06:01:36 EST.

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