我有一个列中包含特征的数据框架,并且我有每个列的逻辑向量,无论它们是否满足某个条件。= TRUE, = FALSE
我只想保留满足条件的列,并且在逻辑向量中有一个TRUE
。
这是向量:
c(Adipocytes = TRUE, B.cells = TRUE, Basophils = FALSE, CD4..memory.T.cells = TRUE,
CD4..naive.T.cells = TRUE, CD4..T.cells = FALSE, CD4..Tcm = TRUE,
CD4..Tem = TRUE, CD8..naive.T.cells = FALSE, CD8..T.cells = TRUE,
CD8..Tcm = TRUE, Class.switched.memory.B.cells = TRUE, DC = TRUE,
Endothelial.cells = FALSE, Eosinophils = TRUE, Epithelial.cells = FALSE,
Fibroblasts = TRUE, Hepatocytes = TRUE, ly.Endothelial.cells = TRUE,
Macrophages = FALSE, Macrophages.M1 = TRUE, Macrophages.M2 = TRUE,
Mast.cells = TRUE, Melanocytes = TRUE, Memory.B.cells = TRUE,
Monocytes = FALSE, mv.Endothelial.cells = TRUE, naive.B.cells = TRUE,
Neutrophils = TRUE, NK.cells = TRUE, pDC = TRUE, Pericytes = TRUE,
Plasma.cells = TRUE, pro.B.cells = TRUE, Tgd.cells = TRUE, Th1.cells = FALSE,
Th2.cells = TRUE, Tregs = FALSE)
和数据框架:
structure(list(response = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L), .Label = c("NoResponse", "Response"), class = "factor"),
Adipocytes = c(0.0147750026478847, 0.0147750026478847, 0.0147750026478847,
0.0212089093123119, 0.0710538305419062, 0.0147750026478847,
0.0147750026478847), B.cells = c(0.0771628600461939, 0.0348430059727367,
0.166843196901955, 0.029029065282152, 0.0289092657550142,
0.0091727181061411, 0.0545426703298063), Basophils = c(0.123412405513611,
0.229980789620818, 0.710327302827785, 0.110317426229336,
0.593651555059932, 0.171672288243263, 0.16957000805118),
CD4..memory.T.cells = c(0.053430101401256, 0.00519336879475164,
0.00519336879475164, 0.0135894934563294, 0.00519336879475164,
0.00519336879475164, 0.0187854542183181), CD4..naive.T.cells = c(0.0198799983437911,
0.0198799983437911, 0.0198799983437911, 0.0198799983437911,
0.0198799983437911, 0.0198799983437911, 0.0198799983437911
), CD4..T.cells = c(0.0308423390003806, -0.000280618433405845,
-0.000280618433405845, -0.000280618433405845, -0.000280618433405845,
-0.000280618433405845, 0.0119164432522563)), row.names = c("Pt1",
"Pt10", "Pt103", "Pt106", "Pt11", "Pt17", "Pt2"), class = "data.frame")
您可以首先将希望保存的列的名称作为子集,然后使用这些名称作为原始数据框架的列的子集。在下面几行中,cols是你的矢量,df是数据框。
df[,names(cols[cols==T])]
试试这个
df[intersect(names(vec == T) , colnames(df))]
其中df
是数据帧,vec
是矢量。