[英]R strptime not piping into the data frame
library(dplyr)
names <- c('a','b','c')
dates1 <- c('2020-08-14','2020-08-15','2020-08-16')
dates2 <- c('2019-08-14','2019-08-15','2019-08-16')
df <- data.frame(names, dates1, dates2)
print(colnames(df))
timestamps <- df %>% select(dates1, dates2) %>%
strptime('%Y-%m-%d')
print(timestamps)
Why is timestamps
a pair of NA
s?为什么timestamps
是一对NA
? How do I get it to correctly apply strptime
to these datetime strings?如何让它正确地将strptime
应用于这些日期时间字符串?
You are applying sptrptime
on dataframe instead you should apply it on columns您在sptrptime
上应用 sprptime 而应该在列上应用它
library(dplyr)
df %>% mutate(across(starts_with('date'), strptime, '%Y-%m-%d'))
# names dates1 dates2
#1 a 2020-08-14 2019-08-14
#2 b 2020-08-15 2019-08-15
#3 c 2020-08-16 2019-08-16
Since you have only date information in the columns you can use as.Date
:由于列中只有日期信息,因此可以使用as.Date
:
df %>% mutate(across(starts_with('date'), as.Date))
It is a two column data.frame, and we could use as.POSIXct
if the intention is to convert to DateTime
class by looping over the columns with across
它是一个两列 data.frame,如果打算通过遍历列转换为DateTime
across
,我们可以使用as.POSIXct
library(dplyr) # >= 1.00
out <- df %>%
select(dates1, dates2) %>%
mutate(across(everything(), as.POSIXct))
out
# dates1 dates2
#1 2020-08-14 2019-08-14
#2 2020-08-15 2019-08-15
#3 2020-08-16 2019-08-16
If we have an earlier version of dplyr
, use mutate_at
or mutate_all
如果我们有dplyr
的早期版本,请使用mutate_at
或mutate_all
df %>%
select(dates1, dates2) %>%
mutate_all(as.POSIXct)
It can be also used with strptime
, but beware of the structure and the class as it is POSIXlt
它也可以与strptime
一起使用,但要注意结构和 class 因为它是POSIXlt
out2 <- df %>%
select(dates1, dates2) %>%
mutate(across(everything(), strptime, format = '%Y-%m-%d'))
unclass(out2$dates1)
#$sec
#[1] 0 0 0
#$min
#[1] 0 0 0
#$hour
#[1] 0 0 0
#$mday
#[1] 14 15 16
#...
Regarding the OP's original question about piping, it does on a single column or vector关于 OP 关于管道的原始问题,它在单个列或向量上
df %>%
pull(dates1) %>%
strptime(format = '%Y-%m-%d')
#[1] "2020-08-14 CDT" "2020-08-15 CDT" "2020-08-16 CDT"
because the documentation for ?strptime
says the input should be因为?strptime
的文档说输入应该是
x - An object to be converted: a character vector for strptime, an object which can be converted to "POSIXlt" for strftime. x - 要转换的 object:strptime 的字符向量,可以转换为 strftime 的“POSIXlt”的 object。
If we don't want to select
, use the select modifiers如果我们不想select
,请使用 select 修饰符
df %>%
mutate(across(-1, as.POSIXct))
# names dates1 dates2
#1 a 2020-08-14 2019-08-14
#2 b 2020-08-15 2019-08-15
#3 c 2020-08-16 2019-08-16
Or if the intention is to convert to Date
class, just use as.Date
或者如果打算转换为Date
class,只需使用as.Date
df %>%
select(dates1, dates2) %>%
mutate(across(everything(), as.Date))
NOTE: strptime returns a list
and is not recommended注意:strptime 返回一个list
,不推荐
Also, another option is base R
此外,另一种选择是base R
df[-1] <- lapply(df[-1], strptime, format = '%Y-%m-%d')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.