使用[[和向量对数据框进行索引时,日期列将被强制转换为数字 [英] Date column coerced to numeric when indexing dataframe with [[ and a vector
问题描述
我正在创建一个类型为 Date
的列的data.frame.当使用 [[[
]和数字矢量为数据帧建立索引时,Date变为数字.使用 purrr :: pmap
时,这会引起问题.谁能解释为什么会这样,并且可以解决吗?
I am creating a data.frame with a column of type Date
. When indexing the data frame with [[
and a numeric vector, the Date becomes a number. This is causing a problem when using purrr::pmap
. Can anyone explain why this is happening and is there a work around?
示例:
x <- data.frame(d1 = lubridate::ymd(c("2018-01-01","2018-02-01")))
class(x$d1)
# [1] "Date"
x[[1]]
# [1] "2018-01-01" "2018-02-01"
x[[c(1, 1)]]
# [1] 17532
推荐答案
概述
阅读为什么unlist()杀死R中的日期以及 unlist()
的文档,您必须手动阻止 purrr :: map()
使用 base :: c()
函数.
Overview
After reading why does unlist() kill dates in R and the documentation of unlist()
, you've got to manually prevent purrr::map()
from coercing the Date objects to integer by way of using the base::c()
function.
阅读 pmap条 Date
后,它看起来就像非常敬畏的人提交了拉取请求,以在重构版本的索引发生在 purrr :: pmap()
一个>.
After reading pmap strips Date
, it looks like someone very awesome submitted a pull request to resolve this issue within a refactored version of the indexing that happens under the hood in purrr::pmap()
.
使用 devtools :: dev_mode()
,您可以安装 mikmart/purrr
'的 purrr
的"pmap"分支版本,以在使用 pmap()
时保留Date对象.
Using devtools::dev_mode()
, you can install mikmart/purrr
's "pmap" branch version of purrr
to retain Date objects while using pmap()
.
# ******pmap() example ****
# load necessary packages -----
library(devtools)
library(lubridate)
# enter dev mode so you don't have to uninstall the cran version of purrr ----
dev_mode(on = TRUE)
# install mikmart's PR to fix the coercing of Dates to integer ----
install_github(repo = "mikmart/purrr", ref = "pmap")
# load mikmart's PR version of purrr ----
library(purrr)
# load necessary data
x <- data.frame(d1 = lubridate::ymd(c("2018-01-01","2018-02-01")))
# for the first column in x ----
# give me each element
# note: no need for c()
list.of.dates <-
x %>%
pmap(.f = ~ .x)
# view results -----
list.of.dates
# [[1]]
# [1] "2018-01-01"
#
# [[2]]
# [1] "2018-02-01"
# view the class of each list -----
map_chr(list.of.dates, class) # [1] "Date" "Date"
#
#
# turn off dev mode ---
dev_mode(on = FALSE)
#
# restart R -----
# Manually hit Shift+Cmd+F10 or point in click under the "Session" tab
#
# clear global environment ----
rm(list = ls())
#
# ******map() example********
# load necessary packages -----
library(tidyverse)
library(lubridate)
# load necessary data ----
x <- data.frame(d1 = lubridate::ymd(c("2018-01-01","2018-02-01")))
# from the first column ------
# give me each element
# and ensure the dates don't get coerced to integers
list.of.dates <-
x$d1 %>%
map(.f = ~ .x %>% c())
# view results -----
list.of.dates
# [[1]]
# [1] "2018-01-01"
#
# [[2]]
# [1] "2018-02-01"
# # view the class of each list -----
map_chr(list.of.dates, class) # [1] "Date" "Date"
# end of script #
这篇关于使用[[和向量对数据框进行索引时,日期列将被强制转换为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!