I have a data frame with a time column, the format is like dd/mm/yyyy and I want to
convert dd/mm/yyyy to a number mmyyyy and store it in a new column
then create another two new columns for mm and yyyy separately and create a new data frame with my original data frame and these 2 columns
How can I do it? Thanks!
If the time column is not character you can use:
format(timecolumn,"%m%Y")
format(timecolumn,"%m")
format(timecolumn,"%Y")
If it is character:
paste0(substr(timecolumn,4,5),substr(timecolumn,7,10))
substr(timecolumn,4,5)
substr(timecolumn,7,10)
Related
I have a data frame which has two columns, one is called year and is in a numeric integer form e.g. 1990 etc... I want to convert this into a date e.g. 01-01-1990 etc... I'm thinking I should be able to use parse_date or similar but can't work it out.
I'm very new to R, so this might seem straightforward. But I have a data frame with an original date column that has values that look like this: 4-02-91, 5-29-93 (i.e. m-d-y). I am trying to separate this column into 3, where months, days, and years are separate. Then I need to combine them again to this format 19910402, 19930529 - I need it this way in order to compare it to another dataset with similar dates.
Here is what I've been trying to do:
# Make DATE an actual date column
dataframe$DATE <- as.Date(used$DATE, format="%m-%d-%Y")
# This changes the original date column into something that looks like this: 1991-04-02, 1993-05-29
# Separate DATE into multiple columns
dataframe$year <- year(dataframe$DATE)
dataframe$month <- month(dataframe$DATE)
dataframe$day <- day(dataframe$DATE)
# Combine dates again to get string
dataframe$raster_date<-paste(dataframe$year, dataframe$month, dataframe$day, sep = "")
The last step looks great except where the months or days are single digits. It's coming out as 199142 and 1993529 instead of 19910402 and 19930529. How do I insert zeros when the month and day values are 1 digit?
Here, we can use sprintf instead of paste as the year, month, day from lubridate extracts those as numeric values and numeric class would drop the 0 padded as prefix. We add those prefix with 0s in sprintf
sprintf("%04d%02d%02d", dataframe$year, dataframe$month, dataframe$day)
I am trying to split the Date.Time column in my data table to separate date and time columns. currently the column is as character.
this is what I already tried but it just gave me a column with 2019 dates. I don't want the year to be 2019 so doesn't work. even if it does, not sure how to get the time to a separate column
office$date <- as.Date(office$Date.Time, format = '%m/%d')
office$date <- as.Date(office$Date.Time, format = '%m/%d')
Date require the year field. You can remove the year field, but the result will be a character, not the date format.
office$date <- as.Date(office$Date.Time, format = 'Y%/%m/%d')
office$date <- as.character(gsub("^.{5}","",office$date))
I am extracting Google Trends data looking at interest_over_time and interest_by_city.
I've noticed the interest_by_city data frame doesn't contain any date information. As I am looking to monitor the changes over time this is problematic.
Is there a way to add a new variable for the date where each observation will be the date and time the data was extracted?
If what you want is to add Sys.time() to the data you just extracted then you can call df$time <- Sys.time().
This question already has answers here:
Converting excel DateTime serial number to R DateTime
(5 answers)
Closed 8 years ago.
I have a very large data set from an instrument which measures depth of a water column, and attaches a date/time stamp to each measurement as a modified Julian Date. Below is a subset of the data. The column is in a data.frame (date) and contains numeric values.
head(date)
Date
41498.736111,
41498.736123,
41498.736134,
41498.736146,
41498.736157,
41498.736169,
When I transfer the first 2 values (i.e. 41498.736111 and 41498.736123) from the Date column to Excel and format the column to (MM:DD:YYYY hh:mm:ss.000), I get the correct value of 8/12/2013 17:39:59.990 and 8/12/2013 17:40:01.027. This tells me that the origin date for these Julian days is "1899-12-30". I would do this all in Excel, but my dataset is much too large and I will have to do this several times for the next few years, so I'd like to be able to do it all in R.
Is there any way to convert the entire column of these modified Julian Dates to the format "%M%D%Y %H:%M:OS3"?
Yeah, you can just add the number of seconds to the origin:
as.POSIXct('1899-12-30')+(date$Date*24*60*60)