Questions tagged [as.date]

as.Date is an R function used to convert between character or numeric representations and objects of class "Date", representing calendar dates.

as.Date is an R function used to convert between character or numeric representations and objects of class "Date", representing calendar dates.

https://www.rdocumentation.org/packages/base/versions/3.3.2/topics/as.Date

349 questions
3
votes
2 answers

sapply as.Date function to change the time variable from integer to date does not work

My question is I have three columns of integers representing dates. If I use as.Date(x,origin="1970-01-01") for each individual column, it works. However, if I use sapply as sapply(data,function(x)as.Date(x,origin="1970-01-01")) it does not…
MYjx
  • 4,157
  • 9
  • 38
  • 53
3
votes
4 answers

R aggregate data.frame with date column

I have the data frame resambling the one in the below Date Expenditure Indicator 29-01-2011 5455 212 25-01-2012 5452 111 11-02-2011 365 5 I'm currently interested in summing up the Expenditure values, I'm trying to use…
Konrad
  • 17,740
  • 16
  • 106
  • 167
2
votes
2 answers

Arithmetic with months(9) resulting in NA

I have come across some unusual behavior calculating dates in R. I am trying to estimate date of conception by back calculating 9 months from date of birth, where date of birth is in the format as_date("1946-03-31". So, the calculation would look…
Calen
  • 305
  • 4
  • 17
2
votes
1 answer

Return NA if date is lower than for specific rows in R

My data looks like this: dput(head(VI_v2_KRR05,28)) structure(list(ID = c("AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "LAM", "LAM", "LAM", "LAM", "LAM", "LAM", "LAM", "LAM", "LAM", "LAM", "P0", "P0", "P0", "P0",…
Cláudio Siva
  • 502
  • 2
  • 10
2
votes
3 answers

Remove leading zeros from single digit months in year-month values

For year month vector: year_months <- c('2021-12', '2021-11', '2021-02', '2021-01'), I try to use the following code to convert year_months to c('2021Y12m', '2021Y11m', '2021Y2m', '2021Y1m') : format(as.Date(lubridate::ym(year_months)),…
ah bon
  • 9,293
  • 12
  • 65
  • 148
2
votes
3 answers

Converting YYYYMMDDHHMMSS to a date and time class in r

I have dataset containing date and time in the following format: datetime<- c(20210830102055, 20210830102312, 20210830102335) cycle <- c(25, 30, 35) df <- data.frame(datetime, cycle) df datetime cycle 1 2.021083e+13 25 2 2.021083e+13 …
2
votes
3 answers

How to transform "X hours/days ago" into standard date time

I have scraped news data, including time in form of: time <- c("11 hours ago", "2 days ago", "3 days ago") How can I translate this into a standard date time format? BTW: I assume that for intra-day differences (e.g. "11 hours ago") the browser…
Marco
  • 2,368
  • 6
  • 22
  • 48
2
votes
2 answers

Format x-axis date in chr type on facet_wrap using R

*** Updated code so it is reproducible. *** This question relates to R using RStudio. I need to format the date on the x-axis as three-letter month and two-digit day, as shown below (Mar 01). The date is a dataframe itself as date.local, it's of…
Fher
  • 35
  • 4
2
votes
0 answers

Preserve %d-%b-%Y as Date rather than character in R tibble

Suppose I look at the date March 5th: > temp <- as.Date("05-Mar-2021", "%d-%b-%Y") > temp [1] "2021-03-05" > tibble(temp) # A tibble: 1 x 1 temp 1 2021-03-05 Note that the tibble correctly says it's a date. Now I want a…
user1357015
  • 11,168
  • 22
  • 66
  • 111
2
votes
1 answer

Getting an NA with abbreviated month format for as.Date in Spanish locale

My session is in Spanish. When I have a format with full month name (%B) I get the correct date: as.Date("01-Febrero-2021", format = "%d-%B-%Y") # [1] "2021-02-01" However, when I try to use the abbreviated month (%b), I get an…
esterodr
  • 117
  • 1
  • 7
2
votes
1 answer

R to create a tally of previous events within a sliding window time period

Any one able to assist me with a problem using R to create a sum of previous events in a specific time period please? I apologise if I do not follow protocol this is my first question here. I have a df of IDs and event dates. In the genuine df the…
LBPM
  • 25
  • 5
2
votes
0 answers

App on Shinyapp io server doesn't display latest dates in table but on local computer it works

I have recently built an app and loaded it to shinyapps io. There I can see my app with all its tabs, tables and functions. In my app I have a table in each tab. With each table the user can select a range for the dates of what is to be shown in the…
Nick
  • 369
  • 1
  • 3
  • 18
2
votes
3 answers

Converting factor into dates for boxplot in R unsuccessful with "st" "nd" "rd" "th" added to dates

The dataset I have contains the following list of dates that are currently being recognised as factors: Interview_Date = c("Monday 23rd May 2005", "Tuesday 24th May 2005", "Wednesday 25th May 2005", "Thursday 26th May 2005", …
2
votes
1 answer

Why does the as.Date function in R converts the years I enter into the current year 2020?

I have some dates in a dataframe, and when I use as.Date() to convert them into dates, the years convert into 2020, which isn't really valid because the file only has data up to 2018. What I have so far: > fechadeinsc1[2] [1] "2020-08-15" >…
2
votes
1 answer

`as.Date()` identifies years in short form(say 01/10/68) as `2068-10-01` instead of `1968-10-01`, how to fix it?

I have a csv dataset that has date of births stored as characters in the format - 01-06-68("%d-%m-%y"). I tried to convert the dates to Date objects using as.Date() but it identifies the year as 2068 when in fact the year is 1968. I understand Date…
Alan
  • 21
  • 3
1 2
3
23 24