lubridate

Create a variable in `df1` depending on one variable of `df1` (`df1$var1`) and one variable of `df2` that is changeable depending on `df1$var1`

落花浮王杯 提交于 2019-12-22 18:32:59
问题 I have data frame df1 that summarises fish depths over time. df1$Site tells you the site where the fish was, df1$Ind tells you the individual and df1$Depth tells you the depth where the fish was at a specific df1$Datetime . On the other hand, I have df2 that summarises the intensity of the currents over time (EVERY THREE HOURS) from the surface to 39 meters depth at intervals of 8 meters ( m0-7 , m8-15 , m16-23 , m24-31 and m32-39 ). As an example: df1<-data.frame(Datetime=c("2016-08-01 15:34

Calculating cumulative time in R

倖福魔咒の 提交于 2019-12-22 15:58:27
问题 I have a dataframe that looks like this: POI LOCAL.DATETIME 1 1 2017-07-11 15:02:13 2 1 2017-07-11 15:20:28 3 2 2017-07-11 15:20:31 4 2 2017-07-11 15:21:13 5 3 2017-07-11 15:21:18 6 3 2017-07-11 15:21:21 7 2 2017-07-11 15:21:25 8 2 2017-07-11 15:21:59 9 1 2017-07-11 15:22:02 10 1 2017-07-11 15:22:05 I want to be able to calculate (probably with lubridate) the cumulative time spent at each POI and combine them into a table that looks something like this: POI TOTAL.TIME 1 1 00:18:18 2 2 00:01

Use ggplot2 to plot time series data

孤街浪徒 提交于 2019-12-22 07:59:42
问题 I am doing a project which requires to use ggplot2 to plot time series data. This is the data set I am using: This is what I've done now: library(ggplot2) library(lubridate) eur$Date <- as.Date(eur$Date) ggplot(eur, aes(Date, EUR)) + geom_line() And I got this very weird plot. Can someone help me fix the problem? 回答1: Edit - if your data is of class timeseries(ts) then you can use ggfortify's autoplot function and it will take care of the conversion for you. library(ggfortify) library

Why are my functions on lubridate dates so slow?

风流意气都作罢 提交于 2019-12-22 04:29:12
问题 I wrote this function which I use all the time: # Give the previous day, or Friday if the previous day is Saturday or Sunday. previous_business_date_if_weekend = function(my_date) { if (length(my_date) == 1) { if (weekdays(my_date) == "Sunday") { my_date = lubridate::as_date(my_date) - 2 } if (weekdays(my_date) == "Saturday") { my_date = lubridate::as_date(my_date) - 1 } return(lubridate::as_date(my_date)) } else if (length(my_date) > 1) { my_date = lubridate::as_date(sapply(my_date, previous

R lubridate converting seconds to date

我只是一个虾纸丫 提交于 2019-12-21 03:36:31
问题 I have a simple question regarding R's lubridate package. I've a series of timestamps in seconds since epoch. I want to convert this to YYYY-MM-DD-HH format. In base R, I can do something like this to first convert it to a date format > x = as.POSIXct(1356129107,origin = "1970-01-01",tz = "GMT") > x [1] "2012-12-21 22:31:47 GMT" Note the above just converts it to a date format, not the YYYY-MM-DD-HH format. How would I do this in lubridate? How would I do it using base R? Thanks much in

Converting numeric to date issues

人走茶凉 提交于 2019-12-20 07:47:27
问题 I have a date column that is numeric as follows Date1 4010 5178 5494 6750 7106 39 39 172 1704 4152 I am trying to convert this to real date using this function as.Date(df$Date1, origin = "1970-01-01") However I am seeing that these numbers are converted to these dates which are incorrect. Date1_Converted 1980-12-24 1984-03-26 1985-01-16 1988-06-25 1989-06-16 1970-02-09 1970-02-09 1970-06-22 1974-09-01 1981-05-15 The correct transformation should have been Date1_CrctTrnsf 2005.10.31 2009.02.11

POSIX formatted Date and Time

依然范特西╮ 提交于 2019-12-20 07:37:57
问题 I am reading this from csv file into R df <- ID DATE TIME 1 10/14/2000 8:30:05 2 02/13/2001 12:05:05 I have trouble converting this to POSIX formatted date and time. df <- ID DATE TIME DATETIMEPOSIX 1 10/14/2000 8:30:05 2000-10-14 8:30:05 2 02/13/2001 01:05:05 2001-02-13 13:05:05 I have tried this but got NAs df$DateTime <- paste(df$DATE, df$TIME) df$DateTimePOSIX <- strptime(df$DateTime, format = "%Y-%m-%d %H:%M:%S") 回答1: The format argument needs to be the format of what it's reading, not

Replace NA´s in dates with another date

六月ゝ 毕业季﹏ 提交于 2019-12-20 05:21:52
问题 Data: DB1 <- data.frame(orderItemID = 1:10, orderDate = c("2013-01-21","2013-03-31","2013-04-12","2013-06-01","2014-01-01", "2014-02-19","2014-02-27","2014-10-02","2014-10-31","2014-11-21"), deliveryDate = c("2013-01-23", "2013-03-01", "NA", "2013-06-04", "2014-01-03", "NA", "2014-02-28", "2014-10-04", "2014-11-01", "2014-11-23")) Expected Outcome: DB1 <- data.frame(orderItemID = 1:10, orderDate= c("2013-01-21","2013-03-31","2013-04-12","2013-06-01","2014-01-01", "2014-02-19","2014-02-27",

Convert multiple columns to dates with lubridate and dplyr

≯℡__Kan透↙ 提交于 2019-12-20 02:28:13
问题 I'm looking for a straightforward way to convert all of the variables in a data frame which begin with 'date' to dates using lubridate::dmy() (they are currently characters with the dmy format). I had thought there would be done with mutate_if or mutate_each in dplyr but I am struggling to figure out how. 回答1: You can use mutate_at() library(dplyr) library(lubridate) df <- mutate_at(df, vars(starts_with("date")), funs(dmy)) or use mutate_if to mutate all date columns to dmy. Using is.Date

Calculate character string “days, hours, minutes, seconds” to numeric total days [duplicate]

你说的曾经没有我的故事 提交于 2019-12-20 02:06:28
问题 This question already has answers here : R extract time components from semi-standard strings (2 answers) Closed 3 years ago . I have seen a lot of questions relating to formatting times, but none in the particular imported format that I have: Time <- c( "22 hours 3 minutes 22 seconds", "170 hours 15 minutes 20 seconds", "39 seconds", "2 days 6 hours 44 minutes 17 seconds", "9 hours 54 minutes 36 seconds", "357 hours 23 minutes 28 seconds", "464 hours 30 minutes 7 seconds", "51 seconds", "31