[R] Combining elements of vectors

2009-06-08 Thread Marie Sivertsen
Dear list,

I have a vector of elements which I want to combined each with each, but
none with itself. For example,

 v - c(a, b, c)

and I need a function 'combine' such that

 combine(v)
[[1]]
[1] a b

[[2]]
[1] a b

[[3]]
[1] b c

I am not very interested in the orders of the output items for now, and the
form can be something differs from list, like matrix or data frame. I know
of 'expand.grid', but it will combine each item with each item including
itself, so not what I wants.

Could you help me please.  Thank you.

Mvh.,
Marie

[[alternative HTML version deleted]]

__
R-help@r-project.org 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.


Re: [R] Combining elements of vectors

2009-06-08 Thread Marie Sivertsen
Thank you both Dimitiris and Jorge Ivan!  I have just found it myself that
'combn' does what I need:

 combn(v, 2, simplify=TRUE)

is exactly what I need.

Mvh.,
Marie





On Mon, Jun 8, 2009 at 3:03 PM, Dimitris Rizopoulos 
d.rizopou...@erasmusmc.nl wrote:

 one way is:

 combn(c(a, b, c), 2)


 I hope it helps.

 Best,
 Dimitris


 Marie Sivertsen wrote:

 Dear list,

 I have a vector of elements which I want to combined each with each, but
 none with itself. For example,

  v - c(a, b, c)


 and I need a function 'combine' such that

  combine(v)

 [[1]]
 [1] a b

 [[2]]
 [1] a b

 [[3]]
 [1] b c

 I am not very interested in the orders of the output items for now, and
 the
 form can be something differs from list, like matrix or data frame. I know
 of 'expand.grid', but it will combine each item with each item including
 itself, so not what I wants.

 Could you help me please.  Thank you.

 Mvh.,
 Marie

[[alternative HTML version deleted]]

 __
 R-help@r-project.org 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.


 --
 Dimitris Rizopoulos
 Assistant Professor
 Department of Biostatistics
 Erasmus University Medical Center

 Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
 Tel: +31/(0)10/7043478
 Fax: +31/(0)10/7043014


[[alternative HTML version deleted]]

__
R-help@r-project.org 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.


Re: [R] Combining elements of vectors

2009-06-08 Thread Marie Sivertsen
On Mon, Jun 8, 2009 at 3:04 PM, baptiste auguie
baptiste.aug...@gmail.comwrote:

 Marie Sivertsen wrote:

 Dear list,

 I have a vector of elements which I want to combined each with each, but
 none with itself. For example,



 v - c(a, b, c)



 and I need a function 'combine' such that



 combine(v)


 [[1]]
 [1] a b

 [[2]]
 [1] a b

 [[3]]
 [1] b c

 I am not very interested in the orders of the output items for now, and
 the
 form can be something differs from list, like matrix or data frame. I know
 of 'expand.grid', but it will combine each item with each item including
 itself, so not what I wants.

 Could you help me please.  Thank you.

 Mvh.,
 Marie

[[alternative HTML version deleted]]

 __
 R-help@r-project.org 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.


 I guess you actually want,

 [[1]]
 [1] a b

 [[2]]
 [1] a c # not a b again



Yes I made a mistake.  Thank you.

Mvh.,
Marie





 [[3]]
 [1] b c


 if so, try

 ?combn
  combn(v,2)
[,1] [,2] [,3]
 [1,] a  a  b
 [2,] b  c  c


 HTH,

 baptiste

 --
 _

 Baptiste AuguiƩ

 School of Physics
 University of Exeter
 Stocker Road,
 Exeter, Devon,
 EX4 4QL, UK

 Phone: +44 1392 264187

 http://newton.ex.ac.uk/research/emag
 __



[[alternative HTML version deleted]]

__
R-help@r-project.org 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.


[R] Parsing configuration files

2009-05-18 Thread Marie Sivertsen
Dear list,

Is there any functionality in R that would allow me to parse config files?
I have trie ??config and apropos('config') without succes, and also search
the R package site.

Mvh.
Marie

[[alternative HTML version deleted]]

__
R-help@r-project.org 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.


Re: [R] How to find the path or the current file?

2009-03-25 Thread Marie Sivertsen
Wacek, this work for me.  Takk!

