#Hello, #I loaded data using read.table - I needed to convert a row in the data frame to date class:> dataV1 V2 V3 V4 1 2008-05-19 2008-04-19 2008-03-21 2008-02-22 2 38.16999817 30.70999908 36.86000061 35.18999863 3 37.47999954 29.95000076 36.45999908 35.36000061 4 37.47999954 30.14999962 36.47000122 35.36000061 5 37.84999847 30.56999969 36.84000015 35.74000168 6 38.38999939 31.14999962 37.34000015 36.27000046 7 39.11000061 31.90999985 38.02999878 36.97999954 8 39.81000137 32.65000153 38.68000031 37.63999939 9 40.47000122 33.34999847 39.27999878 38.27000046> data[1,]V1 V2 V3 V4 1 2008-05-19 2008-04-19 2008-03-21 2008-02-22> dates1<-as.Date(data[1,])do not know how to convert 'dates[1,]' to class "Date" # However, I can individually convert them all:> dates1.1<-as.Date(data[1,1])> dates1.1[1] "2008-05-19" # How can I change the class of the date over the entire row (my original file contains more than one hundred rows)? # Thank you very much. -- View this message in context: http://old.nabble.com/as.Date-from-data.frame-tp26305830p26305830.html Sent from the R help mailing list archive at Nabble.com.
Hi, On Nov 11, 2009, at 1:06 PM, separent wrote:> > #Hello, > > #I loaded data using read.table - I needed to convert a row in the > data > frame to date class: > >> data > > V1 V2 V3 V4 > 1 2008-05-19 2008-04-19 2008-03-21 2008-02-22 > 2 38.16999817 30.70999908 36.86000061 35.18999863 > 3 37.47999954 29.95000076 36.45999908 35.36000061 > 4 37.47999954 30.14999962 36.47000122 35.36000061 > 5 37.84999847 30.56999969 36.84000015 35.74000168 > 6 38.38999939 31.14999962 37.34000015 36.27000046 > 7 39.11000061 31.90999985 38.02999878 36.97999954 > 8 39.81000137 32.65000153 38.68000031 37.63999939 > 9 40.47000122 33.34999847 39.27999878 38.27000046 > >> data[1,] > V1 V2 V3 V4 > 1 2008-05-19 2008-04-19 2008-03-21 2008-02-22 > >> dates1<-as.Date(data[1,])I think this should work if data[1,] returns a vector of characters: R> d <- c("2008-05-19", "2008-05-30") R> dd <- as.Date(d) R> is(dd) [1] "Date" "oldClass" Is your first row being treated as factors? Maybe if you convert to character first it should work? R> dates1<-as.Date(as.character(data[1,])) But the fact that it works for as.Date(data[1,1]) make it a bit weird ... -steve> do not know how to convert 'dates[1,]' to class "Date" > > # However, I can individually convert them all: > >> dates1.1<-as.Date(data[1,1]) > >> dates1.1 > [1] "2008-05-19" > > # How can I change the class of the date over the entire row (my > original > file contains more than one hundred rows)? > # Thank you very much. > -- > View this message in context: http://old.nabble.com/as.Date-from-data.frame-tp26305830p26305830.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.-- Steve Lianoglou Graduate Student: Computational Systems Biology | Memorial Sloan-Kettering Cancer Center | Weill Medical College of Cornell University Contact Info: http://cbio.mskcc.org/~lianos/contact
On Nov 11, 2009, at 12:06 PM, separent wrote:> > #Hello, > > #I loaded data using read.table - I needed to convert a row in the > data > frame to date class: > >> data > > V1 V2 V3 V4 > 1 2008-05-19 2008-04-19 2008-03-21 2008-02-22 > 2 38.16999817 30.70999908 36.86000061 35.18999863 > 3 37.47999954 29.95000076 36.45999908 35.36000061 > 4 37.47999954 30.14999962 36.47000122 35.36000061 > 5 37.84999847 30.56999969 36.84000015 35.74000168 > 6 38.38999939 31.14999962 37.34000015 36.27000046 > 7 39.11000061 31.90999985 38.02999878 36.97999954 > 8 39.81000137 32.65000153 38.68000031 37.63999939 > 9 40.47000122 33.34999847 39.27999878 38.27000046 > >> data[1,] > V1 V2 V3 V4 > 1 2008-05-19 2008-04-19 2008-03-21 2008-02-22 > >> dates1<-as.Date(data[1,]) > > do not know how to convert 'dates[1,]' to class "Date" > > # However, I can individually convert them all: > >> dates1.1<-as.Date(data[1,1]) > >> dates1.1 > [1] "2008-05-19" > > # How can I change the class of the date over the entire row (my > original > file contains more than one hundred rows)? > # Thank you very much.In a data frame, you cannot change the data type for a row, without changing the data type for the entire column. That being said, it looks to me like each column contains continuous data for a chronological series. If that is correct, then you want the dates to be the column names and not the first row., Thus, when you used read.table(), you should have included the argument 'header = TRUE', which would do just that. With read.table(), header is FALSE by default. HTH, Marc Schwartz
On Nov 11, 2009, at 1:06 PM, separent wrote:> > #Hello, > > #I loaded data using read.table - I needed to convert a row in the > data > frame to date class:You _should_ have given the actual code (as the Posting Guide requests), but I would guess that you should have added either as.is=TRUE or stringsAsFactors=FALSE. As it is, you probably have all factors.> >> data > > V1 V2 V3 V4 > 1 2008-05-19 2008-04-19 2008-03-21 2008-02-22 > 2 38.16999817 30.70999908 36.86000061 35.18999863 > 3 37.47999954 29.95000076 36.45999908 35.36000061 > 4 37.47999954 30.14999962 36.47000122 35.36000061 > 5 37.84999847 30.56999969 36.84000015 35.74000168 > 6 38.38999939 31.14999962 37.34000015 36.27000046 > 7 39.11000061 31.90999985 38.02999878 36.97999954 > 8 39.81000137 32.65000153 38.68000031 37.63999939 > 9 40.47000122 33.34999847 39.27999878 38.27000046 > >> data[1,] > V1 V2 V3 V4 > 1 2008-05-19 2008-04-19 2008-03-21 2008-02-22 > >> dates1<-as.Date(data[1,]) > > do not know how to convert 'dates[1,]' to class "Date"You should have given the actual error message which was probably more informative than what your brain interpreted it to mean.> > # However, I can individually convert them all: > >> dates1.1<-as.Date(data[1,1]) > >> dates1.1 > [1] "2008-05-19" > > # How can I change the class of the date over the entire row (my > original > file contains more than one hundred rows)?Off hand I would say you want to work on a transpose of this data. You cannot mix datatypes within columns and it seems fairly clear that the rows below number 1 are numeric. Back to the drawing board.>-- David Winsemius, MD Heritage Laboratories West Hartford, CT
Hello Gentlemen, All of your answers were helpfull. Indeed, 'as.is=TRUE' or 'stringsAsFactors=FALSE' avoid importing as factors, as it did when I imported the table without specifying anything. However, data frames will not allow different datatypes within a single column, so, for time series, I should work with a transpose of the table. However, when the time series is the label of a profile, I should read the table with 'header = TRUE'. In other words: * Plotting values of a column (let's say againts observation number) for a selected time (where time is the name of the series): import data with 'header = TRUE' * Plotting the evolution of the values for a single observation number against time: 'header = TRUE' & transpose & 'as.is=TRUE' Kind Regards, Serge-?tienne Parent Golder Associ?s Canada separent wrote:> > #Hello, > > #I loaded data using read.table - I needed to convert a row in the data > frame to date class: > >> data > > V1 V2 V3 V4 > 1 2008-05-19 2008-04-19 2008-03-21 2008-02-22 > 2 38.16999817 30.70999908 36.86000061 35.18999863 > 3 37.47999954 29.95000076 36.45999908 35.36000061 > 4 37.47999954 30.14999962 36.47000122 35.36000061 > 5 37.84999847 30.56999969 36.84000015 35.74000168 > 6 38.38999939 31.14999962 37.34000015 36.27000046 > 7 39.11000061 31.90999985 38.02999878 36.97999954 > 8 39.81000137 32.65000153 38.68000031 37.63999939 > 9 40.47000122 33.34999847 39.27999878 38.27000046 > >> data[1,] > V1 V2 V3 V4 > 1 2008-05-19 2008-04-19 2008-03-21 2008-02-22 > >> dates1<-as.Date(data[1,]) > > do not know how to convert 'dates[1,]' to class "Date" > > # However, I can individually convert them all: > >> dates1.1<-as.Date(data[1,1]) > >> dates1.1 > [1] "2008-05-19" > > # How can I change the class of the date over the entire row (my original > file contains more than one hundred rows)? > # Thank you very much. >-- View this message in context: http://old.nabble.com/as.Date-from-data.frame-tp26305830p26307735.html Sent from the R help mailing list archive at Nabble.com.