Bin/对R中的p值进行分类

Bin/对R中的p值进行分类,r,categories,bin,p-value,R,Categories,Bin,P Value,如何在R中创建一个函数,将p值转换为以下值 If p>0.05 -> ns If p<0.05 -> 1 If p<10-2 -> 2 If p<10-3 -> 3 If p<10-4 -> 4 etc... 如果p>0.05->ns 如果p1 如果p2 如果p3 如果p4 等 我们可以在执行自定义分类时使用case\u library(dplyr) f1 <- function(pval) { cas

如何在R中创建一个函数,将p值转换为以下值

 If p>0.05 -> ns
 If p<0.05 -> 1
 If p<10-2 -> 2
 If p<10-3 -> 3
 If p<10-4 -> 4
 etc...
如果p>0.05->ns
如果p1
如果p2
如果p3
如果p4
等

我们可以在执行自定义分类时使用
case\u

library(dplyr)
f1 <- function(pval) {
       case_when(pval > 0.5 ~ 'ns',
                 pval < 1e-4 ~ '4',
                 pval < 1e-3 ~ '3',
                 pval < 1e-2 ~ '2',
                 pval < 0.05 ~ '1'
                
                
                 )
      }


f1(c(0.04, 1.2,  0.00005))
#[1] "1"  "ns" "4"            
库(dplyr)
f1 0.5~'ns',
pval<1e-4~'4',
pval<1e-3~'3',
pval<1e-2~'2',
pval<0.05~'1'
)
}
f1(c(0.04,1.2,0.00005))
#[1] “1”“ns”“4”

如果您只想转到10e-4,@akrun答案是好的。一个更普遍的解决方案,它会一直遵循幂函数,如下所示:

# from here: https://stackoverflow.com/questions/6461209/how-to-round-up-to-the-nearest-10-or-100-or-x
roundUp <- function(x) 10^ceiling(log10(x))

categorize_p = function(ps) {
  logged = -log10(roundUp(ps))
  logged[ps > .05] = "ns"
  return(logged)
}

categorize_p(c(.1,.049, .001, .01))
#[1] "ns" "1"  "3"  "2" 
#从这里开始:https://stackoverflow.com/questions/6461209/how-to-round-up-to-the-nearest-10-or-100-or-x
综述.05]=“ns”
返回(已记录)
}
分类(c(.1.049.001.01))
#[1] “ns”“1”“3”“2”