我有以下数据框 [数据框] [1]
现在我想要做的是根据向量p中的值生成带有显着性代码的附加向量(每个估计的p值)。有没有一种方法R可以根据另一个向量的信息生成一个充满星星的矢量(作为重要性)? 而且:有没有一种方法可以告诉R,它应该根据新的观察顺序重新组织数据帧(我希望有以下顺序:vol_s,vol_s_avg,vol_s_med,vol_s_end,vol_l等)?
df的结构
structure(list(id = c("vol_avg_cer", "vol_avg_cer", "vol_avg_cer","vol_avg_cer", "vol_cer", "vol_cer"), type = c("partial", "partial",
"full", "full", "partial", "partial"), parm = c("vol_s_avg",
"vol_l_avg", "vol_s_avg", "vol_l_avg", "vol_s", "vol_l"), estimate = c(-0.00419972506246416,
-0.0199988264598171, -0.0429143892387528, 0.0367191277063419,
-0.0180348542378266, -0.0825424096818213), stderr = c(0.00729095969265321,
0.00950796168366169, 0.0296902477909246, 0.052772355386909, 0.0280972492739437,
0.0458807583546288), p = c(0.564602918461653, 0.0354328407781613,
0.148344569863659, 0.486552631437604, 0.520955910904793, 0.0720085952786877
)), .Names = c("id", "type", "parm", "estimate", "stderr", "p"
), row.names = c(1L, 2L, 20L, 21L, 1825L, 1826L), class = "data.frame")
答案 0 :(得分:0)
您可以使用symnum
将数字转换为符号:
library(dplyr)
mutate(df, signif = symnum(
p, cutpoints = c(0, 0.01, 0.05, 0.10, 0.5, 1),
symbols = c("***", "**", "*", ".", " ")))
答案 1 :(得分:0)
在@ user2802241使用dplyr
和symnum
的答案的基础上,为了对parm
列进行排序,您可以将列的顺序定义为单独的向量,然后设置parm
列作为使用向量作为其级别的因素,并使用arrange
。
e.g。
library(dplyr)
## define a vector with the variables in the order you require
factor_levels <- c("vol_s", "vol_s_avg", "vol_s_med","vol_s_end", "vol_l", "vol_l_avg", "vol_l_med", "vol_l_end")
## stay within dplyr - convert 'parm' to a factor and arrange on it
df <- df %>%
mutate(signif = symnum(p,
cutpoints = c(0, 0.01, 0.05, 0.10, 0.5, 1),
symbols = c("***", "**", "*", ".", " ")),
parm = factor(parm, levels = factor_levels)) %>%
arrange(parm)
> df
id type parm estimate stderr p signif
1 vol_cer partial vol_s -0.018034854 0.028097249 0.52095591
2 vol_avg_cer partial vol_s_avg -0.004199725 0.007290960 0.56460292
3 vol_avg_cer full vol_s_avg -0.042914389 0.029690248 0.14834457 .
4 vol_cer partial vol_l -0.082542410 0.045880758 0.07200860 *
5 vol_avg_cer partial vol_l_avg -0.019998826 0.009507962 0.03543284 **
6 vol_avg_cer full vol_l_avg 0.036719128 0.052772355 0.48655263 .
如果您希望将parm列保留为character
,则可以将其转换回来
df <- df %>%
mutate(signif = symnum(p,
cutpoints = c(0, 0.01, 0.05, 0.10, 0.5, 1),
symbols = c("***", "**", "*", ".", " ")),
parm = factor(parm, levels = factor_levels)) %>%
arrange(parm) %>%
mutate(parm = as.character(parm))