[R] Loop trouble with Excel Serial Numbers!

jim holtman jholtman at gmail.com
Thu Aug 18 15:43:29 CEST 2011


You don't need the loop; it was converting back to numeric.  Try this:

> thedate<-as.matrix(40548:40759,ncol=1)
>
> exdate<-function(){
+  mynewdate<-as.Date(thedate[,1],origin="1899-12-30")
+  print(mynewdate)
+ }
> exdate()
  [1] "2011-01-05" "2011-01-06" "2011-01-07" "2011-01-08" "2011-01-09"
"2011-01-10" "2011-01-11"
  [8] "2011-01-12" "2011-01-13" "2011-01-14" "2011-01-15" "2011-01-16"
"2011-01-17" "2011-01-18"
 [15] "2011-01-19" "2011-01-20" "2011-01-21" "2011-01-22" "2011-01-23"
"2011-01-24" "2011-01-25"
 [22] "2011-01-26" "2011-01-27" "2011-01-28" "2011-01-29" "2011-01-30"
"2011-01-31" "2011-02-01"
 [29] "2011-02-02" "2011-02-03" "2011-02-04" "2011-02-05" "2011-02-06"
"2011-02-07" "2011-02-08"
 [36] "2011-02-09" "2011-02-10" "2011-02-11" "2011-02-12" "2011-02-13"
"2011-02-14" "2011-02-15"
 [43] "2011-02-16" "2011-02-17" "2011-02-18" "2011-02-19" "2011-02-20"
"2011-02-21" "2011-02-22"


On Thu, Aug 18, 2011 at 8:27 AM, Anna Dunietz <anna.dunietz at gmail.com> wrote:
> Hi All!
>
> I'm trying to convert serial numbers in Excel to dates in R.  For each
> single "thedate" entry, I get a correct answer.  But if I try using the for
> loop, I get bizarre numbers in "mynewdata".
>
> thedate<-as.matrix(40548:40759,ncol=1)
>
> exdate<-function(){
>  mynewdate<-NULL
>  for(i in 1:nrow(thedate)){
>  mynewdate[i]<-as.Date(thedate[i,1],origin="1899-12-30")
>  }
>  print(mynewdate)
> }
> exdate()
>
> Thank you very much!
> Anna
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
>



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?



More information about the R-help mailing list