简体   繁体   English

使用cross或c_across计算行计数

[英]Compute row-wise counts using across or c_across

I'd like to ask a question inspired by this question asked years ago here in stack overflow我想问一个问题, 这个问题的灵感来自几年前在堆栈溢出中提出的这个问题

given the data frame: input_df给定数据框:input_df

  num_col_1 num_col_2 text_col_1 text_col_2
1         1         4        yes        yes
2         2         5         no        yes
3         3         6         no       <NA>

this chunk of code这段代码

library(dplyr)    
df %>%
  mutate(sum_yes = rowSums(.[c("text_col_1", "text_col_2")] == "yes"))

will produce this new dataframe将生产这个新的 dataframe

> output_df
  num_col_1 num_col_2 text_col_1 text_col_2 sum_yes
1         1         4        yes        yes       2
2         2         5         no        yes       1
3         3         6         no       <NA>       0

The question is, how do you do the same with modern dplyr verbs across and c_across ?问题是,你如何对现代的 dplyr 动词 cross 和c_across做同样的事情

thank you.谢谢你。

1) c_across Here c_across returns a one row tibble containing the columns indicated by its argument. 1) c_across这里c_across返回一个包含由其参数指示的列的单行 tibble。

library(dplyr)

input_df %>%
  rowwise %>%
  mutate(sum = sum( c_across(starts_with("text")) == "yes", na.rm = TRUE)) %>%
  ungroup

giving:给予:

# A tibble: 3 x 5
  num_col_1 num_col_2 text_col_1 text_col_2   sum
      <int>     <int> <chr>      <chr>      <int>
1         1         4 yes        yes            2
2         2         5 no         yes            1
3         3         6 no         <NA>           0

2) across This gives the same result. 2)cross这给出了相同的结果。 across returns a tibble with only the columns indicated by its argument. cross 返回一个仅包含其参数指示across列的小标题。

input_df %>%
  mutate(sum = rowSums( across(starts_with("text")) == "yes", na.rm = TRUE)) 

Summing the scores for yes将分数相加为是

In case it is of interest to sum the scores corresponding to the yes values:如果有兴趣将对应于 yes 值的分数求和:

3) c_across 3) c_across

library(dplyr)

input_df %>%
  rowwise %>%
  mutate(sum = sum( c_across(starts_with("num")) * 
    (c_across(starts_with("text")) == "yes"), na.rm = TRUE)) %>%
  ungroup

giving:给予:

  # A tibble: 3 x 5
  num_col_1 num_col_2 text_col_1 text_col_2   sum
      <int>     <int> <chr>      <chr>      <int>
1         1         4 yes        yes            5
2         2         5 no         yes            5
3         3         6 no         <NA>           0

4) across The output is the same as (3). 4)跨output同(3)。

input_df %>%
  mutate(sum = rowSums(across(starts_with("num")) * 
                 (across(starts_with("text")) == "yes"), na.rm = TRUE))

Note笔记

The input in reproducible form:可重现形式的输入:

Lines <- "  num_col_1 num_col_2 text_col_1 text_col_2
1         1         4        yes        yes
2         2         5         no        yes
3         3         6         no         NA"
input_df <- read.table(text = Lines)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM