对于所有行(符号),我想将五列F1_G-F5_G与五列F1_P-F5_P相关联。这应该为每个符号给出一个相关值。
Symbol F1_G F2_G F3_G F4_G F5_G F1_P F2_P F3_P F4_P F5_P
1 abca2 0.7696639 1.301428 0.8447565 0.6936672 0.6987410 9.873610 9.705205 8.044027 8.311364 9.961380
2 aco2 7.4274715 7.234892 7.8543164 8.0142983 8.1512194 9.620114 9.767721 7.607115 7.854960 9.472660
3 adat3 -2.0560126 -1.536868 -0.4181457 -1.1946602 -0.7707472 8.975871 8.645235 7.926262 7.432755 8.633583
4 adat3 -2.0560126 -1.536868 -0.4181457 -1.1946602 -0.7707472 9.620114 9.237699 7.162386 7.972086 8.872763
5 adnp 1.4228436 0.932214 0.8964153 0.8125162 0.9921002 9.177645 9.323443 8.507508 8.080413 8.633583
6 arhgap8 -2.6517712 -2.067164 -1.4918958 -2.6517712 -1.5474257 9.395681 8.861322 8.333381 8.038053 8.872763
我尝试了类似的方法,但是并没有考虑每一行:
res <- outer(df[, c(2,3,4,5,6)], df[, c(7,8,9,10,11)], function(X, Y){
mapply(function(...) cor.test(..., na.action = "na.exclude")$estimate,
X, Y)
})
退出:
Symbol Cor
abca2 0.14
aco2 0.12
答案 0 :(得分:1)
由于您要按行进行操作,因此我们可以将apply
与MARGIN = 1
一起使用
#Get column indices ending with G
g_cols <- grep("G$", names(df))
#Get column indices ending with P
p_cols <- grep("P$", names(df))
apply(df, 1, function(x) cor.test(as.numeric(x[g_cols]),
as.numeric(x[p_cols]), na.action = "na.exclude")$estimate)
# 1 2 3 4 5 6
# 0.21890 -0.52925 -0.52776 -0.82073 0.60473 -0.11785
一种tidyverse
方法是
library(tidyverse)
df %>%
mutate(row = row_number()) %>%
select(-Symbol) %>%
gather(key, value, -row) %>%
group_by(row) %>%
summarise(ans = cor.test(value[key %in% g_cols], value[key %in% p_cols],
na.action = "na.exclude")$estimate)
# row ans
# <int> <dbl>
#1 1 0.219
#2 2 -0.529
#3 3 -0.528
#4 4 -0.821
#5 5 0.605
#6 6 -0.118