我生成 1 和 0 的数据帧如下:
library(tidyverse)
library(glue)
num_var <- 3
rep(list(c(0L, 1L)), num_var) %>%
set_names(glue("var_{seq_len(num_var)}")) %>%
expand.grid() %>%
mutate(total = rowSums(.)) %>%
select(total, everything()) %>%
arrange(total, desc(var_1, var_2, var_3))
#> total var_1 var_2 var_3
#> 1 0 0 0 0
#> 2 1 1 0 0
#> 3 1 0 1 0
#> 4 1 0 0 1
#> 5 2 1 1 0
#> 6 2 1 0 1
#> 7 2 0 1 1
#> 8 3 1 …Run Code Online (Sandbox Code Playgroud) library(tidyverse)
df <- tibble(Date = c(rep(as.Date("2020-01-01"), 3), NA),
col1 = 1:4,
thisCol = c(NA, 8, NA, 3),
thatCol = 25:28,
col999 = rep(99, 4))
#> # A tibble: 4 x 5
#> Date col1 thisCol thatCol col999
#> <date> <int> <dbl> <int> <dbl>
#> 1 2020-01-01 1 NA 25 99
#> 2 2020-01-01 2 8 26 99
#> 3 2020-01-01 3 NA 27 99
#> 4 NA 4 3 28 99
Run Code Online (Sandbox Code Playgroud)
我实际的 R 数据框有数百列,这些列的命名并不明确,但可以通过df上面的数据框进行近似。
我想用 替换所有值,NA但 …
我已经通读了《Programming with dplyr》并理解了这一点rename()并select()使用了整洁的选择。我试图将其与粘合语法结合起来,以使用新的双卷曲语法(rlang v0.4.0)创建自定义函数,但是我得到了额外的引号:
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
sel_var = "homeworld"
# Attempt at using (newer) double curly syntax:
starwars %>%
select("{{sel_var}}_old" := {{ sel_var }})
#> # A tibble: 87 x 1
#> `"homeworld"_old`
#> <chr>
#> 1 Tatooine
#> # ... with 77 more …Run Code Online (Sandbox Code Playgroud) 让我们考虑这个简单的数据集
set.seed(12345)
df <- data.frame(a1 = rnorm(5), a2 = rnorm(5), a3 = rnorm(5),
b1 = rnorm(5), b2 = rnorm(5), b3 = rnorm(5),
c1 = rnorm(5), c2 = rnorm(5), c3 = rnorm(5))
Run Code Online (Sandbox Code Playgroud)
看起来像
a1 a2 a3 b1 b2 b3 c1 c2 c3
1 0.5855288 -1.8179560 -0.1162478 0.8168998 0.7796219 1.8050975 0.8118732 0.49118828 1.1285108
2 0.7094660 0.6300986 1.8173120 -0.8863575 1.4557851 -0.4816474 2.1968335 -0.32408658 -2.3803581
3 -0.1093033 -0.2761841 0.3706279 -0.3315776 -0.6443284 0.6203798 2.0491903 -1.66205024 -1.0602656
4 -0.4534972 -0.2841597 0.5202165 1.1207127 -1.5531374 0.6121235 1.6324456 1.76773385 …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用该dplyr::filter()功能来过滤我的小标题的特定行。
这是我的小标题的一部分head(raw.tb):
A tibble: 738 x 4
geno ind X Y
<chr> <chr> <int> <int>
1 san1w16 A1 467 383
2 san1w16 A1 465 378
3 san1w16 A1 464 378
4 san1w16 A1 464 377
5 san1w16 A1 464 376
6 san1w16 A1 464 375
7 san1w16 A1 463 375
8 san1w16 A1 463 374
9 san1w16 A1 463 373
10 san1w16 A1 463 372
# ... with 728 more rows
Run Code Online (Sandbox Code Playgroud)
当我要求: raw.tb %>% dplyr::filter(ind == …
试图排除
tidyr::gather() shiny::selectInput)而不是via 作为我的函数的输入...如何通过整洁的eval功能实现这一目标?
由于我经由单个函数参数传递多个列名,以为需要使用!!!(引文结束剪接)而不是!!如在奠定了与dplyr编程.但这似乎并没有很好地发挥作用,tidyselect::vars_select()而且似乎-造成了麻烦.
这是我想要做的基本事情:
library(magrittr)
gather_data_1 <- function(dat, ...) {
dat %>% tidyr::gather("key", "value", ...)
}
mtcars %>% gather_data_1(-mpg, -cyl) %>% head()
#> mpg cyl key value
#> 1 21.0 6 disp 160
#> 2 21.0 6 disp 160
#> 3 22.8 4 disp 108
#> 4 21.4 6 disp 258
#> 5 18.7 8 disp …Run Code Online (Sandbox Code Playgroud) “tidyselect”包提供了一个选择辅助函数where。where用于通过自定义函数选择数据框列。它是“tidyselect”的内部函数。这意味着where不会被加载到您的命名空间中,您只能通过tidyselect:::where.
但是,我从 dplyr 插图中看到了以下示例:columnwise Operations。
starwars %>%
summarise(across(where(is.character), ~ length(unique(.x))))
#> # A tibble: 1 x 8
#> name hair_color skin_color eye_color sex gender homeworld species
#> <int> <int> <int> <int> <int> <int> <int> <int>
#> 1 87 13 31 15 5 3 49 38
Run Code Online (Sandbox Code Playgroud)
在此示例中,where编写时没有前缀“tidyselect:::”,但显然,代码中没有错误,并且它产生了有意义的结果。这对我来说似乎很奇怪。我想知道为什么代码可以正常运行。
我猜这是由于“代码引用”,它是 tidyeval 方法的一部分。粗略地说,代码引用将代码挂起为表达式,并稍后在“内部环境”中计算表达式。这只是一个直观的猜测,我不知道如何测试。
我希望有人可以帮助我解决“哪里”的问题,或者留下一些关于代码如何为我工作的参考。
如果给出 NULL,我正在努力设置默认选择方法。例如,假设我想实现一个函数,对某些 tidyselect 方法、符号或字符串的所有值进行平方,如果没有给出,则默认情况下它对所有数值进行平方。这是我正在尝试处理的案例的代表:
#Load package
suppressMessages(library(dplyr))
#Define function
square_columns <- function(data, target = NULL){
if (is.null(target)){
target <- rlang::expr(where(is.numeric))
}
data %>%
dplyr::mutate(
dplyr::across(!!target, magrittr::raise_to_power, 2)
)
}
# Examples
# Works
iris %>%
square_columns(target = 'Sepal.Length') %>%
invisible()
# Works
iris %>%
square_columns(target = c('Sepal.Length', 'Sepal.Width')) %>%
invisible()
# Works
iris %>%
square_columns() %>%
invisible()
# Fails
iris %>%
square_columns(target = contains('Sepal'))
#> Error: `contains()` must be used within a *selecting* function.
#> i See <https://tidyselect.r-lib.org/reference/faq-selection-context.html>.
# Fails …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个将 wrap 的函数,并将dplyr::coalesce()接收一个数据对象和列名以进行合并。到目前为止,我的尝试都失败了。
library(dplyr)
df <-
data.frame(col_a = c("bob", NA, "bob", NA, "bob"),
col_b = c(NA, "danny", NA, NA, NA),
col_c = c("paul", NA, NA, "paul", NA))
## col_a col_b col_c
## 1 bob <NA> paul
## 2 <NA> danny <NA>
## 3 bob <NA> <NA>
## 4 <NA> <NA> paul
## 5 bob <NA> <NA>
Run Code Online (Sandbox Code Playgroud)
coalesce_plus_1 <- function(data, vars) {
data %>%
mutate(coalesced_col = coalesce(!!! rlang::syms(tidyselect::vars_select(names(.), vars))))
}
Run Code Online (Sandbox Code Playgroud)
coalesce_plus_2 <- function(data, vars) {
data %>% …Run Code Online (Sandbox Code Playgroud) 这是我的代码:
\nempty <- ""\n\nmtcars %>% select(mpg,empty)\nRun Code Online (Sandbox Code Playgroud)\nError in `select()`:\n! Can't subset columns that don't exist.\n\xe2\x9c\x96 Column `` doesn't exist.\nRun Code Online (Sandbox Code Playgroud)\n该empty对象是 for 循环的输出。所以如果空等于""我不需要选择任何内容,但会出现以下错误。
换句话说,我应该能够选择这样的东西:mtcars %>% select(mpg,)。我需要转变""为
我怎样才能避免这个错误?
\n我知道这可能是一个愚蠢的问题,但我很好奇是否有任何区别,我更喜欢使用 str_detect 因为语法在我的大脑中更有意义。