R convert date to day of the year [duplicate] - r

This question already has answers here:
How do you convert POSIX date to day of year?
(5 answers)
Closed 5 years ago.
I have a data frame as follows
S = c("28/05/2016 07:00", "29/05/2016 07:00", "30/05/2016 07:00")
S1 = c("2016-05-28", "2016-05-29", "2016-05-30")
df = data.frame(S, S1)
I want to convert the dates to day of the year. Using
df$Day_S <- yday(df$S)
df$Day_S1 <- yday(df$S1)
gives
S S1 Day_S Day_S1
1 28/05/2016 07:00 2016-05-28 141 149
2 29/05/2016 07:00 2016-05-29 140 150
3 30/05/2016 07:00 2016-05-30 140 151
which works only for the format of the 'S1' dates.
Ive tried
df$S_1 <- format(as.POSIXct(df$S,format='%d/%m/%Y'),format='%d/%m/%Y')
df$Day_S_1 <- yday(df$S_1)
but this still gives the wrong day of the year.
How can i convert the 'S' column to day of the year?

This works for me
S = c("28/05/2016 07:00", "29/05/2016 07:00", "30/05/2016 07:00")
s_1 <- as.Date(S,format='%d/%m/%Y')
Day_S <- lubridate::yday(s_1)

Related

Extract AM/PM from Time in R [duplicate]

This question already has answers here:
Extract part of string before the first semicolon
(4 answers)
Create categories by comparing a numeric column with a fixed value
(3 answers)
Closed 2 years ago.
Hi I have a sample data frame like this
Time <- c('0:00', '1:00', '2:00', '13:00', '14:00')
Time = data.frame(x)
So what I would like to do is create another column "AMPM" based on the "Time" column. "AMPM" should able to show if the time is in AM or PM
The final output should look like this
Time AMPM
1 0:00 AM
2 1:01 AM
3 2:09 AM
4 13:52 PM
5 14:06 PM
6 15:33 PM
7 16:27 PM
8 21:40 PM
You can remove everything after colon, convert data to integer and assign 'PM' to all the values greater than 11 and "AM" otherwise.
df <- data.frame(Time = c('0:00', '1:00', '2:00', '13:00', '14:00'))
df$AMPM <- ifelse(as.integer(sub(':.*', '', df$Time)) > 11, 'PM', 'AM')
#Without ifelse
#c('AM', 'PM')[(as.integer(sub(':.*', '', x)) > 11) + 1]
df
# Time AMPM
#1 0:00 AM
#2 1:00 AM
#3 2:00 AM
#4 13:00 PM
#5 14:00 PM

grouping by 10 minute intervals of time series data in R and taking the sum [duplicate]

This question already has answers here:
How to sum 5 minute intervals in R
(4 answers)
Closed 3 years ago.
I would like to group by and take the sum of the observations by time intervals of 10 minutes in R.
I have looked into cut, and to.period from the xts package and requires OHLC data (Open High Low Close stock data) - I have a different time series data set.
Python code:
data_timeidx = data.set_index('timestamp')
data_time_grp = data_timeidx.groupby(pd.Grouper(freq='10Min'))
Data:
start <- as.POSIXct("2015-12-05 06:00:00", "%Y-%m-%d %H:%M")
end <- as.POSIXct("2015-12-05 08:00:00", "%Y-%m-%d %H:%M")
time <- seq(start, end, by = 0.05)
d <- data.frame(time)
obs <- nrow(d)
d$data <- rnorm(obs)
This is a straight forward problem using the cut function and the dplyr package:
library(dplyr)
d %>% group_by(group10min=cut(time, "10 min")) %>% summarize(sum=sum(data))
# A tibble: 13 x 2
group10min sum
<fct> <dbl>
1 2015-12-05 06:00:00 63.3
2 2015-12-05 06:10:00 9.68
3 2015-12-05 06:20:00 -139.
4 2015-12-05 06:30:00 -18.4
5 2015-12-05 06:40:00 -104.
6 2015-12-05 06:50:00 28.7
7 2015-12-05 07:00:00 -146.

