Search code examples
rdataframesubstitutionp-value

Substitute p values for stars in data frame in R


I have a data.frame dfP with a column Spearman_p that contains p values (numeric data). I would like to substitute them for p value summary stars. I use this code:

 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"

However, this changes all p values <0.05 (so also those <0.01) to * (one star).

I suspect that R considers **** as a numeric <0.05 in subsequent steps. Is that correct? If so, how can I circumvent this, please?

Thank you.


Solution

  • Try using the following function. It changes the entire vector in one go.

    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)
    

    But maybe it's better if you create a new vector.

    dfP$Spearman_p_stars <- makeStars(dfP$Spearman_p)