我想要:
across和case_when检查列 A1-A3 == 1我的数据框:
df <- tribble(
~ID, ~A1, ~A2, ~A3,
1, 0, 1, 1,
2, 0, 1, 1,
3, 1, 1, 1,
4, 1, 0, 1,
5, 0, 1, 0)
Run Code Online (Sandbox Code Playgroud)
期望输出:
# A tibble: 5 x 5
ID A1 A2 A3 New_Col
<dbl> <dbl> <dbl> <dbl> <chr>
1 1 0 1 1 A2 A3
2 2 0 1 1 A2 A3
3 3 1 1 1 A1 …Run Code Online (Sandbox Code Playgroud) 嘿,我正在尝试通过使用新版本的 dplyr 添加“Last_”来重命名某些列,但我一直收到此错误
Error: `across()` must only be used inside dplyr verbs.
Run Code Online (Sandbox Code Playgroud)
这是我的代码
data %>% rename(across(everything(), ~paste0("Last_", .)))
Run Code Online (Sandbox Code Playgroud)
dplyr 版本:v1.0.2
我想在其中使用该across()函数dplyr但出现错误。例如,运行
iris %>%
group_by(Species) %>%
summarise(across(starts_with("Sepal"), mean))
Run Code Online (Sandbox Code Playgroud)
给我
Error in across(starts_with("Sepal"), mean) :
could not find function "across"
Run Code Online (Sandbox Code Playgroud)
across()是最近的介绍https://towardsdatascience.com/what-you-need-to-know-about-the-new-dplyr-1-0-0-7eaaaf6d78ac in dplyr. 但是,包dplyr已更新并加载
packageVersion('dplyr')
[1] ‘1.0.0’
Run Code Online (Sandbox Code Playgroud)
检查内部 dplyr
ls("package:dplyr")
[1] "%>%" "add_count" "add_count_" "add_row" "add_rownames" "add_tally"
[7] "add_tally_" "all_equal" "all_vars" "anti_join" "any_vars" "arrange"
[13] "arrange_" "arrange_all" "arrange_at" "arrange_if" "as_data_frame" "as_label"
Run Code Online (Sandbox Code Playgroud)
我发现它across不存在,但是如果我在 helper 中查找该函数,?across我会得到解释across.
如何across上班?
- - - 编辑 - - -
我sessionInfo()的如下: …
我想要一个灵活的函数,summarize其中使用:
一个很好的例子是用户提供fun=weighted.mean()并指定权重参数w。
现在,我正在尝试使用.... 问题是我找不到一种方法来...引用数据框中的变量?下面的示例是使用 给出的across(),但如果我使用 ,也会发生同样的情况summarize_at()
谢谢!!
\nlibrary(tidyverse)\nfo1 <- function(df, fun=mean, ...){\n df %>% \n group_by(Species) %>% \n summarise(across(starts_with("sepal"), fun, ...))\n}\n\nfo1(iris)\n#> `summarise()` ungrouping output (override with `.groups` argument)\n#> # A tibble: 3 x 3\n#> Species Sepal.Length Sepal.Width\n#> <fct> <dbl> <dbl>\n#> 1 setosa 5.01 3.43\n#> 2 versicolor 5.94 2.77\n#> 3 virginica 6.59 2.97\nfo1(iris, fun=weighted.mean)\n#> `summarise()` ungrouping output (override …Run Code Online (Sandbox Code Playgroud) 在以前版本的 dplyr 中,如果我想使用 获取行计数以及其他汇总值summarise(),我可以执行类似的操作
library(tidyverse)\n\ndf <- tibble(\n group = c("A", "A", "B", "B", "C"),\n value = c(1, 2, 3, 4, 5)\n)\n\ndf %>%\n group_by(group) %>% \n summarise(total = sum(value), count = n())\n\n`summarise()` ungrouping output (override with `.groups` argument)\n\n# A tibble: 3 x 3\n group total count\n <chr> <dbl> <int>\n1 A 3 2\n2 B 7 2\n3 C 5 1\nRun Code Online (Sandbox Code Playgroud)\n我使用新函数获得相同输出的本能across()是
df %>%\n group_by(group) %>% \n summarise(across(value, list(sum = sum, count = n)))\nError: Problem with `summarise()` …Run Code Online (Sandbox Code Playgroud) 我有人口统计数据集,其中包括家庭成员的年龄。这是通过调查收集的,参与者可以拒绝提供自己的年龄。
结果是一个数据集,每行一个家庭(每个家庭都有一个家庭 ID 代码),列中包含各种家庭特征,例如年龄。拒绝编码为“R”的响应,您可以使用以下代码重新创建示例:
df <- list(Household_ID = c("1A", "1B", "1C", "1D", "1E"),
AGE1 = c("25", "47", "39", "50", "R"),
AGE2 = c("66", "23", "71", "R", "16"),
AGE3 = c("28", "17", "R", "R", "80"),
AGE4 = c("81", "22", "48", "59", "R"))
df <- as_tibble(df)
> df
# A tibble: 5 x 5
Household_ID AGE1 AGE2 AGE3 AGE4
<chr> <chr> <chr> <chr> <chr>
1 1A 25 66 28 81
2 1B 47 23 17 22
3 1C 39 71 R 48 …Run Code Online (Sandbox Code Playgroud) 我有 18 对变量,我想对它们进行成对数学运算以计算 18 个新变量。将公式应用于一列时,dplyr 中的 cross() 函数非常方便。有没有办法将 cross() 应用于成对的列?
简单划分 2 个变量的小例子(我的实际代码会更复杂,一些 ifelse,...):
library(tidyverse)
library(glue)
# filler data
df <- data.frame("label" = c('a','b','c','d'),
"A" = c(4, 3, 8, 9),
"B" = c(10, 0, 4, 1),
"error_A" = c(0.4, 0.3, 0.2, 0.1),
"error_B" = c(0.3, 0, 0.4, 0.1))
# what I want to have in the end
# instead of just 2 (A, B), I have 18
df1 <- df %>% mutate(
'R_A' = A/error_A,
'R_B' = B/error_B
)
# …Run Code Online (Sandbox Code Playgroud) 这在某种程度上与此相关的问题:原则上我试着去了解如何rowwise操作与mutate多个列采用更然后像(1个功能mean(),sum(),min()等)的工作。
我已经了解到可以across完成这项工作而不是c_across。我已经学会了该功能mean()是将不同的功能min()以如下方式mean()不起作用在dataframes,我们需要将其更改到可以不公开或as.matrix做载体- >从Ronak沙阿了解到这里了解横行()和 c_across()
现在以我的实际情况为例:我能够完成这项任务,但我丢失了一个 column d。我怎样才能避免d这种设置中的柱子松动。
我的 df:
df <- structure(list(a = 1:5, b = 6:10, c = 11:15, d = c("a", "b",
"c", "d", "e"), e = 1:5), row.names = c(NA, -5L), class = c("tbl_df",
"tbl", "data.frame"))
Run Code Online (Sandbox Code Playgroud)
不工作:
df %>%
rowwise() %>%
mutate(across(a:e),
avg = mean(unlist(cur_data()), na.rm = TRUE),
min = …Run Code Online (Sandbox Code Playgroud) 当我mutate across数据时,选择的列.cols将被突变的结果替换。我怎样才能在以下情况下执行此操作:
.cols保持输出中选择的列mutate适当地自动重命名由?创建的列例如:
require(dplyr)
#> Loading required package: 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
require(magrittr)
#> Loading required package: magrittr
set.seed(7337)
## Create arbitrary tibble
myTibble <- tibble(x = 1:10,
y = runif(10),
z = y * pi)
## I can mutate across these columns
mutate(myTibble, …Run Code Online (Sandbox Code Playgroud) 请参阅下面的代码。
\nmutate(across(everything(), scale, .names = "{.col}_z"))语法的一部分是生成末尾[,1]附加的列。
两个问题:
\nlibrary(dplyr)\n\n# Input\ndf_test <- tibble(x = c(1, 2, 3, 4), y = c(5, 6, 7, 8))\n\n# My code generating x_z and y_z\ndf_scaled <- df_test %>% \n mutate(across(everything(), scale, .names = "{.col}_z"))\n\n# Output\ndf_scaled\n#> # A tibble: 4 \xc3\x97 4\n#> x y x_z[,1] y_z[,1]\n#> <dbl> <dbl> <dbl> <dbl>\n#> 1 1 5 -1.16 -1.16 \n#> 2 2 6 -0.387 -0.387\n#> 3 3 7 0.387 0.387\n#> …Run Code Online (Sandbox Code Playgroud)