Hello there, I was wondering if you could help me with a quick R issue. I have a data set where one of the columns has both date and time in it, e.g. "12/31/11 23:45" in one cell. I want to use R to split this column into two new columns: date and time. One of the problems with splitting here is that when the dates go into single digits there are no 0's in front of months January-September (e.g., January is represented by 1 as opposed to 01), so every entry is a different length. Therefore, splitting by the space is the only option, I think. Here's the coding I've developed thus far: z$dt <- z$Date #time and date is all under z$Date foo <- strsplit(" ", z$dt) #attempted split based on the space And then if that were to work, I would proceed use the coding: foo2 <- matrix(unlist(foo), ncol = 2, byrow=TRUE) z$Date <- foo[ ,1] z$Time <- foo[ ,2] However, foo <- strsplit(" ", z$dt) isn't working. Do you know what the problem is? If you could respond soon, that would be greatly appreciated! Thanks so much! Alex
Hi Alex, Here is one way: s <- "12/31/11 23:45" strsplit(s, " ")[[1]] # [1] "12/31/11" "23:45" * HTH, Jorge.- * On Wed, May 2, 2012 at 4:00 PM, Alex Roth <> wrote:> Hello there, I was wondering if you could help me with a quick R issue. > > I have a data set where one of the columns has both date and time in > it, e.g. "12/31/11 23:45" in one cell. I want to use R to split this > column into two new columns: date and time. > > One of the problems with splitting here is that when the dates go into > single digits there are no 0's in front of months January-September > (e.g., January is represented by 1 as opposed to 01), so every entry > is a different length. Therefore, splitting by the space is the only > option, I think. > > Here's the coding I've developed thus far: > > z$dt <- z$Date #time and date is all under z$Date > foo <- strsplit(" ", z$dt) #attempted split based on the space > > And then if that were to work, I would proceed use the coding: > > foo2 <- matrix(unlist(foo), ncol = 2, byrow=TRUE) > z$Date <- foo[ ,1] > z$Time <- foo[ ,2] > > However, foo <- strsplit(" ", z$dt) isn't working. Do you know what > the problem is? If you could respond soon, that would be greatly > appreciated! > > Thanks so much! > Alex > > ______________________________________________ > 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]]
On 02-05-2012, at 22:00, Alex Roth wrote:> Hello there, I was wondering if you could help me with a quick R issue. > > I have a data set where one of the columns has both date and time in > it, e.g. "12/31/11 23:45" in one cell. I want to use R to split this > column into two new columns: date and time. > > One of the problems with splitting here is that when the dates go into > single digits there are no 0's in front of months January-September > (e.g., January is represented by 1 as opposed to 01), so every entry > is a different length. Therefore, splitting by the space is the only > option, I think. > > Here's the coding I've developed thus far: > > z$dt <- z$Date #time and date is all under z$Date > foo <- strsplit(" ", z$dt) #attempted split based on the space > > And then if that were to work, I would proceed use the coding: > > foo2 <- matrix(unlist(foo), ncol = 2, byrow=TRUE) > z$Date <- foo[ ,1] > z$Time <- foo[ ,2] > > However, foo <- strsplit(" ", z$dt) isn't working. Do you know what > the problem is? If you could respond soon, that would be greatly > appreciated!Have a look at the help for strsplit. You seem to have the arguments of strsplit the wrong way around. strsplit(z$dt, " ") Berend
Hard to say.. your example is not reproducible. (Study the Posting Guide mentioned at the end of every message on this list.) A stab in the dark might be that z$dt is a factor and needs to be converted to character first. Use the str function to study your data. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. Alex Roth <alexsroth1 at gmail.com> wrote:>Hello there, I was wondering if you could help me with a quick R issue. > >I have a data set where one of the columns has both date and time in >it, e.g. "12/31/11 23:45" in one cell. I want to use R to split this >column into two new columns: date and time. > >One of the problems with splitting here is that when the dates go into >single digits there are no 0's in front of months January-September >(e.g., January is represented by 1 as opposed to 01), so every entry >is a different length. Therefore, splitting by the space is the only >option, I think. > >Here's the coding I've developed thus far: > >z$dt <- z$Date #time and date is all under z$Date >foo <- strsplit(" ", z$dt) #attempted split based on the space > >And then if that were to work, I would proceed use the coding: > >foo2 <- matrix(unlist(foo), ncol = 2, byrow=TRUE) >z$Date <- foo[ ,1] >z$Time <- foo[ ,2] > >However, foo <- strsplit(" ", z$dt) isn't working. Do you know what >the problem is? If you could respond soon, that would be greatly >appreciated! > >Thanks so much! >Alex > >______________________________________________ >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.
So long as x is a character vector, I tend to use the following for this problem.> x <- c("12/31/11 23:45", "1/1/12 2:15") > > x.split <- strsplit(x, " ") > > x.date <- sapply(x.split, function(y) return(y[1])) > x.time <- sapply(x.split, function(y) if (length(y) > 1) return(y[2]) else NA) > > x.date[1] "12/31/11" "1/1/12"> x.time[1] "23:45" "2:15">? Benjamin Nutter |??Biostatistician ? |??Quantitative Health Sciences ? Cleveland Clinic? | ?9500 Euclid Ave.? | ?Cleveland, OH 44195? |?(216) 445-1365 -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Alex Roth Sent: Wednesday, May 02, 2012 4:01 PM To: r-help at r-project.org Subject: [R] R help! Hello there, I was wondering if you could help me with a quick R issue. I have a data set where one of the columns has both date and time in it, e.g. "12/31/11 23:45" in one cell. I want to use R to split this column into two new columns: date and time. One of the problems with splitting here is that when the dates go into single digits there are no 0's in front of months January-September (e.g., January is represented by 1 as opposed to 01), so every entry is a different length. Therefore, splitting by the space is the only option, I think. Here's the coding I've developed thus far: z$dt <- z$Date #time and date is all under z$Date foo <- strsplit(" ", z$dt) #attempted split based on the space And then if that were to work, I would proceed use the coding: foo2 <- matrix(unlist(foo), ncol = 2, byrow=TRUE) z$Date <- foo[ ,1] z$Time <- foo[ ,2] However, foo <- strsplit(" ", z$dt) isn't working. Do you know what the problem is? If you could respond soon, that would be greatly appreciated! Thanks so much! Alex ______________________________________________ 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. ================================== Please consider the environment before printing this e-mail Cleveland Clinic is ranked one of the top hospitals in America by U.S.News & World Report (2010). Visit us online at http://www.clevelandclinic.org for a complete listing of our services, staff and locations. Confidentiality Note: This message is intended for use\...{{dropped:13}}
Hi I would convert it to propper date format and then you can extract anything. dat<-strptime("12/31/11 23:45", format="%m/%d/%y %H:%M") as.Date(dat) [1] "2011-12-31" format(dat, "%H:%M") [1] "23:45" Regards Petr> > Hello there, I was wondering if you could help me with a quick R issue. > > I have a data set where one of the columns has both date and time in > it, e.g. "12/31/11 23:45" in one cell. I want to use R to split this > column into two new columns: date and time. > > One of the problems with splitting here is that when the dates go into > single digits there are no 0's in front of months January-September > (e.g., January is represented by 1 as opposed to 01), so every entry > is a different length. Therefore, splitting by the space is the only > option, I think. > > Here's the coding I've developed thus far: > > z$dt <- z$Date #time and date is all under z$Date > foo <- strsplit(" ", z$dt) #attempted split based on the space > > And then if that were to work, I would proceed use the coding: > > foo2 <- matrix(unlist(foo), ncol = 2, byrow=TRUE) > z$Date <- foo[ ,1] > z$Time <- foo[ ,2] > > However, foo <- strsplit(" ", z$dt) isn't working. Do you know what > the problem is? If you could respond soon, that would be greatly > appreciated! > > Thanks so much! > Alex > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.