[R] type conversion with apply or not
Horace Tso
Horace.Tso at pgn.com
Tue Jun 8 23:37:11 CEST 2010
Guys, many thanks. lapply works. Did not occur to me as I thought lapply returns a list and the receiving entity is a data frame.
H
-----Original Message-----
From: William Dunlap [mailto:wdunlap at tibco.com]
Sent: Tuesday, June 08, 2010 2:22 PM
To: Horace Tso; r-help at r-project.org
Subject: RE: [R] type conversion with apply or not
The short answer is, don't use apply() on data.frame's.
Use lapply to loop over the columns of a data.frame.
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Horace Tso
> Sent: Tuesday, June 08, 2010 2:19 PM
> To: r-help at r-project.org
> Subject: [R] type conversion with apply or not
>
> Folks, i thought it should be straightforward but after a few
> hours poking around, I decided it's best to post my question
> on this list.
>
> I have a data frame consisting of a (large) number of date
> columns, which are read in from a csv file as character
> string. I want to convert them to Date type. Following is an
> example, where the first column is of integer type, while the
> rest are type character.
>
> > head(df)
> TRANSNO TRANS.START_DATE TRANS.END_DATE DIVISION FASB
> 1 250897 7/1/2010 7/31/2010 PSTRUCT Z
> 2 250617 8/1/2010 8/31/2010 PSTRUCT Z
> 3 250364 4/1/2011 6/30/2011 PLR Z
> 4 250176 4/1/2011 6/30/2011 PLR Z
> 5 250176 4/1/2011 6/30/2011 PLR Z
> 6 250364 4/1/2011 6/30/2011 PLR Z
> > sapply(df, class)
> TRANSNO TRANS.START_DATE TRANS.END_DATE
> DIVISION FASB
> "integer" "character" "character"
> "character" "character"
> I thought it's just a matter of applying with a as.Date,
>
> df[,c(2,3)] = apply(df[,c(2,3)], 2, function(x)as.Date(x,"%m/%d/%Y"))
>
> Well, the Date conversion fails and I got,
>
> TRANSNO TRANS.START_DATE TRANS.END_DATE DIVISION FASB
> 1 250897 14791 14821 PSTRUCT Z
> 2 250617 14822 14852 PSTRUCT Z
> 3 250364 15065 15155 PLR Z
> 4 250176 15065 15155 PLR Z
> 5 250176 15065 15155 PLR Z
> 6 250364 15065 15155 PLR Z
> The character columns are indeed converted, but they became
> integer, not Date type. OK, that's strange and so I started
> reading the help pages.
>
> It turns out in apply, the result is coerced to some basic
> vector types. And apparently Date is not one of the basic
> vector types.
>
> "In all cases the result is coerced by
> as.vector<http://127.0.0.1:19182/library/base/help/as.vector>
> to one of the basic vector types before the dimensions are
> set, so that (for example) factor results will be coerced to
> a character array. "
>
> The question then is how type conversion can be carried out
> on some columns of a data frame without using a loop.
>
> Thanks.
>
> Horace Tso
>
>
>
>
>
>
>
>
> [[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.
>
More information about the R-help
mailing list