2 回答
TA贡献1804条经验 获得超2个赞
通常,base R(不是 tidy R)更容易转换为 Python Pandas。您的 R 代码似乎在做的是按国家/地区列对数据框进行子集化,并将每个子集运行到psych::alpha()
. 然后将提取的统计数据返回到带有国家指标的数据框中。
您可以对基数 R 执行完全相同的操作,它可以在列表理解中by
转换为 Pandas 。groupby
看起来psych::alpha
返回的统计数据比pingouin.cronbach_alpha
. 根据需要在未经测试的代码中调整字段和返回值。
基础 R (使用文档)
# DEFINE METHOD
run_cronbach_alpha <- function(sub) {
results <- psych::alpha(sub)$total # RETURNS LIST
data.frame(country = sub$country[1],
raw_alpha = results$raw_alpha,
std.alpha = results$std.alpha,
G6 = results$G6,
average_r = results$average_r,
median_r = results$median,
mean = results$mean,
sd = results$sd)
}
# FILTER COLUMNS IN DATA FRAME
sim_short <- sim_data[c("step_bfi1_ab_cor", ..., "step_bfi39_ab_cor", "country")]
# RUN METHOD BY COUNTRY SUBSETS TO RETURN DF LIST
results_df_list <- by(sim_short, sim_short$country, run_cronbach_alpha)
# ROW BIND ALL DFs TO SINGLE FINAL DATA FRAME
results_df <- do.call(rbind.data.frame, results_df_list)
Python Pandas (使用文档)
# DEFINE METHOD
def run_cronbach_alpha(c, sub):
results = pg.cronbach_alpha(sub.drop(["country"], axis="columns")) # RETURNS TUPLE
return pd.DataFrame({'country': c, 'cronbach_alpha': results[0], index=[0]})
# FILTER COLUMNS IN DATA FRAME
sim_short = sim_data.reindex(["step_bfi1_ab_cor", ..., "step_bfi39_ab_cor", "country"],
axis='columns')
# RUN METHOD BY COUNTRY SUBSETS TO RETURN DF LIST
results_df_list = [run_cronbach_alpha(i, df) for i,df in sim_short.groupby("country")]
# CONCATENATE ALL DFs TO SINGLE FINAL DATA FRAME
results_df = pd.concat(results_df_list)
TA贡献1884条经验 获得超4个赞
def run_cronbach_alpha(c, sub):
results = pg.cronbach_alpha(sub.drop(["country"], axis="columns")) # RETURNS TUPLE
return pd.DataFrame({'country': c, 'cronbach_alpha': results[0]}, index = ["Result"])
# RUN METHOD BY COUNTRY SUBSETS TO RETURN DF LIST
results_df_list = [run_cronbach_alpha(i, df) for i,df in sim_data.groupby("country")]
results_df_list
添加回答
举报