Re: [R] substitute values

From: Dimitris Rizopoulos <dimitris.rizopoulos_at_med.kuleuven.be>
Date: Mon 02 Apr 2007 - 11:42:28 GMT

if your data set is a matrix try this

new.data <- cbind(data, "New Column" = is.na(data[, "Years"]) & data[, "Products"] > 20)

whereas if your data set is a data.frame try this

data$"New.Column" <- as.numeric(is.na(data$Years) & data$Products > 20)

I hope it helps.

Best,
Dimitris



Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium

Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


> 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
>
> Thank you in advance.
>
>
> Serigo Della Franca
>
> [[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.
>

Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm



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 Mon Apr 02 21:46:01 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 Mon 02 Apr 2007 - 13:30:35 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.