我有一张表calles" pvalue"共61列:
我尝试在每列上进行for循环(选择Pvalue< 0.01) 但我没有设法在矢量中记录IdEch。我只有列的编号......这是无用的......我无法找到解决方案....
这是我的剧本:
res = data.frame()
list_pvalue = c()
for (i in 2:ncol(pvalue)) {
res = subset( pvalue , pvalue[,i] < 0.01 )
list_pvalue = c(res$IdEch,list_pvalue)
}
list_pvalue = unique(list_pvalue)
答案 0 :(得分:2)
你几乎就在那里。您希望为每行找到任何 pvalues为< 0.01
的行。
要为每一行执行某些操作,请使用apply
。要查看any
值是否为真,请使用any
(?apply
,?any
)
# pvalue[, -1] selects all but the first column
# pvalue[, -1] < 0.01 is a logical dataframe of TRUE/FALSE
# apply(X, 1, FUN) applys FUN to each row of X
# apply(pvalue[, -1] < 0.01, 1, any) is TRUE for each row that has a pvalue < 0.01
pvalue[apply(pvalue[, -1] < 0.01, 1, any), ] # is what you are after
答案 1 :(得分:2)
尝试以下示例
#dummy data
set.seed(123)
pvalue <- data.frame(IdEch=paste0("ID",1:10),
Pvalue1=runif(10,0,0.05),
Pvalue2=runif(10,0,0.05),
Pvalue3=runif(10,0,0.05),
Pvalue4=runif(10,0,0.05),stringsAsFactors = FALSE)
pvalue
# IdEch Pvalue1 Pvalue2 Pvalue3 Pvalue4
# 1 ID1 0.014378876 0.047841667 0.044476966 0.048151212
# 2 ID2 0.039415257 0.022666708 0.034640170 0.045114952
# 3 ID3 0.020448846 0.033878532 0.032025341 0.034535264
# 4 ID4 0.044150870 0.028631670 0.049713489 0.039773371
# 5 ID5 0.047023364 0.005146234 0.032785290 0.001230684
# 6 ID6 0.002277825 0.044991249 0.035426523 0.023889799
# 7 ID7 0.026405274 0.012304387 0.027203301 0.037922977
# 8 ID8 0.044620952 0.002102977 0.029707101 0.010820397
# 9 ID9 0.027571751 0.016396036 0.014457987 0.015909050
# 10 ID10 0.022830737 0.047725182 0.007355682 0.011581289
pvalue[ rowSums(pvalue[,-1] < 0.01) > 1, "IdEch" ]
# [1] "ID5"
答案 2 :(得分:2)
pvalue[-1]
给出没有IdEch列的数据框。将pmin
应用于此会为您提供每行中的最小值。如果某个值小于0.01,则最小值为。
do.call("pmin", pvalue[-1]) < 0.01
然后为您提供具有较小值的rowindices。那么
pvalue[do.call("pmin", pvalue[-1]) < 0.01]
应该是你想要的。