我想在一个数据集中标明一栏下的所有条目均为北美。 令人惊讶的是,一些浏览正在铺设,有些是数字。
用所有数字的栏目来做这项工作,将处以罚款。 让我们说,我们希望有一个指标变量,告诉我们,下一个数据集的a至c栏是否都是北美国家。
df <- data.frame(a = c(1,4,5,NA),
b = c(NA,4,2,NA),
c = c(3,7,8,NA),
d = c(2,1,1,1))
df
# output
a b c d
1 1 NA 3 2
2 4 4 7 1
3 5 2 8 1
4 NA NA NA 1
Using dplyr
we can create a variable indicating what we need
df %>%
rowwise() %>%
mutate(allNA = case_when(all(is.na(c_across(cols = a:c))) ~ "allNA",
TRUE ~ "notAllNA")) %>%
ungroup()
# output
# A tibble: 4 × 5
a b c d allNA
<dbl> <dbl> <dbl> <dbl> <chr>
1 1 NA 3 2 notAllNA
2 4 4 7 1 notAllNA
3 5 2 8 1 notAllNA
4 NA NA NA 1 allNA
But if one of the variables is a string instead of a number
df <- data.frame(a = c(1,4,5,NA),
b = c(NA,"ava","dillion",NA),
c = c(3,7,8,NA),
d = c(2,1,1,1))
df
# output
a b c d
1 1 <NA> 3 2
2 4 ava 7 1
3 5 dillion 8 1
4 NA <NA> NA 1
同一法典有以下错误:
Error in `mutate()`:
ℹ In argument: `allNA = case_when(...)`.
ℹ In row 1.
Caused by error in `case_when()`:
! Failed to evaluate the left-hand side of formula 1.
Caused by error in `vec_c()`:
! Can t combine `a` <double> and `b` <character>.
Run `rlang::last_trace()` to see where the error occurred.
Any help appreciated.