如何运行 cor.test 只在满足条件的情况下?
How to run cor.test only when conditions are met?
我目前正在尝试确定 21 列的两个数据帧之间的相关性。我希望 1 个数据框中的列仅针对另一个数据框中各自的列进行测试。我试过使用 if 语句。但是,每列的长度都大于 1,所以它不起作用。另外,我试过使用 case_when 但无法正常工作。大家有什么建议吗?我是新手,希望得到任何反馈。
for(i in 4:24){
for(x in 4:24){
if(colnames(`Voss.TG5 1`)== colnames(`Voss.TG5 1`)) { cor.test(`Voss.TG5 1`[,i],`Voss.TG6 1`[,x]) }
print(paste(colnames(`Voss.TG5 1`)[i], "est:",a$estimate,"p=value",a$p.value))
}
}
错误信息
the condition has length >1 and only the first element will be used
假设您要比较第一个 data.frame 的第 4 列和第二个 data.frame 的第 4 列,以及比较 5 和 5,等等,您只需要一个值来循环。
示例:
set.seed(123)
df1 <- data.frame(matrix(rnorm(24e3), nrow=100, ncol=24),
row.names = paste0("id_", 1:100))
colnames(df1) <- paste0("col_", 1:24)
set.seed(456)
df2 <- data.frame(matrix(rnorm(24e3), nrow=100, ncol=24),
row.names = paste0("id_", 1:100))
colnames(df2) <- paste0("col_", 1:24)
getCorResult <- function(x) {
ct <- cor.test(df1[, x], df2[, x])
paste0(colnames(df1)[x], " est: ", ct$estimate,"; p-value: ", ct$p.value)
}
for (i in 4:24) print(getCorResult(i))
#> [1] "col_4 est: 0.127978798171234; p-value: 0.204468537818308"
#> [1] "col_5 est: -0.130168697527086; p-value: 0.19676568528598"
#> [1] "col_6 est: -0.00401742175033025; p-value: 0.968356791558258"
#> [1] "col_7 est: -0.13911136442779; p-value: 0.167480254977386"
#> [1] "col_8 est: -0.0802509291976723; p-value: 0.427369490913814"
#> [1] "col_9 est: 0.0336457448519071; p-value: 0.739646539780128"
#> [1] "col_10 est: 0.101054217409236; p-value: 0.317116556319783"
#> [1] "col_11 est: -0.16499181273189; p-value: 0.100914127184667"
#> [1] "col_12 est: 0.0883842999330604; p-value: 0.381876567737707"
#> [1] "col_13 est: 0.133978968109195; p-value: 0.183865978193586"
#> [1] "col_14 est: -0.146591444752327; p-value: 0.145569923059057"
#> [1] "col_15 est: -0.0994068480087866; p-value: 0.325110357585432"
#> [1] "col_16 est: 0.0510291962770492; p-value: 0.614117314454403"
#> [1] "col_17 est: -0.221875409962651; p-value: 0.0265146668160414"
#> [1] "col_18 est: 0.0272800890564412; p-value: 0.787605537795977"
#> [1] "col_19 est: 0.0480880241589448; p-value: 0.63470899715546"
#> [1] "col_20 est: 0.0178734013379405; p-value: 0.859900388976822"
#> [1] "col_21 est: 0.0419569274347847; p-value: 0.678524920388683"
#> [1] "col_22 est: -0.0790894327800203; p-value: 0.43411046999303"
#> [1] "col_23 est: -0.0222165574940321; p-value: 0.826338882900991"
#> [1] "col_24 est: -0.184979303595513; p-value: 0.0654062507984986"
由 reprex package (v0.3.0)
于 2020 年 6 月 20 日创建
我目前正在尝试确定 21 列的两个数据帧之间的相关性。我希望 1 个数据框中的列仅针对另一个数据框中各自的列进行测试。我试过使用 if 语句。但是,每列的长度都大于 1,所以它不起作用。另外,我试过使用 case_when 但无法正常工作。大家有什么建议吗?我是新手,希望得到任何反馈。
for(i in 4:24){
for(x in 4:24){
if(colnames(`Voss.TG5 1`)== colnames(`Voss.TG5 1`)) { cor.test(`Voss.TG5 1`[,i],`Voss.TG6 1`[,x]) }
print(paste(colnames(`Voss.TG5 1`)[i], "est:",a$estimate,"p=value",a$p.value))
}
}
错误信息
the condition has length >1 and only the first element will be used
假设您要比较第一个 data.frame 的第 4 列和第二个 data.frame 的第 4 列,以及比较 5 和 5,等等,您只需要一个值来循环。
示例:
set.seed(123)
df1 <- data.frame(matrix(rnorm(24e3), nrow=100, ncol=24),
row.names = paste0("id_", 1:100))
colnames(df1) <- paste0("col_", 1:24)
set.seed(456)
df2 <- data.frame(matrix(rnorm(24e3), nrow=100, ncol=24),
row.names = paste0("id_", 1:100))
colnames(df2) <- paste0("col_", 1:24)
getCorResult <- function(x) {
ct <- cor.test(df1[, x], df2[, x])
paste0(colnames(df1)[x], " est: ", ct$estimate,"; p-value: ", ct$p.value)
}
for (i in 4:24) print(getCorResult(i))
#> [1] "col_4 est: 0.127978798171234; p-value: 0.204468537818308"
#> [1] "col_5 est: -0.130168697527086; p-value: 0.19676568528598"
#> [1] "col_6 est: -0.00401742175033025; p-value: 0.968356791558258"
#> [1] "col_7 est: -0.13911136442779; p-value: 0.167480254977386"
#> [1] "col_8 est: -0.0802509291976723; p-value: 0.427369490913814"
#> [1] "col_9 est: 0.0336457448519071; p-value: 0.739646539780128"
#> [1] "col_10 est: 0.101054217409236; p-value: 0.317116556319783"
#> [1] "col_11 est: -0.16499181273189; p-value: 0.100914127184667"
#> [1] "col_12 est: 0.0883842999330604; p-value: 0.381876567737707"
#> [1] "col_13 est: 0.133978968109195; p-value: 0.183865978193586"
#> [1] "col_14 est: -0.146591444752327; p-value: 0.145569923059057"
#> [1] "col_15 est: -0.0994068480087866; p-value: 0.325110357585432"
#> [1] "col_16 est: 0.0510291962770492; p-value: 0.614117314454403"
#> [1] "col_17 est: -0.221875409962651; p-value: 0.0265146668160414"
#> [1] "col_18 est: 0.0272800890564412; p-value: 0.787605537795977"
#> [1] "col_19 est: 0.0480880241589448; p-value: 0.63470899715546"
#> [1] "col_20 est: 0.0178734013379405; p-value: 0.859900388976822"
#> [1] "col_21 est: 0.0419569274347847; p-value: 0.678524920388683"
#> [1] "col_22 est: -0.0790894327800203; p-value: 0.43411046999303"
#> [1] "col_23 est: -0.0222165574940321; p-value: 0.826338882900991"
#> [1] "col_24 est: -0.184979303595513; p-value: 0.0654062507984986"
由 reprex package (v0.3.0)
于 2020 年 6 月 20 日创建