从dplyr的多个列的第一个观察中创建新列

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

New column from first observation of several columns dplyr

问题

  1. 我有以下数据,想要从ABC创建"New"变量:

structure(list(A = c("NA", "NA", "4", "NA"), B = c("NA", "3", "4", "5"), C = c("1", "NA", "NA", "5"), New = c(1, 3, 4, 5)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -4L))

  1. 我只想要从任何一列中获取第一个非-NA观察值。
  2. 我尝试在dplyr中使用```across```,但无法弄清语法。
  3. 有任何想法?
英文:

I have the following data and want to create the "New" variable from A, B, and C:

  1. structure(list(A = c("NA", "NA", "4", "NA"), B = c("NA", "3",
  2. "4", "5"), C = c("1", "NA", "NA", "5"), New = c(1, 3, 4, 5)), class = c("tbl_df",
  3. "tbl", "data.frame"), row.names = c(NA, -4L))

I just want the first non-NA observation from any of the columns
I have attempted to use across in dplyr but have not been able to figure out the syntax.
Any ideas?

答案1

得分: 3

使用 pmax 函数:

  1. df <- type.convert(df, as.is = TRUE)
  2. df %>%
  3. mutate(New = do.call(pmax, c(across(A:C), na.rm = TRUE)))
  4. # 一个 tibble: 4 × 4
  5. A B C New
  6. <int> <int> <int> <int>
  7. 1 NA NA 1 1
  8. 2 NA 3 NA 3
  9. 3 4 4 NA 4
  10. 4 NA 5 5 5
英文:

With pmax:

  1. df &lt;- type.convert(df, as.is = TRUE)
  2. df %&gt;%
  3. mutate(New = do.call(pmax, c(across(A:C), na.rm = TRUE)))
  4. # A tibble: 4 &#215; 4
  5. A B C New
  6. &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;
  7. 1 NA NA 1 1
  8. 2 NA 3 NA 3
  9. 3 4 4 NA 4
  10. 4 NA 5 5 5

答案2

得分: 2

We may need to first convert the &quot;NA&quot; to NA before extracting the first non-NA

  1. library(dplyr)
  2. library(purrr)
  3. df1 %>%
  4. type.convert(as.is = TRUE) %>%
  5. mutate(New = invoke(coalesce, pick(A:C)))

-output

  1. # A tibble: 4 × 4
  2. A B C New
  3. <int> <int> <int> <int>
  4. 1 NA NA 1 1
  5. 2 NA 3 NA 3
  6. 3 4 4 NA 4
  7. 4 NA 5 5 5

Or with fcoalecse from data.table

  1. library(data.table)
  2. df1 %>%
  3. type.convert(as.is = TRUE) %>%
  4. mutate(New = fcoalesce(pick(A:C)))

请注意,代码部分已排版并保留在原文中。

英文:

We may need to first convert the &quot;NA&quot; to NA before extracting the first non-NA

  1. library(dplyr)
  2. library(purrr)
  3. df1 %&gt;%
  4. type.convert(as.is = TRUE) %&gt;%
  5. mutate(New = invoke(coalesce, pick(A:C)))

-output

  1. # A tibble: 4 &#215; 4
  2. A B C New
  3. &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;
  4. 1 NA NA 1 1
  5. 2 NA 3 NA 3
  6. 3 4 4 NA 4
  7. 4 NA 5 5 5

Or with fcoalecse from data.table

  1. library(data.table)
  2. df1 %&gt;%
  3. type.convert(as.is = TRUE) %&gt;%
  4. mutate(New = fcoalesce(pick(A:C)))
  5. </details>
  6. # 答案3
  7. **得分**: 1
  8. Base R option using `apply` by selecting the first `[1]` non NA with `is.na` like this:
  9. ``` r
  10. df$New <- apply(df, 1, \(x) x[!is.na(x)][1])
  11. df
  12. #> A B C New
  13. #> 1 <NA> <NA> 1 1
  14. #> 2 <NA> 3 <NA> 3
  15. #> 3 4 4 <NA> 4
  16. #> 4 <NA> 5 5 5

Created on 2023-03-03 with reprex v2.0.2

英文:

Base R option using apply by selecting the first [1] non NA with is.na like this:

  1. df$New &lt;- apply(df, 1, \(x) x[!is.na(x)][1])
  2. df
  3. #&gt; A B C New
  4. #&gt; 1 &lt;NA&gt; &lt;NA&gt; 1 1
  5. #&gt; 2 &lt;NA&gt; 3 &lt;NA&gt; 3
  6. #&gt; 3 4 4 &lt;NA&gt; 4
  7. #&gt; 4 &lt;NA&gt; 5 5 5

<sup>Created on 2023-03-03 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年3月4日 02:51:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630842.html
匿名

发表评论

匿名网友

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

确定