Hi, I have a problem with the time formatting in R. I have entered time in the format "MM:SS.xyz" and R has automatically classified this as a factor, but I need it numerically. However when I use as.numeric() it gives me totally different numbers. Is there any way I can tell R to read thes input as a number? Thank you very much [[alternative HTML version deleted]]
> # depends on what you want to do with it. > # if you just want to convert to seconds, use the following > x <- c('12:23.45', '34:15.1', '1:34.23') > # split by the ":" > x.s <- strsplit(x, ":") > # convert > x.c <- sapply(x.s, function(a){+ as.numeric(a[1]) * 60 + as.numeric(a[2]) + })> > > x.c[1] 743.45 2055.10 94.23>On Tue, Jul 20, 2010 at 7:33 AM, Sarah Chisholm <sarah.chisholm.09 at ucl.ac.uk> wrote:> Hi, > > I have a problem with the time formatting in R. I have entered time in the format "MM:SS.xyz" and R has automatically classified this as a factor, but I need it numerically. However when I use as.numeric() it gives me totally different numbers. Is there any way I can tell R to read thes input as a number? > > Thank you very much > > ? ? ? ?[[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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
On Jul 20, 2010, at 7:33 AM, Sarah Chisholm wrote:> Hi, > > I have a problem with the time formatting in R. I have entered time > in the format "MM:SS.xyz" and R has automatically classified this as > a factor, but I need it numerically. However when I use as.numeric() > it gives me totally different numbers. Is there any way I can tell R > to read thes input as a number?There are ways using As methods of doing so, but I think those is a bit more complex than a learner at you level should be asked to implement. Guessing that you used one of the read functions to bring in the data (or possibly used hte data.frame function) , you are probably experiencing the effects of the default stringAsFactors=TRUE behavior. Try setting stringsAsFactors to FALSE. Then you can use the standard Date functions: ?Dates -- David Winsemius, MD West Hartford, CT
On Jul 20, 2010, at 8:41 AM, David Winsemius wrote:> > On Jul 20, 2010, at 7:33 AM, Sarah Chisholm wrote: > >> Hi, >> >> I have a problem with the time formatting in R. I have entered time >> in the format "MM:SS.xyz" and R has automatically classified this >> as a factor, but I need it numerically. However when I use >> as.numeric() it gives me totally different numbers. Is there any >> way I can tell R to read thes input as a number? > > There are ways using As methods of doing so, but I think those is a > bit more complex than a learner at you level should be asked to > implement. Guessing that you used one of the read functions to bring > in the data (or possibly used hte data.frame function) , you are > probably experiencing the effects of the default > stringAsFactors=TRUE behavior. Try setting stringsAsFactors to > FALSE. Then you can use the standard Date functions: > > ?DatesI should have said: ? DateTimeClasses>-- David Winsemius, MD West Hartford, CT
Ms. Chisholm, If you could tell us how you plan to use the variables, we will have a better understanding of what you are looking for and will be able to help you. Are you looking for the time in seconds? In that case, do as Mr. Holfman says. He just skipped the part about converting the factors to characters. You can do that by: y <- as.character(x) where x is the vector of factors. Are you looking to have a list of hours, minutes and seconds? That can be done too...Although it would be much easier to just have hours and min.sec On Tue, Jul 20, 2010 at 7:33 AM, Sarah Chisholm <sarah.chisholm.09@ucl.ac.uk> wrote:> Hi, > > I have a problem with the time formatting in R. I have entered time in the > format "MM:SS.xyz" and R has automatically classified this as a factor, but > I need it numerically. However when I use as.numeric() it gives me totally > different numbers. Is there any way I can tell R to read thes input as a > number? > > Thank you very much > > [[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. >-- Aaditya Nanduri aaditya.nanduri@gmail.com [[alternative HTML version deleted]]
You can use strptime to specify the format of the date and time you want, e.g.> x1<-strptime(x, "%Y-%m-%d %H:%M:%S") > x1[1] "2010-04-02 12:00:05"> str(x1)POSIXlt[1:1], format: "2010-04-02 12:00:05" On Wed, Jul 21, 2010 at 8:02 AM, Aaditya Nanduri <aaditya.nanduri at gmail.com> wrote:> Ms. Chisholm, > > If you could tell us how you plan to use the variables, we will have a > better understanding of what you are looking for and will be able to help > you. > Are you looking for the time in seconds? In that case, do as Mr. Holfman > says. He just skipped the part about converting the factors to characters. > You can do that by: > y <- as.character(x) where x is the vector of factors. > > Are you looking to have a list of hours, minutes and seconds? That can be > done too...Although it would be much easier to just have hours and min.sec > > On Tue, Jul 20, 2010 at 7:33 AM, Sarah Chisholm <sarah.chisholm.09 at ucl.ac.uk >> wrote: > >> Hi, >> >> I have a problem with the time formatting in R. I have entered time in the >> format "MM:SS.xyz" and R has automatically classified this as a factor, but >> I need it numerically. However when I use as.numeric() it gives me totally >> different numbers. Is there any way I can tell R to read thes input as a >> number? >> >> Thank you very much >> >> ? ? ? ?[[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. >> > > > > -- > Aaditya Nanduri > aaditya.nanduri at gmail.com > > ? ? ? ?[[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. >