How to split two numerical values into two columns? [duplicate] - r

This question already has answers here:
Split data frame string column into multiple columns
(16 answers)
Closed 7 years ago.
I have tried my best using the split function and others, but to no avail.

We can use read.table/read.csv with the sep option.
read.table(text=as.character(df1$datetime), sep=' ',
col.names=c('date', 'time'),
header=FALSE, stringsAsFactors=FALSE)
# date time
#1 01/01/2011 0:00
#2 01/01/2011 1:00
#3 01/01/2011 2:00
#4 01/01/2011 3:00
#5 01/01/2011 4:00
Or with tidyr
library(tidyr)
separate(df1, datetime, into= c('date', 'time'), sep=' ')

Related

Manually change hour of 'dttm' object in R for entire dataframe column

Is there a simple way to change the hour for all time entries in a dttm column in R to the same value (e.g., 11:00 A.M.)?
For example, given the dttm 2020-01-01 00:00:00, can I change this automatically to 2020-01-01 11:00:00?
Thank you.
An option is to assign only the hour part to a specific value
library(lubridate)
hour(df1$datetime) <- 11
Here are two base R options.
Change the date-time to POSIXlt format and change the hour component from it.
df$a <- as.POSIXlt(df$a)
df$a$hour <- 11
df
# a
#1 2021-08-21 11:17:08
#2 2021-08-21 11:16:59
#3 2021-08-21 11:17:06
#4 2021-08-21 11:17:04
#5 2021-08-21 11:17:01
#6 2021-08-21 11:17:07
#7 2021-08-21 11:17:05
#8 2021-08-21 11:17:02
#9 2021-08-21 11:17:00
#10 2021-08-21 11:17:03
Use regex to change the hour to 11 and change the time to POSIXct again.
df$a <- as.POSIXct(sub('\\s(\\d+)', ' 11', df$a), tz = 'UTC')
data
df <- data.frame(a = Sys.time() + sample(10))

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

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

R convert date to day of the year [duplicate]

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)

Resources