[R] Assigning "dates" attribute

Achim Zeileis Achim.Zeileis at wu-wien.ac.at
Thu Apr 7 17:56:11 CEST 2005


On Thu, 7 Apr 2005 08:26:41 -0700 (PDT) JTW wrote:

> Dear List,
> 
> I have a one-column data set in .csv format.
> 
> I used read.csv to import the data into R, as follows:
> 
> x <- read.csv("data.csv", header = TRUE, sep = ",")
> 
> The data points have a 'dates' attribute, which is in
> a separatel .csv file.  I used the same command as
> above to import it into R.
> 
> To assoicate the 'dates' attribute with the data
> points, I did:
> 
> > attributes(x)<-date
> 
> Which resulted in:
> 
> Error in "attributes<-"(`*tmp*`, value = date) :
> attributes must be in a list
> 
> So then I did:
> 
> > attributes(x)<-list(date)
> 
> Again, got an error, though slightly different this
> time:
> 
> Error in "attributes<-"(`*tmp*`, value = list(date)) :
> attributes must be named
> 
> Any help is appreciated.

The error message is pretty informative, the assignment needs a named
list, e.g.:

R> x <- 1:10
R> attributes(x) <- list(foo = letters[1:10])
R> x
 [1]  1  2  3  4  5  6  7  8  9 10
attr(,"foo")
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"

Note, that this will strip off all other attributes. To add one
attribute, you can do

R> attr(x, "bar") <- LETTERS[1:10]
R> x
 [1]  1  2  3  4  5  6  7  8  9 10
attr(,"foo")
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
attr(,"bar")
 [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"

Furthermore, the data you describe look like a time series. So you might
want to store the data as a time series. For time series with a date
attribute of class "Date", look at the zoo package.
Z





> ______________________________________________
> 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
>




More information about the R-help mailing list