我有问题。我下载了数据并将日期转换为POSIXlt格式
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
汇总数据 df2<-df %>%
group_by(days) %>%
summarise(sum=sum(value))
我发现了一个错误: 错误:列&#39; transaction_date&#39;不支持的类型:POSIXlt,POSIXt
我该如何解决?
UPD。我将Date列的类更改为Character。它解决了问题。但我可以使用dlyr而不更改数据集中的类吗?
答案 0 :(得分:11)
您可以按照评论中的建议使用as.POSIXct
,但如果小时,分钟和秒不重要,那么您应该使用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))
答案 1 :(得分:5)
如上所述here,这是tidyverse的“特征”。他们不想处理java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.util.SparseArray.get(int)' on a null object reference
at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:902)
at android.support.v4.app.FragmentStatePagerAdapter.restoreState(FragmentStatePagerAdapter.java:216)
at android.support.v4.view.ViewPager.onRestoreInstanceState(ViewPager.java:1455)
at android.view.View.dispatchRestoreInstanceState(View.java:14956)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3253)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3259)
at android.view.View.restoreHierarchyState(View.java:14934)
at android.support.v4.app.Fragment.restoreViewState(Fragment.java:396)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1445)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1750)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1819)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2590)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2377)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2332)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2239)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:700)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5770)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
对象,因为它是向量中的某种列表。但是,使用POSIXlt
并不总是一种选择。在我的情况下,我真的需要as.POSIXct
类来处理一些未清理的数据。在这种情况下,只需回到旧的稳定基地R.在你的情况下:
POSIXlt
答案 2 :(得分:0)
我经常使用的一个技巧是:
POSIXt
列(在eventDate
下面的示例中)转换为字符POSIXt
,不要忘记像执行步骤1一样设置正确的格式(format
)和时区(tz
)。示例:
# step 1
df1$eventDate <- as.character.POSIXt(df1$eventDate)
df2$eventDate <- as.character.POSIXt(df2$eventDate)
#step 2
merged_df <- bind_rows(df1, df2)
#step 3
merged_df$eventDate <- strptime(merged_df$eventDate, format = "%Y-%m-%d", tz = "UTC")