简体   繁体   English

根据 r 中现有列的值创建具有条件 label 的新列

[英]Create new column with a conditional label based on value of an existing column in r

I have a 2 column df, both characters, but one column is actually a time (h:m) column.我有一个 2 列 df,两个字符,但一列实际上是一个时间 (h:m) 列。 I would like to create a new column called "DAY.NIGHT" based on whether the existing time column has a value between 8am-6pm or between 6pm-8am.我想根据现有时间列的值是上午 8 点到下午 6 点还是下午 6 点到 8 点,创建一个名为“DAY.NIGHT”的新列。

I have attempted to use the hms package to convert the existing time column into a hms type and I feel like maybe it has something to do with the other packages I have attached because sometimes my code as it is works, but then randomly it will stop working and I can't get it to work again.我尝试使用 hms package 将现有时间列转换为 hms 类型,我觉得它可能与我附加的其他包有关,因为有时我的代码可以正常工作,但随后它会随机停止工作,我无法让它再次工作。

Does anyone have an alternate way perhaps to achieve what I am trying to without using the hms package?有没有人可以在不使用 hms package 的情况下实现我想要的替代方法?

EDIT 1: True to form I just ran it again (without changing anything) and it has randomly worked.编辑1:真实的形式我只是再次运行它(没有改变任何东西)并且它随机工作。 Same thing happened yesterday.昨天也发生了同样的事情。 So again if anyone has an alternate way to achieve this without the hms package it would be great.因此,如果有人在没有 hms package 的情况下有另一种方法来实现这一点,那就太好了。

EDIT 2 Below is the error I tend to get.编辑 2下面是我倾向于得到的错误。 The odd thing is it sometimes will work but most of the time I get this error:奇怪的是它有时会起作用,但大多数时候我都会收到这个错误:

<error/dplyr:::mutate_error>
Error in `mutate()`:
! Problem while computing `DAY.NIGHT = ifelse(...)`.
ℹ The error occurred in row 1.
Caused by error:
! All arguments must be numeric or NA
---
Backtrace:
  1. time_agg %>% rowwise() %>% ...
  8. hms::hms(TIME)
  9. hms:::check_args(args)
 10. base::stop("All arguments must be numeric or NA", call. = FALSE)

Here is an extract of my script where the issue is:这是问题所在的脚本的摘录:

library(tidyverse) 
library(ggplot2)
library(lubridate)
library(scales) 
library(viridis) 
library(hrbrthemes) 
library(e1071) 
library(rstatix)
library(GGally)
library(hms)

df_time <- data.frame(
  REGION = rep(c("NSW", "VIC", "QLD", "SA", "TAS"), each=50),
  TIME = rep(c("00:00", "08:00", "12:00", "21:00", "22:00"), each=10))
)

df_time$TIME <- as_hms(df_time$TIME)

day.night <- df_time %>%
  rowwise() %>%
  mutate('DAY.NIGHT'= ifelse(
    hms(TIME) > hms("8:00:00") & 
      hms(TIME) < hms("18:00:00"), "DAY", "NIGHT"))

I get your error with your first line, df_time$TIME <- as_hms(df_time$TIME) .我在第一行df_time$TIME <- as_hms(df_time$TIME)中得到了您的错误。 I think it's because as_hms expects to have hours, minutes, and seconds, but your input doesn't have seconds.我认为这是因为as_hms期望有小时、分钟和秒,但您的输入没有秒。 Let's paste on the seconds:让我们粘贴几秒钟:

## add :00 seconds for no error
df_time$TIME <- as_hms(paste0(df_time$TIME, ":00"))

Once the TIME column is already hms class, you don't want to use hms() on it again.一旦TIME列已经是hms class,您就不想再对其使用hms() And we want to be using as_hms() not hms .我们希望使用as_hms()而不是hms ( hms() looks like it would be appropriate if you were supplying the H, M, and S as separate arguments.) (如果您将 H、M 和 S 作为单独的 arguments 提供,hms hms()看起来是合适的。)

day.night <- df_time %>%
  mutate('DAY.NIGHT'= ifelse(
    TIME > as_hms("8:00:00") & 
      TIME < as_hms("18:00:00"), "DAY", "NIGHT"))
## no warnings, no errors, should run consistently if your inputs are consistent
day.night
#     REGION     TIME DAY.NIGHT
# 1      NSW 00:00:00     NIGHT
# 2      NSW 00:00:00     NIGHT
# 3      NSW 00:00:00     NIGHT
# 4      NSW 00:00:00     NIGHT
# 5      NSW 00:00:00     NIGHT
# 6      NSW 00:00:00     NIGHT
# ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM