过滤数值列名称上的非NA值

huangapple go评论88阅读模式
英文:

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 &lt;- data.table(
names = c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;),
`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 &lt;- dt %&gt;% filter(!is.na(`2020-01-01`)) %&gt;% 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 &lt;- as.Date(&quot;2020-01-01&quot;)

But of course the code:

dt1 &lt;- dt %&gt;% filter(!is.na(meeting_date)) %&gt;% select(names)

Does not work. The reason for this is the missing backticks, so without success I tried the following codes:

dt1 &lt;- dt %&gt;% filter(!is.na(paste(&quot;`&quot;, meeting_date, &quot;`&quot;, sep=&quot;&quot;))) %&gt;% select(names)
dt1 &lt;- dt %&gt;% filter(!is.na(noquote(paste(&quot;`&quot;, meeting_date, &quot;`&quot;, sep=&quot;&quot;)))) %&gt;% 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 &lt;- as.Date(&quot;2020-01-01&quot;)

dt %&gt;%
 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.naas below

meeting_date &lt;- &quot;2020-01-01&quot;
dtout &lt;- subset(dt,as.vector(!is.na(dt[, ..meeting_date])))

such that

&gt; 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 &lt;- data.table(
  names = c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;),
  `2020-01-01` = c(1, NA, 2),
  `2020-01-02` = c(3, 4, 5),
  `2020-01-03` = c(6, 7, 8)
)

#Make data &#39;long&#39; &amp; change the new &#39;name&#39; column to dates
# change confusing column &#39;name&#39; to date while we&#39;re at it.
dt_long &lt;- dt %&gt;% pivot_longer(-names) %&gt;% 
  mutate(date = lubridate::ymd(name)) %&gt;%
  select(-name)

meeting_date &lt;- as.Date(&quot;2020-01-01&quot;)

dt_long %&gt;% filter(date == meeting_date &amp; (!is.na(value)))

huangapple
  • 本文由 发表于 2020年1月3日 18:47:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577184.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定