Home > Back-end >  Filtering a data frame by column names
Filtering a data frame by column names

Time:09-06

i have two dataframes with another sizes but partly same row and column names. I want to filter tpm_datExpr by finding the common column and row names with datTraits.

So I want my tpm_datExpr dataframe to have 278 columns.

> colnames(tpm_datExpr)[1:10]
 [1] "D5247_S53_L006"    "D5248_S54_L006"    "D5249_S67_L008"    "E02874_L1_S1_L001"
 [5] "E02875_L1_S2_L001" "E02876_L1_S3_L001" "E02877_L1_S4_L001" "E02878_L1_S5_L001"
 [9] "E02879_L1_S6_L001" "E02880_L1_S7_L001"
> rownames(datTraits)[1:10]
 [1] "D5247_S53_L006"    "D5248_S54_L006"    "D5249_S67_L008"    "E02874_L1_S1_L001"
 [5] "E02875_L1_S2_L001" "E02876_L1_S3_L001" "E02877_L1_S4_L001" "E02878_L1_S5_L001"
 [9] "E02879_L1_S6_L001" "E02880_L1_S7_L001"
> ncol(tpm_datExpr)
[1] 623
> nrow(datTraits)
[1] 278

CodePudding user response:

You may use intersect on rownames and names. Example:

df1[intersect(rownames(df1), rownames(df2)), 
            intersect(names(df1), names(df2))]
#   X1 X5 X6
# 2  0  0  0
# 4  0  0  0
# 8  0  0  0
# 9  0  0  0

Data:

df1 <- structure(list(X1 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), X2 = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0), X3 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 
0), X4 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), X5 = c(0, 0, 0, 0, 
0, 0, 0, 0, 0, 0), X6 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), class = "data.frame", row.names = c(NA, 
-10L))

df2 <- structure(list(X1 = c(0, 0, 0, 0), X5 = c(0, 0, 0, 0), X6 = c(0, 
0, 0, 0)), row.names = c(2L, 4L, 8L, 9L), class = "data.frame")
  • Related