dplyr 和 POSIXlt 数据的问题 [英] Problems with dplyr and POSIXlt data
问题描述
我有问题.我下载了数据并将日期转换为 POSIXlt 格式
I have a problem. I downloaded data and tranformed dates into POSIXlt format
df<-read.csv("007.csv", header=T, sep=";")
df$transaction_date<-strptime(df$transaction_date, "%d.%m.%Y")
df$install_date<-strptime(df$install_date, "%d.%m.%Y")
df$days<- as.numeric(difftime(df$transaction_date,df$install_date, units = "days"))
数据框是关于一个在线游戏中的交易.它包含值(它的支付)、transaction_date、intall_date 和 ID.我添加了新列,在安装后显示.我尝试使用 dlyr 汇总数据
Data frame is about transaction in one online game. It contains value (its payment), transaction_date, intall_date and ID. I added new column, which showndays after installation. I tried to summarise data using dlyr
df2<-df %>%group_by(天)%>%summarise(sum=sum(value))
我有一个错误:错误:transaction_date"列的类型不受支持:POSIXlt、POSIXt
And I've got an error: Error: column 'transaction_date' has unsupported type : POSIXlt, POSIXt
我该如何解决?
更新.我将日期列的类更改为字符.它解决了问题.但是我可以在不更改数据集中的类的情况下使用 dlyr 吗?
UPD. I changed classes of Date columns into Character. It solved problem. But can i use dlyr withouts changing classes in my dataset?
推荐答案
您可以按照评论中的建议使用 as.POSIXct
但如果小时、分钟和秒无关紧要,那么您应该只使用 as.Date
You could use as.POSIXct
as recommended in the comments but if the hours, minutes, and seconds don't matter then you should just use as.Date
df <- read.csv("007.csv", header=T, sep=";")
df2 <- df %>%
mutate(
transaction_date = as.Date(transaction_date, "%d.%m.%Y")
,install_date = as.Date(install_date, "%d.%m.%Y")
) %>%
group_by(days = transaction_date - install_date) %>%
summarise(sum=sum(value))
这篇关于dplyr 和 POSIXlt 数据的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!