【发布时间】:2021-02-12 11:38:21
【问题描述】:
我正在尝试根据日期类列对数据集进行采样, “活跃”每季度一次,“非活跃”每月一次
这是我的代码:
library(dplyr)
library(lubridate)
## data ##
df <- structure(list(
mes = c("01/01/2000", "01/02/2000", "01/03/2000",
"01/04/2000", "01/05/2000", "01/06/2000", "01/07/2000", "01/08/2000",
"01/09/2000", "01/10/2000", "01/11/2000", "01/12/2000"),
status = c("Active", "Inactive",
"Active", "Inactive",
"Active", "Inactive",
"Active", "Active",
"Inactive", "Active",
"Inactive", "Active")),
class = "data.frame",
row.names = c(NA, -12L))
## setting date class for "mes" column ##
df$mes <- as.Date(df$mes,
format = "%d/%m/%Y")
## sampling ##
sample_df <- df %>%
dplyr :: filter(status %in% "Active",
status %in% "Inactive") %>%
dplyr :: filter_if(status == "Active",
month(mes) %in% c(3,6,9,12),
month(mes) %in% c(1,2,3,4,5,6,7,8,9,10,11,12))
控制台输出:
Error in is_logical(.p) : objeto 'status' no encontrado
我可以使用任何其他库来完成这项任务吗?
【问题讨论】:
-
查看@akrun 提供的解决方案。出于我自己的好奇心,您能否更新您的问题并提供预期的输出?
-
该错误是由于
filter_if的语法不正确造成的。也不是现在推荐使用across代替filter_if,并且它们都用于选择列名而不是变量值的上下文中。 -
完全不清楚你想要什么作为输出。
标签: r dataframe if-statement filter dplyr