Reputation: 125
I’m learning R and am trying to use the package
seas to apply to my own data. However, I am having some issues with my input data.
The package
provides an example data.frame
with the following structure:
stopifnot(packageVersion("seas") >= '0.4.3') #ensure most recent loaded
str(mscdata)
data.frame': 26358 obs. of 10 variables:
$ id : Factor w/ 3 levels "1096450","1108447",..: 1 1 1 1 1 1 1 1 1 1 ...
$ year : int 1975 1975 1975 1975 1975 1975 1975 1975 1975 1975 ...
$ yday : int 1 2 3 4 5 6 7 8 9 10 ...
$ date : Date, format: "1975-01-01" "1975-01-02" ...
$ t_max : atomic 1.1 0.6 0.6 -4.4 -0.6 -1.1 -7.2 -7.8 -25.6 -26.7 ...
..- attr(*, "units")= chr "°C"
..- attr(*, "long.name")= chr "daily maximum temperature"
$ t_min : atomic -7.2 -7.2 -7.2 -10 -6.1 -10.6 -13.3 -27.2 -32.8 -38.3 ...
..- attr(*, "units")= chr "°C"
..- attr(*, "long.name")= chr "daily minimum temperature"
.....
The structure of my data.frame
appears like this:
str(dat0)
data.frame': 27029 obs. of 6 variables:
$ id : Factor w/ 1 level "2228551": 1 1 1 1 1 1 1 1 1 1 ....
$ year : int 1940 1940 1940 1940 1940 1940 1940 1940 1940 1940 ...
$ date : Date, format: "1940-01-01" "1940-01-02" ...
$ t_max : num -21.1 -17.2 -15 -16.1 -13.9 -16.1 -15.6 -14.4 -17.2 -25 ...
$ t_min : num -32.8 -24.4 -18.9 -17.8 -17.8 -24.4 -19.4 -21.1 -24.4 -29.4
.....
Note when I load the first data.frame
from the package and compare it with my data.frame
, they have the same column headings (colnames
), as follows:
colnames(mscdata)
[1] "id" "year" "yday" "date" "t_max" "t_min" "t_mean" "rain"
[9] "snow" "precip"
My question is: how can I edit the data structure of my data.frame
to match the format of package:seas
(e.g. t_min
has two attr
, while mine has only one)
Upvotes: 1
Views: 129
Reputation: 5274
Hopefully you will be able to generalize from this:
library(seas)
data(mscdata)
str(mscdata)
### similar to example given above
dat0 <- data.frame(
year=rep(1940, 10),
t_max=c(21.1, -17.2, -15, -16.1, -13.9, -16.1, -15.6, -14.4, -17.2, -25),
t_min=c(-32.8, -24.4, -18.9, -17.8, -17.8, -24.4, -19.4, -21.1, -24.4, -29.4)
)
attr(dat0$t_max, "long.name") <- "daily maximum temperature"
### or copy directly from example
attr(dat0$t_max, "units") <- attr(mscdata$t_max, "units")
Or simply:
attributes(dat0$t_max) <- attributes(mscdata$t_max)
See ?attr
or ?attributes
for more.
Upvotes: 1