我无法指定正确的日期格式来对R中的数据帧进行子集 [英] I am unable to specify the correct date format to subset a dataframe in R
问题描述
此问题与我之前的一个相关,子集数据帧指定的月份和年份
This question is related to my previous one, Subsetting a dataframe for a specified month and year
我使用命令
sales< - read .csv(mysales.csv,colClasses =character)
sales <- read.csv("mysales.csv", colClasses="character")
以获取如下所示的数据框:
to obtain a dataframe which looks like this:
row date pieces income
1 21/11/2011 49 220.5
2 22/11/2011 58 261
3 23/11/2011 23 103.5
4 24/11/2011 57 256.5
我想创建一个子集2011年11月使用我上一个问题中提供的代码,但各种尝试失败。所以对于我在控制台中写的支票:
I want to create a subset for November 2011 using the code provided in my previous question, but various attempts have failed. So for a check I wrote in the console:
format.Date(sales[1,1], "%Y")=="2011"
,答案是:
[1] FALSE
此外: p>
Moreover:
format(as.Date(sales[1,1]), "%d/%m/%Y")
[1] "20/11/21"
我至少要知道什么是发生日期格式?
How can I, at least, know what is happening with date format?
如何使用以下代码对数据框进行子集?
What should I do to subset the dataframe using code like:
subset(sales, format.Date(date, "%m")=="11" & format.Date(date, "%Y")=="2011")
对不起,如果我的问题不清楚,但我面临的问题对我来说还不清楚。
Sorry if my question is not clear, but the problem I am facing is not clear to me either.
(编辑以正确格式化)
推荐答案
目前,只是字符串。您需要使用 as.Date
将它们转换为 Date
对象,并指定它们所在的格式(%d /%m /%Y
)或者R不会为你猜测。
Currently, what you think are dates really are just strings of characters. You need to turn them into Date
objects using as.Date
, and for that specify the format they are in (%d/%m/%Y
) or R won't guess it for you.
sales <- data.frame(date = c("21/11/2011", "21/11/2011", "23/11/2012", "24/11/2012"),
pieces = c(49,58,23,57,34),
income = c(220.5, 261, 103.5, 256.5, 112))
class(sales$date)
# [1] "factor"
sales$date <- as.Date(sales$date, "%d/%m/%Y")
class(sales$date)
# [1] "Date"
subset(sales, format.Date(date, "%m")=="11" & format.Date(date, "%Y")=="2011")
# date pieces income
# 1 2011-11-21 49 220.5
# 2 2011-11-21 58 261.0
这篇关于我无法指定正确的日期格式来对R中的数据帧进行子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!