[R] converting to time series object : ts - package:stats

Gabor Grothendieck ggrothendieck at gmail.com
Mon Jun 26 16:26:57 CEST 2006


df[] <- sapply(format(df), as.numeric)

will convert it to numeric but I think the real problem is the read.csv
statement.  Do commas represent separators or decimals since
you have specified comma for both?  Assuming it looks like:

A,B,C
1,2,3
4,5,6

just do:

DF <- read.csv("Data.csv")
str(DF)




On 6/26/06, Sachin J <sachinj.2006 at yahoo.com> wrote:
>
>
> It seems I have problem in reading the data as dataframe. It is reading it
> as factors. Here is the df
>
> df <-
> read.csv("C:/Data.csv",header=TRUE,sep=",",na.strings="NA",
> dec=",", strip.white=TRUE)
>
> > dput(df)
>
> > df <- structure(list(V1 = structure(c(2, 15, 15, 14, 14, 14, 12, 13,
> + 16, 2, 14, 5, 6, 8, 10, 17, 11, 9, 18, 11, 1, 4, 7, 3), .Label =
> c("10.83",
> + "11.08", "11.83", "12.83", "13.08", "13.83", "15.83", "16.83",
> + "17.83", "19.83", "20.83", "23.08", "32.08", "6.08", "7.08",
> + "8.08", "8.83", "9.83"), class = "factor"), V2 = structure(c(8,
> + 15, 2, 10, 9, 18, 1, 4, 10, 2, 8, 6, 17, 5, 16, 13, 5, 14, 3,
> + 11, 3, 12, 7, 7), .Label = c("10.73", "11.73", "11.75", "12.73",
> + "15.75", "19.73", "19.75", "21.73", "25.73", "26.73", "26.75",
> + "27.75", "32.75", "33.75", "37.73", "42.75", "61.75", "9.73"), class =
> "factor"),
> +     V3 = structure(c(3, 8, 7, 9, 11, 9, 3, 8, 10, 9, 11, 10,
> +     2, 1, 12, 12, 6, 5, 4, 6, 2, 5, 5, 1), .Label = c("10.33",
> +     "12.33", "13.08", "13.33", "14.33", "15.33", "21.08", "6.08",
> +     "7.08", "8.08", "9.08", "9.33"), class = "factor")), .Names = c("V1",
> + "V2", "V3"), class = "data.frame", row.names = c("1", "2", "3",
> + "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
> + "16", "17", "18", "19", "20", "21", "22", "23", "24"))
>
> TIA
>
> Sachin
>
>
>
>
> Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
>
> Sorry I meant issue dput(df) and
> post
>
> df <- ...the output your got from dput(df)...
> ...rest of your code...
>
> Now its reproducible.
>
>
> On 6/26/06, Gabor Grothendieck wrote:
> > We don't have data.csv so its still not ***reproducible*** by anyone
> > else. To be reproducible it means that anyone can copy the code
> > in your post, paste it into R and get the same answer.
> >
> > Suggest you post the output of
> > dput(df)
> >
> > and then post
> > dput <- ...the output you got from dput(df)...
> >
> > Now its reproducible.
> >
> > On 6/26/06, Sachin J wrote:
> > > Hi Achim,
> > >
> > > I did the following:
> > >
> > > >df <- read.csv("C:/data.csv", header=TRUE,sep=",",na.strings="NA",
> dec=",", strip.white=TRUE)
> > >
> > > Note: data.csv has 10 (V1...V10) columns.
> > >
> > > >df[1]
> > > V1
> > > 1 11.08
> > > 2 7.08
> > > 3 7.08
> > > 4 6.08
> > > 5 6.08
> > > 6 6.08
> > > 7 23.08
> > > 8 32.08
> > > 9 8.08
> > > 10 11.08
> > > 11 6.08
> > > 12 13.08
> > > 13 13.83
> > > 14 16.83
> > > 15 19.83
> > > 16 8.83
> > > 17 20.83
> > > 18 17.83
> > > 19 9.83
> > > 20 20.83
> > > 21 10.83
> > > 22 12.83
> > > 23 15.83
> > > 24 11.83
> > >
> > > >tsdata <- ts((df[1]),frequency = 12, start = c(2005, 1))
> > >
> > > The resulting time series is different from the df. I don't know why? I
> think I am doing something silly.
> > >
> > > TIA
> > >
> > > Sachin
> > >
> > >
> > > Achim Zeileis wrote:
> > > On Mon, 26 Jun 2006, Sachin J wrote:
> > >
> > > > Hi,
> > > >
> > > > I am trying to convert a dataset (dataframe) into time series object
> > > > using ts function in stats package. My dataset is as follows:
> > > >
> > > > >df
> > > > [1] 11.08 7.08 7.08 6.08 6.08 6.08 23.08 32.08 8.08 11.08 6.08 13.08
> 13.83 16.83 19.83 8.83 20.83 17.83
> > > > [19] 9.83 20.83 10.83 12.83 15.83 11.83
> > >
> > > Please provide a reproducible example. You just showed us the print
> output
> > > for an object, claiming that it is an object of class "data.frame" which
> > > is rather unlikely given the print output.
> > >
> > > > I converted this into time series object as follows
> > > >
> > > > >tsdata <- ts((df),frequency = 12, start = c(1999, 1))
> > >
> > > which produces the right result for me if `df' is a vector or a
> > > data.frame:
> > >
> > > df <- c(11.08, 7.08, 7.08, 6.08, 6.08, 6.08, 23.08, 32.08, 8.08, 11.08,
> > > 6.08, 13.08, 13.83, 16.83, 19.83, 8.83, 20.83, 17.83, 9.83, 20.83,
> > > 10.83, 12.83, 15.83, 11.83)
> > > ts(df, frequency = 12, start = c(1999, 1))
> > > ts(as.data.frame(df), frequency = 12, start = c(1999, 1))
> > >
> > > > The resulting time series is as follows:
> > > >
> > > > Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
> > > > 1999 2 15 15 14 14 14 12 13 16 2 14 5
> > > > 2000 6 8 10 17 11 9 18 11 1 4 7 3
> > > >
> > > > I am unable to understand why the values of df and tsdata does not
> match.
> > >
> > > So are we because you didn't really tell us enough about df...
> > >
> > > Best,
> > > Z
> > >
> > > > I looked at ts function and I couldn't find any data transformation.
> Am
> > > > I missing something here? Any pointers would be of great help.
> > > >
> > > > Thanks in advance.
> > > >
> > > > Sachin
> > > >
> > > >
> > > > ---------------------------------
> > > >
> > > > [[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
> > > >
> > >
> > >
> > > __________________________________________________
> > >
> > >
> > >
> > > [[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
> > >
> >
>
>
>
>
> ________________________________
> Do you Yahoo!?
> Next-gen email? Have it all with the all-new Yahoo! Mail Beta.
>
>



More information about the R-help mailing list