and thank you for the help.
Currently the rnoaa package in Program R gives you an output as a list
. You can access the data in this list easily; however, I would like to alter the way the data is coming out. It currently is not in the format that is usable to me. I have animal relocations, and I am trying to give each animal relocation a assigned value of temperature that was recorded on the day of the location.
Currently the head of my animal dataframe looks like this:
ID date time datetime lat lon DOP Elevation
1 333 2017-03-15 2:30:47 AM 2017-03-15 02:30:47 32.73810 -90.15422 3 48.64
2 333 2017-03-15 10:30:28 PM 2017-03-14 22:30:28 32.73797 -90.15424 2 39.45
3 333 2017-05-25 10:31:00 PM 2017-05-24 22:31:00 32.73790 -90.15489 7 91.02
4 333 2017-05-25 2:30:30 AM 2017-05-25 02:30:30 32.73758 -90.15488 4 28.65
5 333 2017-05-13 10:30:54 AM 2017-05-13 10:30:54 32.73753 -90.15261 4 32.97
6 333 2017-04-14 2:31:59 AM 2017-04-14 02:31:59 32.73747 -90.15155 5 45.97
Getting the weather data from the rnoaa package is easy.
library(rnoaa)
weather <- ncdc(datasetid = "GHCND", stationid = "GHCND:USC00221389", datatypeid = c("PRCP", "TMIN", "TMAX", "TOBS"),
startdate = "2017-03-01", enddate = "2017-12-31", add_units = T, limit = 1000)
However, the output of rnoaa is a tibble that looks like this.
$data
# A tibble: 1,000 x 9
date datatype station value fl_m fl_q fl_so fl_t units
<chr> <chr> <chr> <int> <chr> <chr> <chr> <chr> <chr>
1 2017-03-01T00:00:00 PRCP GHCND:USC00221389 0 "" "" 7 0700 mm_tenths
2 2017-03-02T00:00:00 PRCP GHCND:USC00221389 175 "" "" 7 0700 mm_tenths
3 2017-03-03T00:00:00 PRCP GHCND:USC00221389 0 "" "" 7 0700 mm_tenths
4 2017-03-04T00:00:00 PRCP GHCND:USC00221389 0 "" "" 7 0700 mm_tenths
5 2017-03-05T00:00:00 PRCP GHCND:USC00221389 0 "T" "" 7 0700 mm_tenths
6 2017-03-05T00:00:00 TOBS GHCND:USC00221389 94 "" "" 7 0700 celcius_tenths
7 2017-03-06T00:00:00 PRCP GHCND:USC00221389 18 "" "" 7 0700 mm_tenths
8 2017-03-06T00:00:00 TMAX GHCND:USC00221389 183 "" "" 7 0700 celcius_tenths
9 2017-03-06T00:00:00 TMIN GHCND:USC00221389 94 "" "" 7 0700 celcius_tenths
10 2017-03-06T00:00:00 TOBS GHCND:USC00221389 167 "" "" 7 0700 celcius_tenths
I would like to change the output of the tibble to where every row is a single date and the types of data in datatype are now a column. So I would have precipitation, max temperature, minimum temperature for each day in my animal relocation dataset. Something like this.
date PRCP TOBS TMAX TMIN
2017-03-06 5 78 85 72
Which would then allow me to easily add it to my animal relocation dataset to look like this.
ID date time datetime lat lon DOP Elevation PRCP TOBS TMAX TMIN
1 333 2017-03-15 2:30:47 AM 2017-03-15 02:30:47 32.73810 -90.15422 3 48.64 5 78 85 72
Would something like this be of any help?
library(tidyverse)
weather$data %>%
pivot_wider(id_cols = date,names_from = datatype,values_from = value) %>%
separate(date,into = c("date","time"),sep = "T") %>%
mutate(date = as.Date(date))
this would give you:
# A tibble: 277 x 6
date time PRCP TOBS TMAX TMIN
<date> <chr> <int> <int> <int> <int>
1 2017-03-01 00:00:00 0 NA NA NA
2 2017-03-02 00:00:00 175 NA NA NA
3 2017-03-03 00:00:00 0 NA NA NA
4 2017-03-04 00:00:00 0 NA NA NA
5 2017-03-05 00:00:00 0 94 NA NA
6 2017-03-06 00:00:00 18 167 183 94
7 2017-03-07 00:00:00 0 183 256 167
8 2017-03-08 00:00:00 353 22 222 17
9 2017-03-09 00:00:00 0 139 222 22
10 2017-03-10 00:00:00 86 150 244 139
# ... with 267 more rows