Re: [R] converting multiple columns from POSIX* to Date

2008-12-12 Thread Farrel Buchinsky
I got the whole thing to work, with Hadley Wickham's help.
is.POSIXt <- function(x) inherits(x, "POSIXt")
dates <- sapply(df, is.POSIXt)
df[dates] <-lapply(df[dates], as.Date)


Farrel Buchinsky




On Fri, Dec 12, 2008 at 11:26, Farrel Buchinsky  wrote:
>
> Something strange. The lapply only processed the first row and then wrote 
> that value to every row of the original dataframe. It is as if the lapply is 
> indeed processing every item on the list, namely each column, but the ifelse 
> or the as.Date is getting messed up. Not only is it only processing the first 
> row but it is also returning the value as numbers rather than dates. I am now 
> playing around with Hadley Wickham's plyr package. So far looking good but 
> still need to work out a few things.

__
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] converting multiple columns from POSIX* to Date

2008-12-12 Thread Farrel Buchinsky
Something strange. The lapply only processed the first row and then wrote
that value to every row of the original dataframe. It is as if the lapply is
indeed processing every item on the list, namely each column, but the ifelse
or the as.Date is getting messed up. Not only is it only processing the
first row but it is also returning the value as numbers rather than dates. I
am now playing around with Hadley Wickham's plyr package. So far looking
good but still need to work out a few things.
Farrel Buchinsky
GrandCentral Tel: (412) 567-7870



On Wed, Dec 10, 2008 at 19:08, Marc Schwartz wrote:

> Use inherits() then rather than class():
>
>  DF[] <- lapply(DF, function(x) ifelse(inherits(x, "POSIXt"),
>as.Date(x), x))
>
> That should hopefully work better than my first attempt.
>
> HTH,
>
> Marc Schwartz
>
> on 12/10/2008 05:47 PM Farrel Buchinsky wrote:
> > I will try that but I am somewhat skeptical since when I go
> > class(date.of.birth) I get not just one word but two:  "POSIXt"
> > "POSIXct". Will that not mess up the logical test
> >
> > When I tried the following:
> > lapply(as.list(dataframename),class)=="POSIXt"
> > every item was false
> >
> > Farrel Buchinsky
> > GrandCentral Tel: (412) 567-7870
> >
> >
> >
> >
> >
> >> Dear Farrel,
> >> Determine the class of each column and apply as.Date() just to those
> which
> >> class is POSIX. For more details see ?class. Here is an example assuming
> >> that you're data is named "mydata":
> >> apply(mydata, 2, function(x) ifelse( class(x)=="POSIXt" |
> >>  class(x)=="POSIXlt" , as.Date(x) , x ) )
> >>
> >> HTH,
> >>
> >> Jorge
> >>
> >>
> >>
> >> On Wed, Dec 10, 2008 at 6:26 PM, Farrel Buchinsky 
> wrote:
> >>> converting a POSIX class variable to a date class is easy.
> >>> dates<-as.Date(x) #where X is of class POSIX
> >>> How does one do that to all columns in a data frame that are of POSIX
> >>> class and leave all the other columns (integers, factors) as is.
> >>>
> >>> Feel free to reply with just one or two buzzwords that I could then
> >>> search for to find how to do it.
> >>>
> >>> Farrel Buchinsky
> >>>
> >>> __
> >>> 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-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] converting multiple columns from POSIX* to Date

2008-12-10 Thread Marc Schwartz
Use inherits() then rather than class():

  DF[] <- lapply(DF, function(x) ifelse(inherits(x, "POSIXt"),
as.Date(x), x))

That should hopefully work better than my first attempt.

HTH,

Marc Schwartz

on 12/10/2008 05:47 PM Farrel Buchinsky wrote:
> I will try that but I am somewhat skeptical since when I go
> class(date.of.birth) I get not just one word but two:  "POSIXt"
> "POSIXct". Will that not mess up the logical test
> 
> When I tried the following:
> lapply(as.list(dataframename),class)=="POSIXt"
> every item was false
> 
> Farrel Buchinsky
> GrandCentral Tel: (412) 567-7870
> 
> 
> 
> 
> 
>> Dear Farrel,
>> Determine the class of each column and apply as.Date() just to those which
>> class is POSIX. For more details see ?class. Here is an example assuming
>> that you're data is named "mydata":
>> apply(mydata, 2, function(x) ifelse( class(x)=="POSIXt" |
>>  class(x)=="POSIXlt" , as.Date(x) , x ) )
>>
>> HTH,
>>
>> Jorge
>>
>>
>>
>> On Wed, Dec 10, 2008 at 6:26 PM, Farrel Buchinsky <[EMAIL PROTECTED]> wrote:
>>> converting a POSIX class variable to a date class is easy.
>>> dates<-as.Date(x) #where X is of class POSIX
>>> How does one do that to all columns in a data frame that are of POSIX
>>> class and leave all the other columns (integers, factors) as is.
>>>
>>> Feel free to reply with just one or two buzzwords that I could then
>>> search for to find how to do it.
>>>
>>> Farrel Buchinsky
>>>
>>> __
>>> 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-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-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] converting multiple columns from POSIX* to Date

2008-12-10 Thread Farrel Buchinsky
I will try that but I am somewhat skeptical since when I go
class(date.of.birth) I get not just one word but two:  "POSIXt"
"POSIXct". Will that not mess up the logical test

When I tried the following:
lapply(as.list(dataframename),class)=="POSIXt"
every item was false

Farrel Buchinsky
GrandCentral Tel: (412) 567-7870





>
> Dear Farrel,
> Determine the class of each column and apply as.Date() just to those which
> class is POSIX. For more details see ?class. Here is an example assuming
> that you're data is named "mydata":
> apply(mydata, 2, function(x) ifelse( class(x)=="POSIXt" |
>  class(x)=="POSIXlt" , as.Date(x) , x ) )
>
> HTH,
>
> Jorge
>
>
>
> On Wed, Dec 10, 2008 at 6:26 PM, Farrel Buchinsky <[EMAIL PROTECTED]> wrote:
>>
>> converting a POSIX class variable to a date class is easy.
>> dates<-as.Date(x) #where X is of class POSIX
>> How does one do that to all columns in a data frame that are of POSIX
>> class and leave all the other columns (integers, factors) as is.
>>
>> Feel free to reply with just one or two buzzwords that I could then
>> search for to find how to do it.
>>
>> Farrel Buchinsky
>>
>> __
>> 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-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] converting multiple columns from POSIX* to Date

2008-12-10 Thread Marc Schwartz
on 12/10/2008 05:26 PM Farrel Buchinsky wrote:
> converting a POSIX class variable to a date class is easy.
> dates<-as.Date(x) #where X is of class POSIX
> How does one do that to all columns in a data frame that are of POSIX
> class and leave all the other columns (integers, factors) as is.
> 
> Feel free to reply with just one or two buzzwords that I could then
> search for to find how to do it.
> 
> Farrel Buchinsky

If 'DF' is your source data frame, something like:

  DF[] <- lapply(DF, function(x) ifelse(class(x) == "POSIXt",
as.Date(x), x)


This is untested, so proceed with due caution.

HTH,

Marc Schwartz

__
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] converting multiple columns from POSIX* to Date

2008-12-10 Thread Farrel Buchinsky
converting a POSIX class variable to a date class is easy.
dates<-as.Date(x) #where X is of class POSIX
How does one do that to all columns in a data frame that are of POSIX
class and leave all the other columns (integers, factors) as is.

Feel free to reply with just one or two buzzwords that I could then
search for to find how to do it.

Farrel Buchinsky

__
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.