为什么 unlist() 在 R 中杀死日期? [英] Why does unlist() kill dates in R?

查看:46
本文介绍了为什么 unlist() 在 R 中杀死日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我取消列出日期列表时,它会将它们变回数字.这是正常的吗?除了重新应用 as.Date 之外的任何解决方法?

When I unlist a list of dates it turns them back into numerics. Is that normal? Any workaround other than re-applying as.Date?

> dd <- as.Date(c("2013-01-01", "2013-02-01", "2013-03-01"))
> class(dd)
[1] "Date"
> unlist(dd)
[1] "2013-01-01" "2013-02-01" "2013-03-01"
> list(dd)
[[1]]
[1] "2013-01-01" "2013-02-01" "2013-03-01"

> unlist(list(dd))
[1] 15706 15737 15765

这是一个错误吗?

推荐答案

do.call 是一个使用列表做某事"的便捷函数.在我们的例子中,使用 c 连接它.将列表中的 cbindrbind data.frames 转换为单个大 data.frame 的情况并不少见.

do.call is a handy function to "do something" with a list. In our case, concatenate it using c. It's not uncommon to cbind or rbind data.frames from a list into a single big data.frame.

我们在这里所做的实际上是连接 dd 列表的元素.这类似于 c(dd[[1]], dd[[2]]).请注意,c 可以作为函数或字符提供.

What we're doing here is actually concatenating elements of the dd list. This would be analogous to c(dd[[1]], dd[[2]]). Note that c can be supplied as a function or as a character.

> dd <- list(dd, dd)
> (d <- do.call("c", dd))
[1] "2013-01-01" "2013-02-01" "2013-03-01" "2013-01-01" "2013-02-01" "2013-03-01"
> class(d) # proof that class is still Date
[1] "Date"

这篇关于为什么 unlist() 在 R 中杀死日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