Remove Year from Dates in R

Remove year from dates in R

Does this work?

temp<-as.Date(c("2014-06-01","1993-06-01", "2013-06-03", "1999-01-31"), "%Y-%m-%d")

x<-format(temp, format="%m-%d")

x
[1] "06-01" "06-01" "06-03" "01-31"

sort(x)
[1] "01-31" "06-01" "06-01" "06-03"

dropping the year from a date in R

R has its own Date representation, which you should use. Once you convert data to Date it is easy to manipulate their format using the format function.

http://www.statmethods.net/input/dates.html

as an example

> d <- as.Date( "2010-03-17" )
> d
[1] "2010-03-17"
> format( d, format="%m/%d")
[1] "03/17"

or with your data style

> format( as.Date("03/17/2010", "%m/%d/%Y"), format="%m/%d")
[1] "03/17"

How do I remove an automatically set year when using As.date in R?

What's wrong with the year? If you want to print a lovely list of birthdays, just omit the year when formatting the date (see format for this).

I'll add some comments, because you are taking a lot of extra steps to do simple things. Firstly, in your question when creating your dates and names, you create a simple vector (with c) and then wrap it into a matrix (a column-matrix in this case). I assume because you want to use cbind. But cbind also accepts vectors to join as columns.

Here's the short version to create the matrix:

xy <- c("09/08", "10/14", "10/06", "05/11", "02/23", "10/27",
"08/04", "11/29", "07/23", "12/17")
names <- c("G", "C", "R", "OB", "S", "B", "Ms", "Mi", "Ma", "A")
cbind(Name=names, Date=xy)

The issue with this approach is if you are combining several different data types, e.g. both characters and numbers. The resulting matrix can only contain 1 data type at a time - and if any of the columns are a character vector, everything is reduced to characters. No more doing math on values saved as a character.

Second step is converting to a data.frame. But you could just create the data.frame directly:

data.frame(Name=names, Date=xy)

If your goal is to sort by, you strictly don't have to convert them to dates, because you already have the format starting with month.

Extract year from date

if all your dates are the same width, you can put the dates in a vector and use substring

Date
a <- c("01/01/2009", "01/01/2010" , "01/01/2011")
substring(a,7,10) #This takes string and only keeps the characters beginning in position 7 to position 10

output

[1] "2009" "2010" "2011"

R: How to remove the day from a date?

Besides format by @Greg, another option is using sub like below

> sub(".*?/","","13/01/2020")
[1] "01/2020"

Remove all dates in a range over multiple years

You can create a date dynamically to remove :

library(dplyr)
library(lubridate)

start <- '04-15'
end <- '08-16'

dat %>%
mutate(Date = as.Date(Date),
year = year(Date)) %>%
filter(!(Date >= ymd(paste(year, start, sep = '-')) &
Date <= ymd(paste(year, end, sep = '-'))))

# Date Type Num year
#1 2000-11-02 B 6.0 2000
#2 2001-11-04 B 6.5 2001
#3 2002-02-06 B 5.5 2002
#4 2000-02-08 A 7.0 2000
#5 2001-11-10 B 3.0 2001

How to remove time-field string from a date-as-character variable?

You can turn them into dates and then format as desired, e.g.:

v <- c("9/21/2011 0:00:00",  "9/25/2011 0:00:00",  "10/2/2011 0:00:00",  
"9/28/2011 0:00:00", "9/27/2011 0:00:00")
v <- format(as.POSIXct(v,format='%m/%d/%Y %H:%M:%S'),format='%m/%d/%Y')
> v
[1] "09/21/2011" "09/25/2011" "10/02/2011" "09/28/2011" "09/27/2011"

Or, you can simply remove the " 0:00:00" substring using gsub:

v <- gsub(x=v,pattern=" 0:00:00",replacement="",fixed=T)
> v
[1] "9/21/2011" "9/25/2011" "10/2/2011" "9/28/2011" "9/27/2011"

Extract Month and Year From Date in R

This will add a new column to your data.frame with the specified format.

df$Month_Yr <- format(as.Date(df$Date), "%Y-%m")

df
#> ID Date Month_Yr
#> 1 1 2004-02-06 2004-02
#> 2 2 2006-03-14 2006-03
#> 3 3 2007-07-16 2007-07

# your data sample
df <- data.frame( ID=1:3,Date = c("2004-02-06" , "2006-03-14" , "2007-07-16") )

a simple example:

dates <- "2004-02-06"

format(as.Date(dates), "%Y-%m")
> "2004-02"

side note:
the data.table approach can be quite faster in case you're working with a big dataset.

library(data.table)
setDT(df)[, Month_Yr := format(as.Date(Date), "%Y-%m") ]


Related Topics



Leave a reply



Submit