英文:
Filter non-NAs on numeric column name
问题
我正在尝试筛选包含数值/日期名称的列,使用as.Date
变量。
例如,考虑这个小数据库:
dt <- data.table(
names = c("A", "B", "C"),
`2020-01-01` = c(1, NA, 2),
`2020-01-02` = c(3, 4, 5),
`2020-01-03` = c(6, 7, 8)
)
我当前正在按以下方式筛选所需的日期列:
dt1 <- dt %>% filter(!is.na(`2020-01-01`)) %>% select(names)
我的想法是创建一个meeting_date
变量,这个变量应该作为我的所有R代码的日期参考。
meeting_date <- as.Date("2020-01-01")
但是当然,以下代码不起作用:
dt1 <- dt %>% filter(!is.na(meeting_date)) %>% select(names)
这是因为缺少反引号,所以我尝试了以下代码但未成功:
dt1 <- dt %>% filter(!is.na(paste("`", meeting_date, "`", sep=""))) %>% select(names)
dt1 <- dt %>% filter(!is.na(noquote(paste("`", meeting_date, "`", sep="")))) %>% select(names)
有人知道如何继续吗?谢谢!
英文:
I am trying to filter a column which contains a numeric/date name using a as.Date
variable.
As an example, consider this small database:
dt <- data.table(
names = c("A", "B", "C"),
`2020-01-01` = c(1, NA, 2),
`2020-01-02` = c(3, 4, 5),
`2020-01-03` = c(6, 7, 8)
)
I am currently filtering the desired date column as follows:
dt1 <- dt %>% filter(!is.na(`2020-01-01`)) %>% select(names)
My idea is to create a meeting_date
variable, this variable should be used as a date reference for all my R code.
meeting_date <- as.Date("2020-01-01")
But of course the code:
dt1 <- dt %>% filter(!is.na(meeting_date)) %>% select(names)
Does not work. The reason for this is the missing backticks, so without success I tried the following codes:
dt1 <- dt %>% filter(!is.na(paste("`", meeting_date, "`", sep=""))) %>% select(names)
dt1 <- dt %>% filter(!is.na(noquote(paste("`", meeting_date, "`", sep="")))) %>% select(names)
Does anyone knows how to proceed? Thanks!
答案1
得分: 2
可以这样做:
meeting_date <- as.Date("2020-01-01")
dt %>%
filter_at(vars(one_of(as.character(meeting_date))), ~ !is.na(.))
names 2020-01-01 2020-01-02 2020-01-03
1 A 1 3 6
2 C 2 5 8
英文:
You can do:
meeting_date <- as.Date("2020-01-01")
dt %>%
filter_at(vars(one_of(as.character(meeting_date))), ~ !is.na(.))
names 2020-01-01 2020-01-02 2020-01-03
1 A 1 3 6
2 C 2 5 8
答案2
得分: 0
你可以使用以下的代码来使用 subset
+ is.na
:
meeting_date <- "2020-01-01"
dtout <- subset(dt, as.vector(!is.na(dt[, ..meeting_date])))
这将得到如下的结果:
> dtout
names 2020-01-01 2020-01-02 2020-01-03
1: A 1 3 6
2: C 2 5 8
英文:
You can use subset
+ is.na
as below
meeting_date <- "2020-01-01"
dtout <- subset(dt,as.vector(!is.na(dt[, ..meeting_date])))
such that
> dtout
names 2020-01-01 2020-01-02 2020-01-03
1: A 1 3 6
2: C 2 5 8
答案3
得分: 0
长数据应该更容易处理:
library(data.table)
dt <- data.table(
names = c("A", "B", "C"),
`2020-01-01` = c(1, NA, 2),
`2020-01-02` = c(3, 4, 5),
`2020-01-03` = c(6, 7, 8)
)
#将数据转换为“长”格式并将新的“name”列更改为日期
#在此过程中更改令人困惑的列“name”为日期。
dt_long <- dt %>% pivot_longer(-names) %>%
mutate(date = lubridate::ymd(name)) %>%
select(-name)
meeting_date <- as.Date("2020-01-01")
dt_long %>% filter(date == meeting_date & (!is.na(value)))
英文:
Long data should be easier to work with:
library(data.table)
dt <- data.table(
names = c("A", "B", "C"),
`2020-01-01` = c(1, NA, 2),
`2020-01-02` = c(3, 4, 5),
`2020-01-03` = c(6, 7, 8)
)
#Make data 'long' & change the new 'name' column to dates
# change confusing column 'name' to date while we're at it.
dt_long <- dt %>% pivot_longer(-names) %>%
mutate(date = lubridate::ymd(name)) %>%
select(-name)
meeting_date <- as.Date("2020-01-01")
dt_long %>% filter(date == meeting_date & (!is.na(value)))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论