I am working with daily temperature data that I have already run through R to pull out the first and last days of each year that are above a calculated threshold unique to each city dataset.
Data is brought into R in a .csv file with columns "YR", "number_of_days", "start_date", and "end_date". I only care about the "start_date" and "end_date" columns for this calculation.
For example, if I am looking at heat extremes, the first day of the year to have a temperature above 33 degrees C is May 1st and the last day of the year to have a temperature above 33 degrees C is October 20th. I do not care what the temperatures of the days in between are, just the start and end dates.
I want to convert the "May 1st" to an absolute number to be compared to other years. Below is sample data from BakersfieldTMAXextremes data.frame:
YR number_of_days start_date end_date
1900 27 5/22/00 10/18/00
1901 42 6/29/01 10/22/01
1902 76 6/7/02 9/23/02
1903 97 5/6/03 10/18/03
1904 98 4/8/04 9/15/04
1905 115 5/11/05 10/10/05
1906 90 4/20/06 10/27/06
1907 97 5/27/07 10/10/07
1908 107 4/11/08 9/16/08
1909 106 5/2/09 9/23/09
1910 89 4/18/10 10/15/10
1911 54 5/5/11 9/4/11
1912 51 5/31/12 10/18/12
1913 100 4/25/13 10/18/13
1914 78 4/19/14 10/14/14
1915 84 5/27/15 10/8/15
1916 73 5/5/16 9/28/16
1917 99 6/2/17 10/8/17
1918 81 6/2/18 10/13/18
1919 85 5/28/19 9/26/19
1920 61 5/17/20 9/30/20
1921 85 6/5/21 11/3/21
1922 91 5/14/22 9/25/22
1923 67 5/9/23 9/17/23
1924 91 5/8/24 9/29/24
1925 70 5/3/25 9/24/25
1926 84 4/25/26 9/9/26
1927 77 4/25/27 10/20/27
1928 88 5/5/28 10/9/28
1929 91 5/22/29 10/23/29
1930 86 5/23/30 10/7/30
1931 91 4/20/31 9/26/31
1932 82 5/11/32 10/5/32
1933 93 5/27/33 10/7/33
1934 101 4/20/34 10/12/34
1935 93 5/21/35 10/11/35
1936 85 5/10/36 9/26/36
For example, I would like to see the first start date as 141 (because it is the 141st day out of the 365 days in a year). At this point I couldn't care less about leap years, so we'll pretend they don't exist. I want the output in a table with the "YR", "start_date", and "end_date" (except with absolute values). For the first one, I would want "1900", "141" and "291" as the output.
I've tried to do this with an if-else statement, but it seems cumbersome to do for 365 days of the year (also I am fairly new to R and only have experience doing this in MATLAB). Any help is greatly appreciated!
Based on this answer , you can modify your data frame as follows:
library(lubridate)
df$start_date <- yday(df$start_date)
df$end_date <- yday(df$end_date)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.