Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用p值替换R中数据框中的星星_R_Dataframe_Substitution_P Value - Fatal编程技术网

用p值替换R中数据框中的星星

用p值替换R中数据框中的星星,r,dataframe,substitution,p-value,R,Dataframe,Substitution,P Value,我有一个data.framedfP,其中一列Spearman\u p包含p值(数字数据)。我想用它们代替p值汇总星。我使用以下代码: dfP$Spearman_p[dfP$Spearman_p < 0.0001] <- "****" dfP$Spearman_p[dfP$Spearman_p < 0.001] <- "***" dfP$Spearman_p[dfP$Spearman_p < 0.01] <- "**" dfP$Spearman_p[df

我有一个
data.frame
dfP
,其中一列
Spearman\u p
包含p值(数字数据)。我想用它们代替p值汇总星。我使用以下代码:

 dfP$Spearman_p[dfP$Spearman_p < 0.0001] <- "****"
 dfP$Spearman_p[dfP$Spearman_p < 0.001] <- "***"
 dfP$Spearman_p[dfP$Spearman_p < 0.01] <- "**"
 dfP$Spearman_p[dfP$Spearman_p < 0.05] <- "*"
 dfP$Spearman_p[dfP$Spearman_p > 0.05] <- "ns"

dfP$Spearman\u p[dfP$Spearman\u p<0.0001]一旦进行第一次替换,就将向量
dfP$Spearman\u p
转换为字符向量。在比较字符向量时,
“***”
(或任意数量的星星)小于0.05,因此表达式的计算结果为
TRUE
“***”
替换为
“*”

另见:

我建议创建一个新列,即
Spearman\u p\u sign

dfP$Spearman_p_sign <- "ns"
dfP$Spearman_p_sign[dfP$Spearman_p < 0.0001] <- "****"
dfP$Spearman_p_sign[dfP$Spearman_p < 0.001] <- "***"
dfP$Spearman_p_sign[dfP$Spearman_p < 0.01] <- "**"
dfP$Spearman_p_sign[dfP$Spearman_p < 0.05] <- "*"

dfP$Spearman\u符号一旦进行第一次替换,就将向量
dfP$Spearman\u p
转换为字符向量。在比较字符向量时,
“***”
(或任意数量的星星)小于0.05,因此表达式的计算结果为
TRUE
“***”
替换为
“*”

另见:

我建议创建一个新列,即
Spearman\u p\u sign

dfP$Spearman_p_sign <- "ns"
dfP$Spearman_p_sign[dfP$Spearman_p < 0.0001] <- "****"
dfP$Spearman_p_sign[dfP$Spearman_p < 0.001] <- "***"
dfP$Spearman_p_sign[dfP$Spearman_p < 0.01] <- "**"
dfP$Spearman_p_sign[dfP$Spearman_p < 0.05] <- "*"

dfP$Spearman\u p\u sign尝试使用以下函数。它一次就改变了整个向量

makeStars <- function(x){
  stars <- c("****", "***", "**", "*", "ns")
  vec <- c(0, 0.0001, 0.001, 0.01, 0.05, 1)
  i <- findInterval(x, vec)
  stars[i]
}

dfP$Spearman_p <- makeStars(dfP$Spearman_p)

makeStars尝试使用以下函数。它一次就改变了整个向量

makeStars <- function(x){
  stars <- c("****", "***", "**", "*", "ns")
  vec <- c(0, 0.0001, 0.001, 0.01, 0.05, 1)
  i <- findInterval(x, vec)
  stars[i]
}

dfP$Spearman_p <- makeStars(dfP$Spearman_p)

makeStars我发现这个问题非常有趣,因为它看起来
Ops
组在与
一起使用时有一个独特的行为。我发现这个问题非常有趣,因为它看起来
Ops
组在与
一起使用时有一个独特的行为。另一种选择:使用
stars.pval()
来自
gtools
软件包


.

另一种选择:使用
gtools
包中的
stars.pval()


.

感谢您的详细解释和解决方案。2倍真棒!感谢您的详细解释和解决方案。2倍真棒!有趣!感谢您的见解和解决方案。很有趣!感谢您的见解和解决方案。@Sylvia Rodriguez为愚蠢的错误感到抱歉,将编辑。完成了,太棒了!很棒的解决方案。谢谢你的编辑。@SylviaRodriguez为这个愚蠢的错误感到抱歉,威尔编辑。完成了,太棒了!很棒的解决方案。也谢谢你的编辑。
> "*" < 1
[1] TRUE
> "*" < 10
[1] TRUE
> "*" < 100
[1] TRUE
> "*" < 0.1
[1] TRUE
> "+" < 0.1
[1] TRUE
> "+" < 100
[1] TRUE
test <- data.frame(col1 = c("A", "B", "C"), 
                   col2 = c(0.04, 0.009, 0.0009), stringsAsFactors = FALSE)

test$new[test$col2 < 0.05] <- "a"
test$new[test$col2 < 0.01] <- "aa"
test$new[test$col2 < 0.001] <- "aaa"

test$new2 <- gsub("a", "*", test$new)

  col1   col2 new new2
1    A 0.0400   a    *
2    B 0.0090  aa   **
3    C 0.0009 aaa  ***