Mvh.
Marie

On Wed, Mar 25, 2009 at 2:59 PM, Wacek Kusnierczyk 
waclaw.marcin.kusnierc...@idi.ntnu.no wrote:

 Wacek Kusnierczyk wrote:
  hacking up on gabor's solution, i've created a trivial function that
  will allow you to access a file given a path relative to the path of the
  file calling the function.
 
  to be concrete, suppose you have two files -- one library and one
  executable -- located in two sibling directories, and you want one of
  them to access (e.g., source) the other without the need to specify the
  absolute path, and irrespectively of the current working directory.
  here is a simple example.
 
  mkdir foo/{bin,lib} -p
 
  echo '
 # the library file
 foo = function() cat(foo\n)
  '  foo/lib/lib.r
 
  echo '
 # the executable file
 source(http://miscell.googlecode.com/svn/rpath/rpath.r;)
 source(rpath(../lib/lib.r))
 foo()
  '  foo/bin/bin.r
 

 one thing i forgot to add:  that contrarily to what gabor warned about
 his solution, you don't have to have the call to rpath at the top level,
 and can embed it in nested nevironments or calls; thus, the following
 executable:

echo '
   # the executable file
   source(http://miscell.googlecode.com/svn/rpath/rpath.r;)
(function()
  (function()
 (function() {
 source(rpath(../lib/lib.r))
foo() })())())()
'  foo/bin/bin.r

 will still work as below.

  now you can execute foo/bin/bin.r from whatever location, or source it
  in r within whatever working directory, and still have it load
  foo/lib/lib.r:
 
  r foo/bin/bin.r
  # foo
 
  (cd foo; r bin/bin.r)
  # foo
 
  r -e 'source(foo/bin/bin.r)'
  # foo
 
  (cd foo/bin; r -e 'source(bin.r)')
  # foo
 
  so the trick for you is to source rpath, and voila.  (note, it's not
  foolproof;  as duncan explained, such approach may not work in some
  circumstances.)
 
  does this address your problem?
 
  hilsen,
  vQ
 
  Gabor Grothendieck wrote:
 
  See:
 
  https://stat.ethz.ch/pipermail/r-help/2009-January/184745.html
 
  On Tue, Mar 24, 2009 at 7:16 AM, Marie Sivertsen mariesiv...@gmail.com
 wrote:
 
 
  Dear useRs,
 
  I have a collection of source file and some of these call others.  The
 files
  are distribute among a number of directories, and to know how to call
 some
  other file they need to know what file is currently executed.
 
  As example, I have a file 'search.R' located in directory 'bin' which
 needs
  to access the file 'terms.conf' located in the directory 'conf', a
 sibling
  of 'bin'.  I can have somethings like readLines('../conf/terms.conf')
 in
  search.R, but this work only if search.R is executed from bin, when
 getwd is
  'bin'.  But when search.R calls from the parent as bin/search.R or any
 other
  derectory then R complains that it could not find the file
  '../conf/terms.conf'.
 
  So my questions is:  how can the file search.R, when executied,
 discover its
  own location and load terms.conf from location of
  search.R/../conf/terms.conf?  the location of search.R can be
 unrelated to
  the current directory.
 
  Mvh.
  Marie



[[alternative HTML version deleted]]

__
R-help@r-project.org 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.


[R] How to find the path or the current file?

2009-03-24 Thread Marie Sivertsen
Dear useRs,

I have a collection of source file and some of these call others.  The files
are distribute among a number of directories, and to know how to call some
other file they need to know what file is currently executed.

As example, I have a file 'search.R' located in directory 'bin' which needs
to access the file 'terms.conf' located in the directory 'conf', a sibling
of 'bin'.  I can have somethings like readLines('../conf/terms.conf') in
search.R, but this work only if search.R is executed from bin, when getwd is
'bin'.  But when search.R calls from the parent as bin/search.R or any other
derectory then R complains that it could not find the file
'../conf/terms.conf'.

So my questions is:  how can the file search.R, when executied, discover its
own location and load terms.conf from location of
search.R/../conf/terms.conf?  the location of search.R can be unrelated to
the current directory.

Mvh.
Marie

[[alternative HTML version deleted]]

__
R-help@r-project.org 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.


Re: [R] How to find the path or the current file?

2009-03-24 Thread Marie Sivertsen
Thank you Gabor and Duncan for your replys.

Mvh.
Marie


On Tue, Mar 24, 2009 at 1:00 PM, Gabor Grothendieck ggrothendi...@gmail.com
 wrote:

 See:

 https://stat.ethz.ch/pipermail/r-help/2009-January/184745.html

 On Tue, Mar 24, 2009 at 7:16 AM, Marie Sivertsen mariesiv...@gmail.com
 wrote:
  Dear useRs,
 
  I have a collection of source file and some of these call others.  The
 files
  are distribute among a number of directories, and to know how to call
 some
  other file they need to know what file is currently executed.
 
  As example, I have a file 'search.R' located in directory 'bin' which
 needs
  to access the file 'terms.conf' located in the directory 'conf', a
 sibling
  of 'bin'.  I can have somethings like readLines('../conf/terms.conf') in
  search.R, but this work only if search.R is executed from bin, when getwd
 is
  'bin'.  But when search.R calls from the parent as bin/search.R or any
 other
  derectory then R complains that it could not find the file
  '../conf/terms.conf'.
 
  So my questions is:  how can the file search.R, when executied, discover
 its
  own location and load terms.conf from location of
  search.R/../conf/terms.conf?  the location of search.R can be unrelated
 to
  the current directory.
 
  Mvh.
  Marie
 
 [[alternative HTML version deleted]]
 
  __
  R-help@r-project.org 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.
 


[[alternative HTML version deleted]]

__
R-help@r-project.org 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.


[R] Problems with ARIMA models?

2009-02-25 Thread Marie Sivertsen
Dear R,

I have find a website where they report problem with ARIMA models in R.  I
run the examples there and they give result as shown on the website.  Does
this mean that nothing has corrected in R?  Maybe you not have seen the
page, but the author said he contacted you.

Here is the URL: http://www.stat.pitt.edu/stoffer/tsa2/Rissues.htm

I like to know your opinion.

Mvh.
Marie

[[alternative HTML version deleted]]

__
R-help@r-project.org 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.


[R] Problem with plotting data from within a function

2009-02-05 Thread Marie Sivertsen
Dear List,

I try to develop code where plotting functions are embeded in my own
functions.  The following is simplified example:

 test - function() {
data - data.frame(x=rep(1:2, each=50), y=rnorm(100))
   library(lattice)
attach(data)
xyplot(y~x)
detach()
}
test()


As far as I understand R, when test is called this should create one local
data frame, attach it that it's columns are available directly, load the
lattice library, plot data, and then detach the data frame.  But, when I
call test, nothing happens.  I see not plot and no error message.  When I
try the script from Rscript, nothing happens too.  But when I copy the
inside code from test and run it directly, all seems to work.  What am I
doing wrong?  Is this a question to be asked here, or to the developers of
lattice package?

Mvh.
Marie

[[alternative HTML version deleted]]

__
R-help@r-project.org 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.


Re: [R] eval and as.name

2009-02-05 Thread Marie Sivertsen
Hi,

Why do you use the equals sign for assignment instead of the arrow, is this
equal?

Mvh.
Marie

On Thu, Feb 5, 2009 at 11:59 PM, Wacek Kusnierczyk 
waclaw.marcin.kusnierc...@idi.ntnu.no wrote:


 you may want to avoid this sort of indirection by using lists with named
 components:

 d = list(a=c(1,3,5,7), b=c(2,4,6,8))
 sum(unlist(d))
 with(d, sum(a+b))
 sum(d[['a']], d[['b']])
 sum(sapply(n, function(v) d[[v]]))

 and so on.

 vQ

 __
 R-help@r-project.org 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.


[[alternative HTML version deleted]]

__
R-help@r-project.org 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.


Re: [R] tab characters

2009-01-30 Thread Marie Sivertsen
I see 'The R Inferno' being refered quiet often recently.  But it was now
pointed by Duncan Murdoch that for example the statement concerning
variables in a for loop is not correct in there (page 62).  As I can not
find any information about the book been reviewed by anyone I have a
question:  is it reliable resource for learning about R?  What is the
authority of Patrick Burns?  I would like to avoid spending much time on
learning from 'The R Inferno' to only later discover that it was wrong.

Mvh.
Marie



On Fri, Jan 30, 2009 at 11:13 AM, Patrick Burns pbu...@pburns.seanet.comwrote:

 'The R Inferno' pages 45-46.


 Patrick Burns
 patr...@burns-stat.com
 +44 (0)20 8525 0696
 http://www.burns-stat.com
 (home of The R Inferno and A Guide for the Unwilling S User)

 Nick Matzke wrote:

 Hi all,

 Working at the R command line, how do I get strings to display e.g. tab or
 newline characters as they should be displayed, rather than as e.g. \n or
 \t?

 e.g.:
  x=\t
  x=\t
  x
 [1] \t
  print(x)
 [1] \t


 __
 R-help@r-project.org 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.


[[alternative HTML version deleted]]

__
R-help@r-project.org 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.


Re: [R] tab characters

2009-01-30 Thread Marie Sivertsen
Thank you both Peter and Duncan for explanations.  'The R Inferno' is indeed
not so much introduction but I find it useful to know about how I can go
wrong in simple things before I do.

Mvh.
Marie

On Fri, Jan 30, 2009 at 3:03 PM, Peter Dalgaard p.dalga...@biostat.ku.dkwrote:

 Marie Sivertsen wrote:
  I see 'The R Inferno' being refered quiet often recently.  But it was now
  pointed by Duncan Murdoch that for example the statement concerning
  variables in a for loop is not correct in there (page 62).  As I can not
  find any information about the book been reviewed by anyone I have a
  question:  is it reliable resource for learning about R?  What is the
  authority of Patrick Burns?  I would like to avoid spending much time on
  learning from 'The R Inferno' to only later discover that it was wrong.


 In short: Don't worry too much.

 Pat is an experienced S and R user. In fact, he's been around since R
 was little but a gleam in its fathers' eyes. He has a record of writing
 stuff and publishing on the web, in a style somewhat different from what
 the publishing companies seem to want. The R Inferno is his latest
 addition, so he's liable to make a few Hey, I wrote about that kind of
 posts.

 A quick look suggest that this should be quite an amusing read, but it
 isn't a textbook for beginners learning R. Rather, it is assumed that
 you already know the basics, and now need to read about the pitfalls.
 There could be inaccuracies (the for loop description is indeed mildly
 off-base), but the intended audience can reasonably be assumed to
 possess a critical mind. And the nice thing about web publications is
 that mistakes can be fixed quickly.

 -pd



[[alternative HTML version deleted]]

__
R-help@r-project.org 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.


Re: [R] Unexpected behaviour of the as.Date (was: Error as.Date on Invalid Dates)

2009-01-23 Thread Marie Sivertsen
Thank you Greg for your explanations.  I think you explained the problem
clearly now.

Mvh.
Marie


On Thu, Jan 22, 2009 at 10:24 PM, Greg Snow greg.s...@imail.org wrote:

 Comments interspersed below

 From: Marie Sivertsen [mailto:mariesiv...@gmail.com]
 Sent: Thursday, January 22, 2009 1:17 PM
 To: Greg Snow
 Cc: r-h...@stat.math.ethz.ch
 Subject: Re: [R] Unexpected behaviour of the as.Date (was: Error as.Date on
 Invalid Dates)

  [snip]


 For your question, the help page for as.Date includes:

  format: A character string.  The default is '%Y-%m-%d'.  For
  details see 'strftime'.


 To be strict, neither 1/13/2001 nor 13/1/2001 match the format, so both
 should raise error, I think.  Since the behaviour seem not to apply the
 default strictly, why ought one think 13/1/2001 will not be parsed the
 only reasonable way?


 The help page for as.Date refers to the help page for strptime which says
 that details are system specific. So there may be some systems where you
 would get an error from '/' not being '-', but apparently on your system
 they are treated the same.   Personally I see a big difference between
 interpreting an obvious separator as such and changing the order of values.
  The fact that it sometimes gets the one correct does not imply to me that
 the other should happen automatically.

 Dealing with the separators can be done on an individual basis as each
 character string is processed.  Guessing the order of the entries could
 require looking at the entire vector/file/dataset, which I expect would slow
 things down quite a bit.  (and how long would it be before someone
 complained that it processed file A correctly, but file B should have been
 treated like A, but since it only included days less than 13, the program
 did not realize this).


 And

 Character strings are processed as far as
 necessary for the format specified: any trailing characters are
 ignored.

 I don't see anything in your examples that runs counter to the above.


 Yes they do.  None of them match the format, but some parse correctly, some
 produce rubbish, and some raise error.  Maybe you want to improve the help
 page fo the as.Date to say something like The default is a sequence of
 numerical representations of the year, then the month, then the day,
 separated by one of '-', '/', ..., which make it clearer.
 But is it correct? It may be system dependent (or all systems may do the
 exact same now).  How about if the help page tells you to find out for your
 system (easy fix, it already does).

 Remember that computers do exactly what you tell them to do, not what you
 think that they should do.


 Computers do exactly what they were programmed to do, and what they will do
 depends on what the developer told them to do when they are given certain
 input.  I expect them to do exactly what I tell them to do, and it is to
 parse 1/13/2001 the only reasonable way.  It seems that someone told them
 to do something else...

 I was using the general 'you' above that includes the programmer as well as
 the user, since you (singular) did not specify the format, the computer used
 the default format that the programmer (part of the collective 'you')
 specified which says the order is year, month, day.

 Many problems come as a result of users forgetting that they are smarter
 than the computer.  I see 3 ways to remedy the problem:

 1. Make computers that are as smart or smarter than people.
 2. Make the programmers anticipate every way that someone may use a
 particular function and make them implement all of the functionality even if
 they don't think it is worth the time/effort since there is an easy work
 around for many of the less likely used features.
 3. Don't expect the computer to guess correctly and tell it exactly what
 you want it to do.

 I don't think that number 1 will ever happen, and there are plenty of
 science fiction stories that suggest problems with even trying.

 Option 2 stinks of hubris, and even if it were possible, I personally would
 not want to wait until they were finished before being able to use the
 functions/programs.

 Which leaves option 3, which I think is the best approach even without
 arguments against the others.

 I think the moral of this story is: program defensively, always specify a
 date format!


 Mvh.
 Marie



 --
 Gregory (Greg) L. Snow Ph.D.
 Statistical Data Center
 Intermountain Healthcare
 greg.s...@imail.org
 801.408.8111




[[alternative HTML version deleted]]

__
R-help@r-project.org 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.


Re: [R] Error as.Date on Invalid Dates

2009-01-22 Thread Marie Sivertsen
I am relatively new to R, so maybe I am miss something, but I now
tried the as.Date now and have problems understanding how it works (or
don't work as it seem).


Brian D Ripley wrote:
 On Thu, 22 Jan 2009, Terry Therneau wrote:

 One idea is to use the as.date function, for the older (and less capable) 
 'date'
 class.  This is currently loaded by default with library(survival).  It 
 returns
 NA for an invalid date rather than dying.

 So does as.Date **if you specify the format** (as you have to with your 
 as.date:
 it has a default one):


as.Date(2001/1/1)
Works fine

as.Date(1/1/2001)
Prints 1-01-20 ???

as.Date(13/1/2001)
Prints 13-01-20 ???

as.Date(1/13/2001)
Prints error: not in standard unambigous format

It seems that as if both 1/1/2001 and 13/1/2001 were considered by
R to be in a
standard unambiguous format (or otherwise an error be reported?) and yet they
are parsed incorrectly according to what one could think is obvious.
It is also
surprizing that not only 13/1/2001 but also 1/2/2001 and 2/1/2001 are
successful but incorrect parsed as if they are unambiguous, and yet
13/1/2001 is ambiguous, though there is really just one way to
parse it meaningfully.

I think the strings that are incorrectly parsed should raise errors,
and the last example should be succesful parsed.  What is the reason
for the observed?

Mvh.
Marie

[[alternative HTML version deleted]]

__
R-help@r-project.org 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.


[R] Unexpected behaviour of the as.Date (was: Error as.Date on Invalid Dates)

2009-01-22 Thread Marie Sivertsen
Dear Brian,

I dont understand what you mean.  The thread was about the as.Date which you
suggested to be used instead of the as.date.  Following your advice I tried
the as.Date and have questions about the observed behaviour, which was
surprising to me.  Is this what you call hijacking?  Do you mean I ought
start a new thread instead?  I thought my question were relevant to the
threads' subject.  I am sorry if it were not.

So here is the questions once again:  why do the as.Date behave as in my
examples below, is this intended?



On Thu, Jan 22, 2009 at 3:55 PM, Brian D Ripley rip...@stats.ox.ac.ukwrote:

 You've hijacked a thread here.


 On Thu, 22 Jan 2009, Marie Sivertsen wrote:


 I am relatively new to R, so maybe I am miss something, but I now tried
 the a
 s.Date now and have problems understanding how it works (or don't work as
 it seem).

 Brian D Ripley wrote:
  On Thu, 22 Jan 2009, Terry Therneau wrote:
 
  One idea is to use the as.date function, for the older (and less
 capable) 'date'
  class.  This is currently loaded by default with library(survival).  It
 re
 turns

  NA for an invalid date rather than dying.
 
  So does as.Date *if you specify the format* (as you have to with your
 as.da
 te:
  it has a default one):



My examples:




 as.Date(2001/1/1)
 Works fine
 as.Date(1/1/2001)
 Prints 1-01-20 ???
 as.Date(13/1/2001)
 Prints 13-01-20 ???

 as.Date(1/13/2001)
 Prints error: not in standard unambigous format
 It seems that as if both 1/1/2001 and 13/1/2001 were considered by R
 to b
 e in a standard unambiguous format (or otherwise an error be reported?)
 and yet they


 are parsed incorrectly according to what one could think is obvious.  It
 is a
 lso surprizing that not only 13/1/2001 but also 1/2/2001 and
 2/1/2001 are successful but incorrect parsed as if they are unambiguous,
 and yet
 13/1/2001 is ambiguous, though there is really just one way to parse it
 meaningfully.
 I think the strings that are incorrectly parsed should raise errors, and
 the last example should be succesful parsed.  What is the reason for the
 observed
 ?





Mvh.
Marie





 --
 Brian D. Ripley,  rip...@stats.ox.ac.uk
 Professor of Applied Statistics,  
 http://www.stats.ox.ac.uk/~ripley/http://www.stats.ox.ac.uk/%7Eripley/
 University of Oxford, Tel:  +44 1865 272861 (self)
 1 South Parks Road, +44 1865 272866 (PA)
 Oxford OX1 3TG, UKFax:  +44 1865 272595


[[alternative HTML version deleted]]

__
R-help@r-project.org 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.


Re: [R] Unexpected behaviour of the as.Date (was: Error as.Date on Invalid Dates)

2009-01-22 Thread Marie Sivertsen
Thank you Greg and Gabor for explanations.  I have some further question
below.

On Thu, Jan 22, 2009 at 8:16 PM, Greg Snow greg.s...@imail.org wrote:

 I believe the original thread was about whether the function returns NA or
 stops with an error when given an invalid date (such as Feb 29 in a non-leap
 year).  Your question was about how as.Date returned something different
 from what you expected.  Related, but different enough that it probably
 would have been better to start a new thread.



I hope it was then okay I started a new thread.





 For your question, the help page for as.Date includes:

  format: A character string.  The default is '%Y-%m-%d'.  For
  details see 'strftime'.



To be strict, neither 1/13/2001 nor 13/1/2001 match the format, so both
should raise error, I think.  Since the behaviour seem not to apply the
default strictly, why ought one think 13/1/2001 will not be parsed the
only reasonable way?




 And

 Character strings are processed as far as
 necessary for the format specified: any trailing characters are
 ignored.

 I don't see anything in your examples that runs counter to the above.



Yes they do.  None of them match the format, but some parse correctly, some
produce rubbish, and some raise error.  Maybe you want to improve the help
page fo the as.Date to say something like The default is a sequence of
numerical representations of the year, then the month, then the day,
separated by one of '-', '/', ..., which make it clearer.



 Remember that computers do exactly what you tell them to do, not what you
 think that they should do.



Computers do exactly what they were programmed to do, and what they will do
depends on what the developer told them to do when they are given certain
input.  I expect them to do exactly what I tell them to do, and it is to
parse 1/13/2001 the only reasonable way.  It seems that someone told them
to do something else...

Mvh.
Marie

[[alternative HTML version deleted]]

__
R-help@r-project.org 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.