简体   繁体   English

将函数应用于多列以有条件地改变观察

[英]apply a function to multiple columns to change observations conditionally

Can any one suggest dplyr solution to recode values into 1 and 0 based on some cutoff like x > 0.5.任何人都可以建议 dplyr 解决方案根据 x > 0.5 等一些截止值将值重新编码为 1 和 0。 I got the solution by applying apply but i need dplyr solution.我通过应用apply得到了解决方案,但我需要 dplyr 解决方案。

library(tidyverse)
#> Warning: package 'tidyverse' was built under R version 3.6.3
#> Warning: package 'ggplot2' was built under R version 3.6.3
#> Warning: package 'tibble' was built under R version 3.6.3
#> Warning: package 'tidyr' was built under R version 3.6.3
#> Warning: package 'readr' was built under R version 3.6.3
#> Warning: package 'purrr' was built under R version 3.6.3
#> Warning: package 'dplyr' was built under R version 3.6.3
#> Warning: package 'stringr' was built under R version 3.6.3
#> Warning: package 'forcats' was built under R version 3.6.3
library(QCA)
#> Warning: package 'QCA' was built under R version 3.6.3
#> Loading required package: admisc
#> Warning: package 'admisc' was built under R version 3.6.3
#> 
#> Attaching package: 'admisc'
#> The following objects are masked from 'package:dplyr':
#> 
#>     compute, recode
#> The following objects are masked from 'package:purrr':
#> 
#>     negate, simplify
#> The following object is masked from 'package:tidyr':
#> 
#>     expand
#> 
#> To cite package QCA in publications, please use:
#>   Dusa, Adrian (2019) QCA with R. A Comprehensive Resource.
#>   Springer International Publishing.
#> 
#> To run the graphical user interface, use: runGUI()
data(NF)
head(NF)
#>      A   I   M   U   W
#> AU 0.9 0.7 0.3 0.7 0.7
#> BE 0.7 0.1 0.1 0.9 0.7
#> DK 0.7 0.3 0.1 0.9 0.1
#> FR 0.7 0.9 0.1 0.1 0.9
#> DE 0.7 0.9 0.3 0.3 0.6
#> IE 0.1 0.7 0.9 0.7 0.9
apply(NF[, 1:4], 2, function(x) as.numeric(x > 0.5))
#>       A I M U
#>  [1,] 1 1 0 1
#>  [2,] 1 0 0 1
#>  [3,] 1 0 0 1
#>  [4,] 1 1 0 0
#>  [5,] 1 1 0 0
#>  [6,] 0 1 1 1
#>  [7,] 0 1 0 1
#>  [8,] 1 0 0 0
#>  [9,] 1 0 1 1
#> [10,] 1 0 1 1
#> [11,] 1 1 1 1
#> [12,] 1 1 0 0

NF %>% mutate_at(.x[,1:4] %>% as.numeric(.x > 0.5))
#> Error in eval(lhs, parent, parent): object '.x' not found

Created on 2020-11-21 by the reprex package (v0.3.0)reprex 包(v0.3.0) 于 2020 年 11 月 21 日创建

base R ,我们可以做

+(df > 0.5)

Does this work:这是否有效:

df %>% mutate(across(everything(), ~ +(. > 0.5)))
# A tibble: 6 x 5
      A     I     M     U     W
  <int> <int> <int> <int> <int>
1     1     1     0     1     1
2     1     0     0     1     1
3     1     0     0     1     0
4     1     1     0     0     1
5     1     1     0     0     1
6     0     1     1     1     1

Data used:使用的数据:

df
# A tibble: 6 x 5
      A     I     M     U     W
  <dbl> <dbl> <dbl> <dbl> <dbl>
1   0.9   0.7   0.3   0.7   0.7
2   0.7   0.1   0.1   0.9   0.7
3   0.7   0.3   0.1   0.9   0.1
4   0.7   0.9   0.1   0.1   0.9
5   0.7   0.9   0.3   0.3   0.6
6   0.1   0.7   0.9   0.7   0.9
library(dplyr)

NF %>% 
    mutate_at(
      vars(A,I,M,U) 
      , ~case_when(.x > 0.5 ~ 1, TRUE ~ 0)
    )

Your error is that the first argument of mutate_at expects a specification of which variables to mutate.您的错误是mutate_at的第一个参数需要指定要变异的变量。 Here I specified variables A, I, M, U.这里我指定了变量 A、I、M、U。

The second argument specifies what to do.第二个参数指定要做什么。 The '~' symbol tells R that we now specify a function. '~' 符号告诉 R 我们现在指定了一个函数。 We write '.我们写 '。 x' to refer to the inputs of the function. x' 指的是函数的输入。

case_when is a type of ifelse function, where TRUE is the equivalent to the else statement. case_when是一种ifelse函数,其中TRUE相当于else语句。

This should do the trick!这应该可以解决问题!

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

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