hello, how can I do to drop C from this character "C325" ? _____________________________________________________________________________ [[alternative HTML version deleted]]
elyakhlifi mustapha wrote:> hello, > how can I do to drop C from this character "C325" ?> x <- "C325"> substring(x, first=2)[1] "325"> gsub("C", "", x)[1] "325"> gsub("[A-Z]", "", x)[1] "325" ?substring ?gsub> _____________________________________________________________________________ > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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.-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
elyakhlifi mustapha wrote:> > hello, > how can I do to drop C from this character "C325" ? >1. if C is always single and always first:> substring("C325",2)2. more generic solution, drops all letters> sp<-unlist(strsplit("C325",split="[A-Z]")) > sp<-sp[nchar(sp)>0] > sp-- View this message in context: http://www.nabble.com/drop-a-letter-tf3763392.html#a10638376 Sent from the R help mailing list archive at Nabble.com.
Dear R-ians I have a data frame like Person_id Date/time Count --------- ---------- ------- 123 20 May 1999 1 123 21 May 1999 3 222 1 Feb 2000 2 222 3 Feb 2000 4 I want to create ts objects for each person_id (i.e. 123 and 222). Time series frequency can be in months and all starting from the same date (i.e. May 1999). Rather than manually creating sum of counts for each month and creating ts objects manually for each persons, Is there a function for creating ts objects automatically from such data? Thanks, Faith OZGUL
On 5/16/07, fatih ozgul <fatih.ozgul at gmail.com> wrote:> Dear R-ians > > I have a data frame like > > Person_id Date/time Count > --------- ---------- ------- > 123 20 May 1999 1 > 123 21 May 1999 3 > 222 1 Feb 2000 2 > 222 3 Feb 2000 4 > > I want to create ts objects for each person_id (i.e. 123 and 222). Time > series frequency can be in months and all starting from the same date (i.e. > May 1999). > > Rather than manually creating sum of counts for each month and creating ts > objects manually for each persons, > > Is there a function for creating ts objects automatically from such data? >Try this: # read in as data frame and fix up date Lines.raw <- "Person_id Date/time Count 123 20 May 1999 1 123 21 May 1999 3 222 1 Feb 2000 2 222 3 Feb 2000 4 " library(zoo) DF <- read.table(textConnection(Lines.raw), skip = 1, col.names = c("Person_id", "d", "b", "Y", "Count")) DF$Date.time <- as.Date(paste(DF$d, DF$b, DF$Y), "%d %b %Y") # aggregate counts over months, series and convert to "ts" f <- function(DF) aggregate(zoo(DF$Count), as.yearmon(DF$Date.time), sum) z <- do.call("merge.zoo", lapply(split(DF, DF$Person_id), f)) frequency(z) <- 12 as.ts(z)
On 5/16/07, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:> On 5/16/07, fatih ozgul <fatih.ozgul at gmail.com> wrote: > > Dear R-ians > > > > I have a data frame like > > > > Person_id Date/time Count > > --------- ---------- ------- > > 123 20 May 1999 1 > > 123 21 May 1999 3 > > 222 1 Feb 2000 2 > > 222 3 Feb 2000 4 > > > > I want to create ts objects for each person_id (i.e. 123 and 222). Time > > series frequency can be in months and all starting from the same date (i.e. > > May 1999). > > > > Rather than manually creating sum of counts for each month and creating ts > > objects manually for each persons, > > > > Is there a function for creating ts objects automatically from such data? > > > > Try this: > > # read in as data frame and fix up date > Lines.raw <- "Person_id Date/time Count > 123 20 May 1999 1 > 123 21 May 1999 3 > 222 1 Feb 2000 2 > 222 3 Feb 2000 4 > " > library(zoo) > DF <- read.table(textConnection(Lines.raw), skip = 1, > col.names = c("Person_id", "d", "b", "Y", "Count")) > DF$Date.time <- as.Date(paste(DF$d, DF$b, DF$Y), "%d %b %Y") > > # aggregate counts over months, series and convert to "ts" > f <- function(DF) aggregate(zoo(DF$Count), as.yearmon(DF$Date.time), sum) > z <- do.call("merge.zoo", lapply(split(DF, DF$Person_id), f)) > frequency(z) <- 12 > as.ts(z) >A slight simplification based on zoo 1.3-1 (which just appeared on CRAN) is the following. It converts the month and year columns directly into class "yearmon" eliminating the intermediate step of going via "Date" class. Its the same number of lines but a couple of the lines are now simpler. # read in as data frame and fix up date Lines.raw <- "Person_id Date/time Count 123 20 May 1999 1 123 21 May 1999 3 222 1 Feb 2000 2 222 3 Feb 2000 4 " library(zoo) DF <- read.table(textConnection(Lines.raw), skip = 1, col.names = c("Person_id", "d", "b", "Y", "Count")) DF$yearmon <- as.yearmon(paste(DF$b, DF$Y), "%b %Y") # aggregate counts over months, merge series and convert to "ts" ag <- function(DF) aggregate(zoo(DF$Count), DF$yearmon, sum) z <- do.call("merge.zoo", lapply(split(DF, DF$Person_id), ag)) frequency(z) <- 12 as.ts(z)