If else in R code

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

If else in R code

Andrew McFadden
Hi There

I am a new user of "R" and having a few problems

a<-c("2001-02-1","NA","NA")
a<-as.Date(a,format = "%Y-%m-%d")

b<-c("2001-03-1","2001-03-2","2001-03-3")
b<-as.Date(b,format = "%Y-%m-%d")

c<-data.frame(a,b)

I would like to write an if statement where if "a" is not null return
"a", if "a" is null (NA) return "b" less two days. Could someone help?.


Andrew  



########################################################################
This email message and any attachment(s) is intended solely for the
addressee(s) named above. The information it contains is confidential
and may be legally privileged.  Unauthorised use of the message, or the
information it contains, may be unlawful. If you have received this
message by mistake please call the sender immediately on 64 4 8940100
or notify us by return email and erase the original message and
attachments. Thank you.

The Ministry of Agriculture and Forestry accepts no responsibility for
changes made to this email or to any attachments after transmission from
the office.
########################################################################

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: If else in R code

Simon Blomberg-4
How about:

a<-c("2001-02-1",NA,NA)
b<-c("2001-03-1","2001-03-2","2001-03-3")

 res <- ifelse(is.na(a), as.character(as.Date(b)-2),
as.character(as.Date(a)))
res

Or with no ifelse:

 res <- a
 res[is.na(a)] <- as.character(as.Date(b[is.na(a)])-2)
 res

Cheers,

Simon.

On Fri, 2008-02-08 at 14:23 +1300, Andrew McFadden wrote:

> Hi There
>
> I am a new user of "R" and having a few problems
>
> a<-c("2001-02-1","NA","NA")
> a<-as.Date(a,format = "%Y-%m-%d")
>
> b<-c("2001-03-1","2001-03-2","2001-03-3")
> b<-as.Date(b,format = "%Y-%m-%d")
>
> c<-data.frame(a,b)
>
> I would like to write an if statement where if "a" is not null return
> "a", if "a" is null (NA) return "b" less two days. Could someone help?.
>
>
> Andrew  
>
>
>
> ########################################################################
> This email message and any attachment(s) is intended solely for the
> addressee(s) named above. The information it contains is confidential
> and may be legally privileged.  Unauthorised use of the message, or the
> information it contains, may be unlawful. If you have received this
> message by mistake please call the sender immediately on 64 4 8940100
> or notify us by return email and erase the original message and
> attachments. Thank you.
>
> The Ministry of Agriculture and Forestry accepts no responsibility for
> changes made to this email or to any attachments after transmission from
> the office.
> ########################################################################
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
--
Simon Blomberg, BSc (Hons), PhD, MAppStat.
Lecturer and Consultant Statistician
Faculty of Biological and Chemical Sciences
The University of Queensland
St. Lucia Queensland 4072
Australia
Room 320 Goddard Building (8)
T: +61 7 3365 2506
http://www.uq.edu.au/~uqsblomb
email: S.Blomberg1_at_uq.edu.au

Policies:
1.  I will NOT analyse your data for you.
2.  Your deadline is your problem.

The combination of some data and an aching desire for
an answer does not ensure that a reasonable answer can
be extracted from a given body of data. - John Tukey.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: If else in R code

Peter Dalgaard
Simon Blomberg wrote:

> How about:
>
> a<-c("2001-02-1",NA,NA)
> b<-c("2001-03-1","2001-03-2","2001-03-3")
>
>  res <- ifelse(is.na(a), as.character(as.Date(b)-2),
> as.character(as.Date(a)))
> res
>
> Or with no ifelse:
>
>  res <- a
>  res[is.na(a)] <- as.character(as.Date(b[is.na(a)])-2)
>  res
>
>  
What's up with the as.character stuff?

 res <- a
 res[is.na(res)] <- b[is.na(res)]-2

with ifelse, you have the annoyance that it strips attributes, so you
need something like

 res <- ifelse(is.na(a),b-2, a)
 class(res) <- "Date"
 res




> Cheers,
>
> Simon.
>
> On Fri, 2008-02-08 at 14:23 +1300, Andrew McFadden wrote:
>  
>> Hi There
>>
>> I am a new user of "R" and having a few problems
>>
>> a<-c("2001-02-1","NA","NA")
>> a<-as.Date(a,format = "%Y-%m-%d")
>>
>> b<-c("2001-03-1","2001-03-2","2001-03-3")
>> b<-as.Date(b,format = "%Y-%m-%d")
>>
>> c<-data.frame(a,b)
>>
>> I would like to write an if statement where if "a" is not null return
>> "a", if "a" is null (NA) return "b" less two days. Could someone help?.
>>
>>
>> Andrew  
>>
>>
>>
>> ########################################################################
>> This email message and any attachment(s) is intended solely for the
>> addressee(s) named above. The information it contains is confidential
>> and may be legally privileged.  Unauthorised use of the message, or the
>> information it contains, may be unlawful. If you have received this
>> message by mistake please call the sender immediately on 64 4 8940100
>> or notify us by return email and erase the original message and
>> attachments. Thank you.
>>
>> The Ministry of Agriculture and Forestry accepts no responsibility for
>> changes made to this email or to any attachments after transmission from
>> the office.
>> ########################################################################
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] 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.
>>    


--
   O__  ---- Peter Dalgaard             Ă˜ster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - ([hidden email])                  FAX: (+45) 35327907

______________________________________________
[hidden email] 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.