Re: [R] substitute values

From: Sergio Della Franca <sergio.della.franca_at_gmail.com>
Date: Tue 03 Apr 2007 - 09:16:10 GMT

I have much more problem in the following case:

 Years Products New Column

 1          10           0
 2          25           0
 3          40           0
 4          NA          0
 5          35           0
<NA>    23           1
 6         NA           0
 7         67            0
 8         NA           0
NA       NA           *NA*
NA       NA           *NA*

When i hane NA in both columns the results of the procedure give me NA. I'd like to obtain 0.

2007/4/3, Mark Wardle <mark@wardle.org>:
>
> Sergio Della Franca wrote:
> > Dear R-Helpers,
> >
> > I have the following data set(y):
> >
> > Years Products
> > 1 10
> > 2 25
> > 3 40
> > 4 NA
> > 5 35
> > <NA> 23
> > 6 NA
> > 7 67
> > 8 NA
> >
> > I want to create a new column into my dataset(y) under the following
> > conditions:
> > if years =NA and products >20 then new column=1 else new column=0;
> > to obtain the following results:
> >
> > Years Products New Column
> > 1 10 0
> > 2 25 0
> > 3 40 0
> > 4 NA 0
> > 5 35 0
> > <NA> 23 1
> > 6 NA 0
> > 7 67 0
> > 8 NA 0
> >
>
> How about using ifelse():
> year = c(1,2,3,4,5,NA,6,7,8)
> products = c(10,25,40,NA,35,23,NA,67,NA)
> ifelse(is.na(year) & products>20,1,0)
>
>
> =>
>
> [1] 0 0 0 0 0 1 0 0 0
>
>
> Mark
>
> --
> Dr. Mark Wardle
> Specialist registrar, Neurology
> Cardiff, UK
>

        [[alternative HTML version deleted]]



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 Apr 03 19:19:46 2007

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 03 Apr 2007 - 11:31:08 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.