标签: across

以使用 mutate、cross 和 case_when 为条件连接一列中的列名

我想要:

  1. 使用acrosscase_when检查列 A1-A3 == 1
  2. 连接 A1-A3 == 1 和
  3. 使用连接的列名改变新列

我的数据框:

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)

r dplyr across

17
推荐指数
2
解决办法
521
查看次数

使用 dplyr rename(across(

嘿,我正在尝试通过使用新版本的 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

r rename dplyr tidyverse across

13
推荐指数
2
解决办法
3337
查看次数

在 dplyr 包中找不到跨函数

我想在其中使用该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()的如下: …

r dplyr across

7
推荐指数
1
解决办法
2099
查看次数

R dplyr:当...将引用数据中的变量名称时,如何将...与summary(across())一起使用?

我想要一个灵活的函数,summarize其中使用:

\n
    \n
  1. 聚合函数由用户给出
  2. \n
  3. 聚合函数可能会使用更多参数来引用数据本身中的变量。
  4. \n
\n

一个很好的例子是用户提供fun=weighted.mean()并指定权重参数w

\n

现在,我正在尝试使用.... 问题是我找不到一种方法来...引用数据框中的变量?下面的示例是使用 给出的across(),但如果我使用 ,也会发生同样的情况summarize_at()

\n

谢谢!!

\n
library(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)

r dplyr tidyverse rlang across

7
推荐指数
1
解决办法
2437
查看次数

如何使用 dplyr::across() 内的 n() 按组计算行数?

在以前版本的 dplyr 中,如果我想使用 获取行计数以及其他汇总值summarise(),我可以执行类似的操作

\n
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\n
Run Code Online (Sandbox Code Playgroud)\n

我使用新函数获得相同输出的本能across()

\n
df %>%\n  group_by(group) %>% \n  summarise(across(value, list(sum = sum, count = n)))\nError: Problem with `summarise()` …
Run Code Online (Sandbox Code Playgroud)

r dplyr across

7
推荐指数
1
解决办法
3813
查看次数

如何将 across() 函数与 mutate() 和 case_when() 结合起来,根据条件对多列中的值进行变异?

我有人口统计数据集,其中包括家庭成员的年龄。这是通过调查收集的,参与者可以拒绝提供自己的年龄。

结果是一个数据集,每行一个家庭(每个家庭都有一个家庭 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)

r dplyr across

6
推荐指数
1
解决办法
2865
查看次数

您可以使用 dplyr cross() 来遍历成对的列吗?

我有 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)

r dplyr tidyverse across

6
推荐指数
2
解决办法
336
查看次数

组合:rowwise()、mutate()、cross(),用于多种功能

这在某种程度上与此相关的问题:原则上我试着去了解如何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)

r dplyr rowwise across

6
推荐指数
2
解决办法
170
查看次数

当使用“mutate”和“across”时,如何保留旧列并重命名新列

当我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)

r rename dplyr across

6
推荐指数
1
解决办法
1511
查看次数

为什么在 dplyr 中使用 mutate + across 时“scale”会创建末尾带有“[,1]”的列?

请参阅下面的代码。

\n

mutate(across(everything(), scale, .names = "{.col}_z"))语法的一部分是生成末尾[,1]附加的列。

\n

两个问题:

\n
    \n
  1. 为什么会发生这种情况?
  2. \n
  3. 我怎样才能避免或消除它?
  4. \n
\n
library(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)

r scale dplyr across

6
推荐指数
1
解决办法
497
查看次数

标签 统计

across ×10

dplyr ×10

r ×10

tidyverse ×3

rename ×2

rlang ×1

rowwise ×1

scale ×1