[R] ddply - how to transform df column in place
Dear R-users, I am trying to get the plyr syntax right, without much success. Given: d- data.frame(cbind(x=1,y=seq(20100801,20100830,1))) names(d)-c(first, daterep) d2-d # I can convert the daterep column in place the classic way: d$daterep-as.Date(strptime(d$daterep, format=%Y%m%d)) # How to do it the plyr way? ddply(d2, c(daterep), function(df){as.Date(df, format=%Y%m%d)}) # returns: Error in as.Date.default(df, format = %Y%m%d) : # do not know how to convert 'df' to class Date Thanks for any hints, ---jean -- View this message in context: http://r.789695.n4.nabble.com/ddply-how-to-transform-df-column-in-place-tp3764037p3764037.html Sent from the R help mailing list archive at Nabble.com. __ 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] ddply - how to transform df column in place
Hi Jean, On Tue, Aug 23, 2011 at 6:16 PM, jjap jean.plamon...@fpinnovations.ca wrote: Dear R-users, I am trying to get the plyr syntax right, without much success. Given: d- data.frame(cbind(x=1,y=seq(20100801,20100830,1))) names(d)-c(first, daterep) d2-d # I can convert the daterep column in place the classic way: d$daterep-as.Date(strptime(d$daterep, format=%Y%m%d)) # How to do it the plyr way? ddply(d2, c(daterep), function(df){as.Date(df, format=%Y%m%d)}) # returns: Error in as.Date.default(df, format = %Y%m%d) : # do not know how to convert 'df' to class Date There is no plyr way to do this, as this is not the kind of operation plyr is designed to carry out. plyr is designed to simplify the process of 1) splitting the data into groups, 2) operating on each group, and 3) putting the results back together. Since your example does not involve this kind of split-apply-combine approach plyr will not help you. HTH, Ista Thanks for any hints, ---jean -- View this message in context: http://r.789695.n4.nabble.com/ddply-how-to-transform-df-column-in-place-tp3764037p3764037.html Sent from the R help mailing list archive at Nabble.com. __ 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. -- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org __ 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] ddply - how to transform df column in place
Jean, Ista is right, but: In your function you are asking as.Date to convert the whole data.frame df rather than just your daterep column. out-ddply(d2, .(daterep), function(df) as.Date(strptime(df$daterep,format='%Y%m%d'))) str(out) 'data.frame':30 obs. of 2 variables: $ daterep: num 20100801 20100802 20100803 20100804 20100805 ... $ V1 : Date, format: 2010-08-01 2010-08-02 2010-08-03 2010-08-04 ... On Tue, Aug 23, 2011 at 3:16 PM, jjap jean.plamon...@fpinnovations.cawrote: Dear R-users, I am trying to get the plyr syntax right, without much success. Given: d- data.frame(cbind(x=1,y=seq(20100801,20100830,1))) names(d)-c(first, daterep) d2-d # I can convert the daterep column in place the classic way: d$daterep-as.Date(strptime(d$daterep, format=%Y%m%d)) # How to do it the plyr way? ddply(d2, c(daterep), function(df){as.Date(df, format=%Y%m%d)}) # returns: Error in as.Date.default(df, format = %Y%m%d) : # do not know how to convert 'df' to class Date Thanks for any hints, ---jean -- View this message in context: http://r.789695.n4.nabble.com/ddply-how-to-transform-df-column-in-place-tp3764037p3764037.html Sent from the R help mailing list archive at Nabble.com. __ 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] ddply - how to transform df column in place
On Aug 23, 2011, at 6:16 PM, jjap wrote: Dear R-users, I am trying to get the plyr syntax right, without much success. Given: d- data.frame(cbind(x=1,y=seq(20100801,20100830,1))) names(d)-c(first, daterep) d2-d # I can convert the daterep column in place the classic way: d$daterep-as.Date(strptime(d$daterep, format=%Y%m%d)) # How to do it the plyr way? ddply(d2, c(daterep), function(df){as.Date(df, format=%Y%m%d)}) # returns: Error in as.Date.default(df, format = %Y%m%d) : # do not know how to convert 'df' to class Date I'm pretty sure that when you do that you are sending dataframes to 'x' that have been split apart on the basis of their daterep values. Why should there be a plyr way of converting types when there is no splitting of dataframes needed? If you wanted nevertheless to do it the plyr way: d2 - ddply(d, NULL , transform, daterep = as.Date(strptime(d$daterep, format=%Y%m%d)) -- David Winsemius, MD West Hartford, CT __ 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] ddply - how to transform df column in place
On Aug 23, 2011, at 6:38 PM, David Winsemius wrote: On Aug 23, 2011, at 6:16 PM, jjap wrote: Dear R-users, I am trying to get the plyr syntax right, without much success. Given: d- data.frame(cbind(x=1,y=seq(20100801,20100830,1))) names(d)-c(first, daterep) d2-d # I can convert the daterep column in place the classic way: d$daterep-as.Date(strptime(d$daterep, format=%Y%m%d)) # How to do it the plyr way? ddply(d2, c(daterep), function(df){as.Date(df, format=%Y%m%d)}) # returns: Error in as.Date.default(df, format = %Y%m%d) : # do not know how to convert 'df' to class Date I'm pretty sure that when you do that you are sending dataframes to 'x' that have been split apart on the basis of their daterep values. Why should there be a plyr way of converting types when there is no splitting of dataframes needed? If you wanted nevertheless to do it the plyr way: d2 - ddply(d, NULL , transform, daterep = as.Date(strptime(d$daterep, format=%Y%m%d)) Make that: d2 - ddply(d, NULL , transform, daterep = as.Date(strptime(daterep, format=%Y%m%d)) ) d2# you do get an unnecessary column -- David Winsemius, MD West Hartford, CT __ 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. David Winsemius, MD West Hartford, CT __ 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.