我正在处理一些时间序列数据,并试图添加一个列,该列根据另一列中的日期显示"峰值"或"非峰值"。我试图使用dplyr
将其处理到管道中。
下面是我的代码:
fish_comp_small<- read.csv('Fish composition dai data 2020-21 (small fish).csv') %>%
mutate(Date_time_S = as.POSIXct(paste(as.Date(as.character(Date_S),"%Y-%m-%d"), Time_S, sep=" "),format = "%Y-%m-%d %H:%M", tz="Asia/Bangkok")) %>%
mutate(Date_time_E = as.POSIXct(paste(as.Date(as.character(Date_E),"%Y-%m-%d"), Time_E, sep=" "),format = "%Y-%m-%d %H:%M", tz="Asia/Bangkok"))
我只需要创建一个新的列,其中"peak"将放在日期"2020 - 11 - 27"和"2020 - 11 - 28"之间以及日期"2020 - 12 - 24"和"2020 - 12 - 25"之间的行中。我想在其他地方都说"off-peak"。我尝试过使用if_else,但没有成功。
以下是reprex数据:
df<- structure(list(Trip = c("T2", "T2", "T2", "T2", "T2", "T2", "T2",
"T2", "T2", "T2"), dai_name = c("3C", "3C", "3C", "3C", "3C",
"3C", "3C", "3C", "3C", "3C"), sampleID = c("S01", "S01", "S01",
"S01", "S01", "S01", "S01", "S01", "S01", "S01"), Date_S = c("2020-11-27",
"2020-11-27", "2020-11-28", "2020-11-28", "2020-12-15", "2020-12-15",
"2020-12-24", "2020-12-25", "2021-01-07", "2021-01-23"), Time_S = c("8:22:00",
"8:22:00", "8:22:00", "8:22:00", "8:22:00", "8:22:00", "8:22:00",
"8:22:00", "8:22:00", "8:22:00"), Date_E = c("2020-11-27", "2020-11-27",
"2020-11-28", "2020-11-28", "2020-12-15", "2020-12-15", "2020-12-24",
"2020-12-25", "2021-01-07", "2021-01-23"), Time_E = c("10:35:00",
"10:35:00", "10:35:00", "10:35:00", "10:35:00", "10:35:00", "10:35:00",
"10:35:00", "10:35:00", "10:35:00"), Total.Catch.per.haul.kg. = c(5.9,
5.9, 5.9, 5.9, 5.9, 5.9, 5.9, 5.9, 5.9, 5.9), Subsample.kg. = c(5.02,
5.02, 5.02, 5.02, 5.02, 5.02, 5.02, 5.02, 5.02, 5.02), ScientificName = c("Pangasius.larnaudii",
"Crossocheilus.atritimess", "Syncrossus.helodes", "Yasuhikotakia.eos",
"Yasuhikotakia.modesta", "Amblyrhynchichthys.micracanthus", "Crossocheilus.reticulatus",
"Pangasius.conchophilus", "Phalacronotus.sp1", "Coilia.lindmani"
), Abundance = c(2L, 1L, 1L, 1L, 1L, 4L, 4L, 1L, 1L, 6L), Weight.g. = c(224.9,
2.7, 16, 9.5, 4.4, 97.9, 7.4, 3.1, 28.8, 44.5), note = c("Sampling",
"Sampling", "Sampling", "Sampling", "Sampling", "Sampling", "Sampling",
"Sampling", "Sampling", "Sampling"), Date_time_S = structure(c(1606440120,
1606440120, 1606440120, 1606440120, 1606440120, 1606440120, 1606440120,
1606440120, 1606440120, 1606440120), class = c("POSIXct", "POSIXt"
), tzone = "Asia/Bangkok"), Date_time_E = structure(c(1606448100,
1606448100, 1606448100, 1606448100, 1606448100, 1606448100, 1606448100,
1606448100, 1606448100, 1606448100), class = c("POSIXct", "POSIXt"
), tzone = "Asia/Bangkok")), row.names = c(NA, 10L), class = "data.frame")
1条答案
按热度按时间cgyqldqp1#
尝试以下
lubridate
解决方案,首先创建“峰值”间隔,然后使用ifelse()
语句检查Date_S
是否存在任何间隔:输出(仅相关列)