adding two column of a data where col1 contains date and col2 contains days

I have a data frame in which i have two columns date and days and i want to add date column with days and show the result in other column
data frame-1
col date is in format of mm/dd/yyyy format
date days
3/2/2019 8
3/5/2019 4
3/6/2019 4
3/21/2019 3
3/25/2019 7
and i want my output like this
date days new-date
3/2/2019 8 3/10/2019
3/5/2019 4 3/9/2019
3/6/2019 4 3/10/2019
3/21/2019 3 3/24/2019
3/25/2019 7 4/1/2019
i was trying this
as.Date("3/10/2019") +8
but i think it will work for a single value
Convert to actual Date values and then add Days. You need to specify the actual format of date (read ?strptime) while converting it to Date.
as.Date(df$date, "%m/%d/%Y") + df$days
#[1] "2019-03-10" "2019-03-09" "2019-03-10" "2019-03-24" "2019-04-01"
If you want the output back in same format, we can use format
df$new_date <- format(as.Date(df$date, "%m/%d/%Y") + df$days, "%m/%d/%Y")
df
# date days new_date
#1 3/2/2019 8 03/10/2019
#2 3/5/2019 4 03/09/2019
#3 3/6/2019 4 03/10/2019
#4 3/21/2019 3 03/24/2019
#5 3/25/2019 7 04/01/2019
If you get confused with different date format we can use lubridate to do
library(lubridate)
with(df, mdy(date) + days)

change 201401(numeric) in to date variable in R [duplicate]

This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 4 years ago.
enter image description here
var1 is a numeric variable
i try as.Date many times
but it did not work
I want to change the 201401(year-month) to date variable.
Dates also need a day, and you do not have that. So you need to assume which day of the month your are looking at:
dat <- data.frame(var1 = c(201401, 201402, 201403), Freq=sample(1:3))
assumed_day <- 15
dat$date <- as.Date(paste(dat$var1, assumed_day), format = "%Y%m %d")
print(dat)
# var1 Freq date
#1 201401 1 2014-01-15
#2 201402 2 2014-02-15
#3 201403 3 2014-03-15
See as.Date.
Using the format and other related function you can format the dates however you like:
dat$formatted <- paste(months(dat$date), format(dat$date, "%Y"))
print(dat)
# var1 Freq date formatted
#1 201401 1 2014-01-15 January 2014
#2 201402 3 2014-02-15 February 2014
#3 201403 2 2014-03-15 March 2014

using if else statements to manipulate dates [duplicate]

This question already has answers here:
How to add leading zeros?
(8 answers)
Closed 6 years ago.
I am trying to do an if else statement to say if the value is less than 10 add a zero in front, if not leave it as is. I am trying to get all of my dates to be 2 digits. Please assist.
if(df$col < 10){
paste '0'
else df$col
}
I was trying to break it down into different columns
EventID SampleDate SampleTime
130466 3/19/2008 12:30:00
131392 4/30/2008 08:45:00
131658 5/14/2008 10:00:00
117770 6/11/2008 08:45:00
118680 7/23/2008 09:15:00
118903 8/6/2008 09:00:00
SampleDatech year month day2
3/19/2008 2008 3 19
4/30/2008 2008 4 30
5/14/2008 2008 5 14
6/11/2008 2008 6 11
7/23/2008 2008 7 23
8/6/2008 2008 8 6
If you are trying to output just the day with a leading zero to a new column, you can use a combination of strftime and as.Date.
df$day = strftime(as.Date(df$SampleDate, "%m/%d/%Y"), "%d")
Or if you want to keep the whole date, but add the leading zero to the day you can do this.
df$NewDate = strftime(as.Date(df$SampleDate, "%m/%d/%y"), "%m/%d/%Y")

Resources