根据数据框中的向量生成值
Generate Values According to Vector in Data Frame
我有以下数据框
[数据框][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")
您可以使用 symnum
:
将数字转换为符号
library(dplyr)
mutate(df, signif = symnum(
p, cutpoints = c(0, 0.01, 0.05, 0.10, 0.5, 1),
symbols = c("***", "**", "*", ".", " ")))
基于@user2802241 使用 dplyr
和 symnum
的回答,要对 parm
列进行排序,您可以将列的顺序定义为单独的向量,然后将 parm
列设置为一个因子,使用向量作为其级别,并在其上设置 arrange
。
例如
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))
我有以下数据框 [数据框][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")
您可以使用 symnum
:
library(dplyr)
mutate(df, signif = symnum(
p, cutpoints = c(0, 0.01, 0.05, 0.10, 0.5, 1),
symbols = c("***", "**", "*", ".", " ")))
基于@user2802241 使用 dplyr
和 symnum
的回答,要对 parm
列进行排序,您可以将列的顺序定义为单独的向量,然后将 parm
列设置为一个因子,使用向量作为其级别,并在其上设置 arrange
。
例如
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))