Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
100 views
in Technique[技术] by (71.8m points)

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

I have am struggling using dates in R. I got to set it using as.Date, but then I automatically get the current year even if I did not specify it. How can I remove the year from Date?

xy <- matrix(c("09/08", "10/14", "10/06", 
           "05/11", "02/23", "10/27",
           "08/04", "11/29", "07/23",
           "12/17"), byrow=TRUE)

names <- matrix(c("G", "C", "R", "OB", "S", "B", "Ms", "Mi", "Ma", "A"), byrow=T)

I then do the cbind, give some names and turn it into a df

names_birthday <- cbind(names, xy)
colnames(names_birthday) <- c("Name", "Date")
names_birthday <- as.data.frame(names_birthday)

But then when using as.Date and specifying that I'm using months and days, I get a '2021' out from nowhere:

 names_birthday <- names_birthday %>%
 group_by(Name) %>%
 mutate(Date=as.Date(Date, format = "%m/%d")) %>%
 arrange(Date)

 names_birthday

  # A tibble: 10 x 2
  # Groups:   Name [10]
     Name  Date      
     <chr> <date>    
   1 S     2021-02-23
   2 OB    2021-05-11
   3 Ma    2021-07-23
   4 Ms    2021-08-04
   5 G     2021-09-08
   6 R     2021-10-06
   7 C     2021-10-14
   8 B     2021-10-27
   9 Mi    2021-11-29
  10 A     2021-12-17

Any ideas?

question from:https://stackoverflow.com/questions/66063279/how-do-i-remove-an-automatically-set-year-when-using-as-date-in-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...