I have a two-dimensional table. and i want to know How to import Rstudio by read.zoo? And generate a line graph.thanks everybody!
Asked
Active
Viewed 59 times
0

glig
- 33
- 6
-
Please edit this questions so that you have a minimal reproducible example. Do not use images. Also please explain how you have tried to do what you want and what didn't work. Make sure you have read ?read.zoo. – Elin Mar 29 '20 at 10:59
-
I have done it for you in the Note in my answer but next time please follow the instructions at the top of the [tag:r] tag regarding posting. – G. Grothendieck Mar 29 '20 at 12:20
-
i'm so sorry for my post format and i'm a beginner for R,thanks for your answer! – glig Mar 29 '20 at 12:53
1 Answers
1
zoo/xts assumes that each column is a time series whereas the input has them in rows. Thus, using the input file generated reproducibly in the Note at the end, read it in as an ordinary data frame, transpose it and then use read.zoo
. See ?read.zoo
and vignette("zoo-read", package = "zoo")
for more information.
library(zoo)
DF <- read.table("myfile.dat", as.is = TRUE, strip.white = TRUE,
check.names = FALSE)
m <- t(DF)
read.zoo(data.frame(rownames(m), m))
giving this zoo object:
A B
2010-01-01 1 3
2010-02-01 2 4
yearmon
Since the dates seem to be monthly an alternative would be to use yearmon class for the dates by adding the FUN=as.yearmon argument to read.zoo. yearmon class represents year/month directly and sorts in date order.
# alternative to read.zoo line
read.zoo(data.frame(rownames(m), m), FUN = as.yearmon)
giving this zoo object:
A B
Jan 2010 1 3
Feb 2010 2 4
Note
The input in reproducible form. (Please provide it like this next time.)
Lines <- "2010-01-01 2010-02-01
A 1 2
B 3 4"
cat(Lines, file = "myfile.dat")

G. Grothendieck
- 254,981
- 17
- 203
- 341