[英]How do I remove an automatically set year when using As.date in R?
I have am struggling using dates in R.我一直在努力使用 R 中的日期。 I got to set it using as.Date, but then I automatically get the current year even if I did not specify it.
我必须使用 as.Date 设置它,但即使我没有指定它,我也会自动获取当前年份。 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然后我做 cbind,给一些名字,然后把它变成一个 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:但是,当使用 as.Date 并指定我使用月份和日期时,我会从无处得到一个“2021”:
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?有任何想法吗?
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).如果您想打印一个可爱的生日列表,只需在格式化日期时省略年份(参见
format
)。
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).首先,在创建日期和名称时的问题中,您创建一个简单的向量(使用
c
) ,然后将其包装成一个矩阵(在这种情况下为列矩阵)。 I assume because you want to use cbind
.我假设是因为您想使用
cbind
。 But cbind
also accepts vectors to join as columns.但
cbind
也接受向量作为列连接。
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, eg 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.生成的
matrix
一次只能包含一种数据类型 - 如果任何一列是字符向量,则所有内容都将简化为字符。 No more doing math on values saved as a character.不再对保存为字符的值进行数学运算。
Second step is converting to a data.frame.第二步是转换为data.frame。 But you could just create the data.frame directly:
但是您可以直接创建 data.frame :
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.如果您的目标是按排序,则不必将它们转换为日期,因为您已经有了以月份开头的格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.