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

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

Filter non-NAs on numeric column name

问题

我正在尝试筛选包含数值/日期名称的列,使用as.Date变量。

例如,考虑这个小数据库:

  1. dt <- data.table(
  2. names = c("A", "B", "C"),
  3. `2020-01-01` = c(1, NA, 2),
  4. `2020-01-02` = c(3, 4, 5),
  5. `2020-01-03` = c(6, 7, 8)
  6. )

我当前正在按以下方式筛选所需的日期列:

  1. dt1 <- dt %>% filter(!is.na(`2020-01-01`)) %>% select(names)

我的想法是创建一个meeting_date变量,这个变量应该作为我的所有R代码的日期参考。

  1. meeting_date <- as.Date("2020-01-01")

但是当然,以下代码不起作用:

  1. dt1 <- dt %>% filter(!is.na(meeting_date)) %>% select(names)

这是因为缺少反引号,所以我尝试了以下代码但未成功:

  1. dt1 <- dt %>% filter(!is.na(paste("`", meeting_date, "`", sep=""))) %>% select(names)
  2. 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:

  1. dt &lt;- data.table(
  2. names = c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;),
  3. `2020-01-01` = c(1, NA, 2),
  4. `2020-01-02` = c(3, 4, 5),
  5. `2020-01-03` = c(6, 7, 8)
  6. )

I am currently filtering the desired date column as follows:

  1. 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.

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

But of course the code:

  1. 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:

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

可以这样做:

  1. meeting_date <- as.Date("2020-01-01")
  2. dt %>%
  3. filter_at(vars(one_of(as.character(meeting_date))), ~ !is.na(.))
  4. names 2020-01-01 2020-01-02 2020-01-03
  5. 1 A 1 3 6
  6. 2 C 2 5 8
英文:

You can do:

  1. meeting_date &lt;- as.Date(&quot;2020-01-01&quot;)
  2. dt %&gt;%
  3. filter_at(vars(one_of(as.character(meeting_date))), ~ !is.na(.))
  4. names 2020-01-01 2020-01-02 2020-01-03
  5. 1 A 1 3 6
  6. 2 C 2 5 8

答案2

得分: 0

你可以使用以下的代码来使用 subset + is.na

  1. meeting_date <- "2020-01-01"
  2. dtout <- subset(dt, as.vector(!is.na(dt[, ..meeting_date])))

这将得到如下的结果:

  1. > dtout
  2. names 2020-01-01 2020-01-02 2020-01-03
  3. 1: A 1 3 6
  4. 2: C 2 5 8
英文:

You can use subset + is.naas below

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

such that

  1. &gt; dtout
  2. names 2020-01-01 2020-01-02 2020-01-03
  3. 1: A 1 3 6
  4. 2: C 2 5 8

答案3

得分: 0

长数据应该更容易处理:

  1. library(data.table)
  2. dt <- data.table(
  3. names = c("A", "B", "C"),
  4. `2020-01-01` = c(1, NA, 2),
  5. `2020-01-02` = c(3, 4, 5),
  6. `2020-01-03` = c(6, 7, 8)
  7. )
  8. #将数据转换为“长”格式并将新的“name”列更改为日期
  9. #在此过程中更改令人困惑的列“name”为日期。
  10. dt_long <- dt %>% pivot_longer(-names) %>%
  11. mutate(date = lubridate::ymd(name)) %>%
  12. select(-name)
  13. meeting_date <- as.Date("2020-01-01")
  14. dt_long %>% filter(date == meeting_date & (!is.na(value)))
英文:

Long data should be easier to work with:

  1. library(data.table)
  2. dt &lt;- data.table(
  3. names = c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;),
  4. `2020-01-01` = c(1, NA, 2),
  5. `2020-01-02` = c(3, 4, 5),
  6. `2020-01-03` = c(6, 7, 8)
  7. )
  8. #Make data &#39;long&#39; &amp; change the new &#39;name&#39; column to dates
  9. # change confusing column &#39;name&#39; to date while we&#39;re at it.
  10. dt_long &lt;- dt %&gt;% pivot_longer(-names) %&gt;%
  11. mutate(date = lubridate::ymd(name)) %&gt;%
  12. select(-name)
  13. meeting_date &lt;- as.Date(&quot;2020-01-01&quot;)
  14. 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:

确定