【发布时间】:2021-06-17 05:34:37
【问题描述】:
我想知道如何使用 ifelse() 命令为数据框列表创建变量。
我创建了 2 个数据集,使用来自 ggplot2::diamonds 数据集的 300 个顶部和底部值,称为 diamonds_top300 和 diamonds_bottom300:
# Loads packages
# ---- NOTE: making plots and diamonds dataset
if(!require(ggplot2)){install.packages("ggplot2")}
# ---- NOTE: for data wrangling
if(!require(dplyr)){install.packages("dplyr")}
# dataset creation
# ---- NOTE: selects only the top 300 rows of the dataset
diamonds_top300 <- data.frame(dplyr::top_n(diamonds, 300, table))
# ---- NOTE: selects only the bottom 300 rows of the dataset
diamonds_bottom300 <- data.frame(dplyr::top_n(diamonds, -300, table))
然后我使用 lapply 和函数创建了一个包含 2 个模型的列表,仅在使用的数据集上有所不同:
# Loads packages
# ---- NOTE: run mixed effects models
if(!require(lme4)){install.packages("lme4")}
## lists datasets to use
DATASET_list <- c("diamonds_top300", "diamonds_bottom300")
## creates model
# ---- NOTE: creates list object
freq_mlm_poisson_model <-
lapply(DATASET_list,
function(data_list) wrapr::let(
c(data_list_model = data_list),
(lme4::glmer(
price ~ cut + color + carat + (1 | clarity) + (1 | depth),
data = data_list_model,
family = poisson()
)
)
)
)
# ---- NOTE: changes list object name
freq_mlm_poisson_model <-
setNames(freq_mlm_poisson_model, paste("freq_mlm_poisson_model",
DATASET_list,
sep = "__")
)
然后我使用 lapply 和函数以列表形式创建这些模型的摘要:
### creates summary model for list object freq_mlm_poisson_model
# ---- NOTE: creates list object
freq_mlm_poisson_summary <-
lapply(
freq_mlm_poisson_model,
function(model_list) {
summary(model_list)
}
)
# ---- NOTE: changes list object name
freq_mlm_poisson_summary <-
setNames(freq_mlm_poisson_summary, paste("freq_mlm_poisson_summary",
DATASET_list,
sep = "__")
)
然后我将摘要转换为仅包含固定效果信息的数据框:
### turns summary list fixed effects into list of data frames
# ---- NOTE: creates object with summary of fixed effects
freq_mlm_poisson_summary_fixedeffects <-
lapply(freq_mlm_poisson_summary, `[[`, 10)
# ---- NOTE: creates list object
freq_mlm_poisson_summary_fixedeffects_df <-
lapply(
freq_mlm_poisson_summary_fixedeffects,
function(model_list) {
data.frame(model_list)
}
)
# ---- NOTE: changes list object name
freq_mlm_poisson_summary_fixedeffects_df <-
setNames(freq_mlm_poisson_summary_fixedeffects_df, paste("freq_mlm_poisson_summary_fixedeffects_df",
DATASET_list,
sep = "__")
)
有没有办法使用 ifelse() 命令或其他命令在每个列表中创建名为 p_value_sign 的变量,如果变量 Pr(>|z|) 中的相应 p 值小于 0.05,它会告诉是/否(即,p
【问题讨论】:
-
你需要一个逻辑向量还是'yes','no'字符列?
-
我想拥有第二个包含此信息的变量,以便我可以同时拥有数字和其他变量。它可以是一个逻辑向量。第二个变量也可以给出符号级别(例如,“在 p
-
谢谢,你可以试试更新的解决方案