Jason Rupert
2009-Jun-04 02:45 UTC
[R] Getting a column of values from a list - think I'm doing it the hard way
Example code it shown below. I think I am doing this the hard way. I'm just trying to get the full year value from an array of dates. An example array is shown below. Right now, I'm using a "for" loop to pull the year out of a list where the dates were split up into their individual components. This seems to work, but just wondering if there is an easier way. Thanks for any insights. #*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~ HouseDates <- c("02/27/90", "02/27/91", "01/14/92", "02/28/93", "02/01/94", "02/01/95", "02/01/96") # ?as.Date HouseDatesFormatted<-as.Date(HouseDates, "%m/%d/%y") HouseDatesFormatted HouseDatesList<-strsplit(as.character(HouseDatesFormatted), "-", fixed=TRUE) HouseYear_array<-NULL length_array<-length(HouseDatesList) for(ii in 1:length_array) { HouseYear<-HouseDatesList[[ii]][1] HouseYear_array<-c(HouseYear_array, HouseYear) } as.character(HouseYear_array) # Desired: # [1] "1990" "1991" "1992" "1993" "1994" "1995" "1996"
Ronggui Huang
2009-Jun-04 02:57 UTC
[R] Getting a column of values from a list - think I'm doing it the hard way
2009/6/4 Jason Rupert <jasonkrupert at yahoo.com>:> > Example code it shown below. > > I think I am doing this the hard way. ?I'm just trying to get the full year value from an array of dates. ?An example array is shown below. ?Right now, I'm using a "for" loop to pull the year out of a list where the dates were split up into their individual components. > > This seems to work, but just wondering if there is an easier way. > > Thanks for any insights. > > #*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~ > HouseDates <- c("02/27/90", "02/27/91", "01/14/92", "02/28/93", "02/01/94", "02/01/95", "02/01/96") > > # ?as.Date > HouseDatesFormatted<-as.Date(HouseDates, "%m/%d/%y") > > HouseDatesFormatted > > HouseDatesList<-strsplit(as.character(HouseDatesFormatted), "-", fixed=TRUE)> sapply(HouseDatesList,function(x) x[[1]])[1] "1990" "1991" "1992" "1993" "1994" "1995" "1996"> HouseYear_array<-NULL > length_array<-length(HouseDatesList) > for(ii in 1:length_array) > { > ? ? ? ?HouseYear<-HouseDatesList[[ii]][1] > > ? ? ? ?HouseYear_array<-c(HouseYear_array, HouseYear) > } > > as.character(HouseYear_array) > > # Desired: > # [1] "1990" "1991" "1992" "1993" "1994" "1995" "1996" > > ______________________________________________ > 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. >-- HUANG Ronggui, Wincent PhD Candidate Dept of Public and Social Administration City University of Hong Kong Home page: http://asrr.r-forge.r-project.org/rghuang.html
Marc Schwartz
2009-Jun-04 03:02 UTC
[R] Getting a column of values from a list - think I'm doing it the hard way
On Jun 3, 2009, at 9:45 PM, Jason Rupert wrote:> > Example code it shown below. > > I think I am doing this the hard way. I'm just trying to get the > full year value from an array of dates. An example array is shown > below. Right now, I'm using a "for" loop to pull the year out of a > list where the dates were split up into their individual components. > > This seems to work, but just wondering if there is an easier way. > > Thanks for any insights. > > #*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~ > HouseDates <- c("02/27/90", "02/27/91", "01/14/92", "02/28/93", > "02/01/94", "02/01/95", "02/01/96") > > # ?as.Date > HouseDatesFormatted<-as.Date(HouseDates, "%m/%d/%y") > > HouseDatesFormatted > > HouseDatesList<-strsplit(as.character(HouseDatesFormatted), "-", > fixed=TRUE) > > HouseYear_array<-NULL > length_array<-length(HouseDatesList) > for(ii in 1:length_array) > { > HouseYear<-HouseDatesList[[ii]][1] > > HouseYear_array<-c(HouseYear_array, HouseYear) > } > > as.character(HouseYear_array) > > # Desired: > # [1] "1990" "1991" "1992" "1993" "1994" "1995" "1996"Yep, definitely an easier way: HouseDates <- c("02/27/90", "02/27/91", "01/14/92", "02/28/93", "02/01/94", "02/01/95", "02/01/96") # You can convert the entire *vector* to a Date and then format the result to Year only. # See ?format.Date > format(as.Date(HouseDates, "%m/%d/%y"), "%Y") [1] "1990" "1991" "1992" "1993" "1994" "1995" "1996" One of the key things to bear in mind about R, is that in general (there are exceptions), avoiding explicit 'for' loops is a paradigm. R is by design, a vectorized language. That means that many operations are designed to take a vector, matrix or array and operate on it in a "whole object" fashion. Essentially, iterate an operation over the entire object with a single function call. If you are coming from a different programming language, this is perhaps one of the more challenging perspectives to achieve. But once you make that leap, you will look back and be amazed at how you ever managed to get things before (and how many lines of code it took...) If you have not, read through 'An Introduction to R', provided with your R distribution or on the R web site. It's a good place to start and there are other contributed materials and books listed on the R web site that can supplement that as you progress. HTH, Marc Schwartz
ONKELINX, Thierry
2009-Jun-04 08:02 UTC
[R] Getting a column of values from a list - think I'm doing it thehard way
Dear Jason, Have a look at years() from the chron package. library(chron) HouseDates <- c("02/27/90", "02/27/91", "01/14/92", "02/28/93", "02/01/94", "02/01/95", "02/01/96") HouseDatesFormatted<-as.Date(HouseDates, "%m/%d/%y") years(HouseDates) HTH, Thierry ------------------------------------------------------------------------ ---- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx at inbo.be www.inbo.be To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey -----Oorspronkelijk bericht----- Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Namens Jason Rupert Verzonden: donderdag 4 juni 2009 4:45 Aan: R-help at r-project.org Onderwerp: [R] Getting a column of values from a list - think I'm doing it thehard way Example code it shown below. I think I am doing this the hard way. I'm just trying to get the full year value from an array of dates. An example array is shown below. Right now, I'm using a "for" loop to pull the year out of a list where the dates were split up into their individual components. This seems to work, but just wondering if there is an easier way. Thanks for any insights. #*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~ HouseDates <- c("02/27/90", "02/27/91", "01/14/92", "02/28/93", "02/01/94", "02/01/95", "02/01/96") # ?as.Date HouseDatesFormatted<-as.Date(HouseDates, "%m/%d/%y") HouseDatesFormatted HouseDatesList<-strsplit(as.character(HouseDatesFormatted), "-", fixed=TRUE) HouseYear_array<-NULL length_array<-length(HouseDatesList) for(ii in 1:length_array) { HouseYear<-HouseDatesList[[ii]][1] HouseYear_array<-c(HouseYear_array, HouseYear) } as.character(HouseYear_array) # Desired: # [1] "1990" "1991" "1992" "1993" "1994" "1995" "1996" ______________________________________________ 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. Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document. The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document.