首页 > 解决方案 > 如果在 data.frame 中

问题描述

我想在其他列中的值之后的同一行中的两列之间选择一个值。

我的功能是:如果 shapiro1、shapiro2 和 F_test 中的值小于 0.05,则选择 t_test 中的值,否则选择 wilcox 的值。您是否可以制作这样的功能并应用于更大的列?

structure(list(modalities = structure(1:3, .Label = c("BS1", 
"HW1", "PG"), class = "factor"), shapiro1 = c(0.0130672654432492, 
0.305460485386201, 0.148320635833262), shapiro2 = c(0.920315823302857, 
0.1354174735521, 0.148320635833262), F_test = c(0.20353475323665, 
0.00172897172228584, 1), t_test = c(2.88264982135322e-06, 5.75374264225996e-05, 
NaN), wilcox = c(0.00909069801592506, 0.00902991076269246, NaN
)), class = "data.frame", row.names = c(NA, -3L))

标签: rif-statement

解决方案


您可以选择列,应用rowSums并检查该行中的任何值是否小于 0.05,然后相应地选择t_testwilcox值。

cols <- c("shapiro1", "shapiro2", "F_test")
ifelse(rowSums(df[cols] < 0.05) > 0, df$t_test, df$wilcox)
#[1] 2.882650e-06 5.753743e-05          NaN

推荐阅读