R语言 如何选择跨列有多个值的行

yeotifhr  于 2023-06-03  发布在  其他
关注(0)|答案(1)|浏览(192)

我有一个dataframe:

dput(gene1[1:5,1:5])
structure(list(en_Adipose_Subcutaneous.db = c(0.0531016390078734, 
-0.00413407782001034, -0.035434632568444, 0.00968736935965742, 
0.0523714252287003), en_Adipose_Visceral_Omentum.db = c(0, 0, 
0, 0, 0), en_Adrenal_Gland.db = c(0, 0, 0, 0, 0), en_Artery_Aorta.db = c(0, 
0, 0, 0, 0), en_Artery_Coronary.db = c(0, 0, 0, 0, 0)), row.names = c("rs1041770", 
"rs12628452", "rs915675", "rs11089130", "rs36061596"), class = "data.frame")

我想只选择那些至少在2列以上有值的行。并删除仅在一列中有值的那些行。我写了这段代码:

one_tissueonly <- NULL
for(i in 1:552){
y <- which(gene1[i,]!=0)  ## >1 means more than one col 
if(length(y)>1){  ##select only for one col:
  value <- gene1[i,]
}
one_tissueonly <- rbind(one_tissueonly,value)
}

但它会生成一些相同的行:对于第一个值,使用rbind函数:

dput(one_tissueonly[1:5,1:5])
structure(list(en_Adipose_Subcutaneous.db = c(0.0531016390078734, 
0.0531016390078734, 0.0531016390078734, 0.00968736935965742, 
0.0523714252287003), en_Adipose_Visceral_Omentum.db = c(0, 0, 
0, 0, 0), en_Adrenal_Gland.db = c(0, 0, 0, 0, 0), en_Artery_Aorta.db = c(0, 
0, 0, 0, 0), en_Artery_Coronary.db = c(0, 0, 0, 0, 0)), row.names = c("rs1041770", 
"rs10417701", "rs10417702", "rs11089130", "rs36061596"), class = "data.frame")

输出文件如下所示:

有没有人知道如何解决这个问题。谢谢你。

qeeaahzv

qeeaahzv1#

遵循Gregor托马斯的建议(由于您希望两个或多个组织显示相同的标记物,因此进行了修改)

# using bracets:
gene <- gene[rowSums(gene != 0) > 1, ]
# using subset()
gene <- subset(gene,rowSums(gene != 0) > 1)

相关问题