Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将时间转换为';“英国皇家海军”;R中的字符格式_R_Date_Time - Fatal编程技术网

将时间转换为';“英国皇家海军”;R中的字符格式

将时间转换为';“英国皇家海军”;R中的字符格式,r,date,time,R,Date,Time,我有一列日期时间,格式为'2000-11-21 10:01:01',2000-11-21 00:02:01',2000-11-21 00:00:06。我想创建一个新列,将时间设置为HMS格式,例如,在上面的3个日期中,它将返回“HMS”、“MS”、“S”。我会尝试以下方法,但我想知道是否有更简单的方法: ifelse( grepl("00:00:", datecolumn), "S", ifelse(grepl("00:", datecolumn), "MS", "HMS"

我有一列日期时间,格式为'2000-11-21 10:01:01',2000-11-21 00:02:01',2000-11-21 00:00:06。我想创建一个新列,将时间设置为HMS格式,例如,在上面的3个日期中,它将返回“HMS”、“MS”、“S”。我会尝试以下方法,但我想知道是否有更简单的方法:

ifelse(
  grepl("00:00:", datecolumn), "S", 
        ifelse(grepl("00:", datecolumn), "MS", "HMS")
)
输出:

 datecolumn                 HMS
2000-11-21 10:01:01         HMS
2000-11-21 00:02:01          MS
2000-11-21 00:00:06           S
2000-11-21 00:00:10           S
2000-11-21 00:10:06          MS
2000-11-21 00:00:07           S
2000-11-21 10:00:06         HMS

您可以将lubridate软件包与粘贴一起使用,如下所示:

require(lubridate)
df$new_col <- paste(ifelse(hour(df$date) > 0, "H", ""), 
                    ifelse(minute(df$date) > 0, "M", ""), 
                    ifelse(second(df$date) > 0, "S", ""), sep = "")
require(润滑油)
df$new_col 0,“H”和“),
ifelse(分钟(df$日期)>0,“M”和“),
ifelse(第二个(df$date)>0,“S”,sep=“”)
来自
dplyr
case\u when()
函数可以为嵌套的
ifelse
块提供可读的替代方法
stringi
并不是真正需要的(
grepl
wld工作得很好),但我喜欢
stringi
函数名的表达性(而且
stringr
是不必要的支柱):

请注意,这里的顺序很重要:

mutate(xdf, computed_hms = case_when(
  stri_detect_regex(datecolumn, "00:00:[[:digit:]]{2}") ~ "S",
  stri_detect_regex(datecolumn, "00:[[:digit:]]{2}:[[:digit:]]{2}") ~ "MS",
  stri_detect_regex(datecolumn, "[[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}") ~ "HMS"
  TRUE ~ NA_character_
))
##            datecolumn HMS computed_hms
## 1 2000-11-21 10:01:01 HMS          HMS
## 2 2000-11-21 00:02:01  MS           MS
## 3 2000-11-21 00:00:06   S            S
## 4 2000-11-21 00:00:10   S            S
## 5 2000-11-21 00:10:06  MS           MS
## 6 2000-11-21 00:00:07   S            S
## 7 2000-11-21 10:00:06 HMS          HMS

将时间部分转换为
数据。表::ITime
(“一天中存储为整数秒数的时间类”),并使用适当的
断点和
标签
剪切它:

d$HMS <- cut(data.table::as.ITime(d$datecolumn),
             breaks = c(0, 60 - 1, 60 * 60 - 1, Inf),
             labels = c("s", "ms", "hms"))
d
#                     datecolumn HMS
# 1          2000-11-21 10:01:01 hms
# 2          2000-11-21 00:02:01  ms
# 3          2000-11-21 00:00:06   s
# 4          2000-11-21 00:00:10   s
# 5          2000-11-21 00:10:06  ms
# 6          2000-11-21 00:00:07   s
# 7          2000-11-21 10:00:06 hms

d$HMS您能分享一个输出示例吗?您所说的HMS、MS、附加样本输出是什么意思!HMS-小时分秒,MS-分钟秒,S-秒。啊,比我快5秒!对于
2000-11-21 10:00:06
d$HMS <- cut(data.table::as.ITime(d$datecolumn),
             breaks = c(0, 60 - 1, 60 * 60 - 1, Inf),
             labels = c("s", "ms", "hms"))
d
#                     datecolumn HMS
# 1          2000-11-21 10:01:01 hms
# 2          2000-11-21 00:02:01  ms
# 3          2000-11-21 00:00:06   s
# 4          2000-11-21 00:00:10   s
# 5          2000-11-21 00:10:06  ms
# 6          2000-11-21 00:00:07   s
# 7          2000-11-21 10:00:06 hms