英文:
New column from first observation of several columns dplyr
问题
我有以下数据,想要从A、B和C创建"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))
我只想要从任何一列中获取第一个非-NA观察值。
我尝试在dplyr中使用```across```,但无法弄清语法。
有任何想法?
英文:
I have the following data and want to create the "New" variable from A, B, and C:
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))
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
函数:
df <- type.convert(df, as.is = TRUE)
df %>%
mutate(New = do.call(pmax, c(across(A:C), na.rm = TRUE)))
# 一个 tibble: 4 × 4
A B C New
<int> <int> <int> <int>
1 NA NA 1 1
2 NA 3 NA 3
3 4 4 NA 4
4 NA 5 5 5
英文:
With pmax
:
df <- type.convert(df, as.is = TRUE)
df %>%
mutate(New = do.call(pmax, c(across(A:C), na.rm = TRUE)))
# A tibble: 4 × 4
A B C New
<int> <int> <int> <int>
1 NA NA 1 1
2 NA 3 NA 3
3 4 4 NA 4
4 NA 5 5 5
答案2
得分: 2
We may need to first convert the "NA"
to NA
before extracting the first non-NA
library(dplyr)
library(purrr)
df1 %>%
type.convert(as.is = TRUE) %>%
mutate(New = invoke(coalesce, pick(A:C)))
-output
# A tibble: 4 × 4
A B C New
<int> <int> <int> <int>
1 NA NA 1 1
2 NA 3 NA 3
3 4 4 NA 4
4 NA 5 5 5
Or with fcoalecse
from data.table
library(data.table)
df1 %>%
type.convert(as.is = TRUE) %>%
mutate(New = fcoalesce(pick(A:C)))
请注意,代码部分已排版并保留在原文中。
英文:
We may need to first convert the "NA"
to NA
before extracting the first non-NA
library(dplyr)
library(purrr)
df1 %>%
type.convert(as.is = TRUE) %>%
mutate(New = invoke(coalesce, pick(A:C)))
-output
# A tibble: 4 × 4
A B C New
<int> <int> <int> <int>
1 NA NA 1 1
2 NA 3 NA 3
3 4 4 NA 4
4 NA 5 5 5
Or with fcoalecse
from data.table
library(data.table)
df1 %>%
type.convert(as.is = TRUE) %>%
mutate(New = fcoalesce(pick(A:C)))
</details>
# 答案3
**得分**: 1
Base R option using `apply` by selecting the first `[1]` non NA with `is.na` like this:
``` r
df$New <- apply(df, 1, \(x) x[!is.na(x)][1])
df
#> A B C New
#> 1 <NA> <NA> 1 1
#> 2 <NA> 3 <NA> 3
#> 3 4 4 <NA> 4
#> 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:
df$New <- apply(df, 1, \(x) x[!is.na(x)][1])
df
#> A B C New
#> 1 <NA> <NA> 1 1
#> 2 <NA> 3 <NA> 3
#> 3 4 4 <NA> 4
#> 4 <NA> 5 5 5
<sup>Created on 2023-03-03 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论