简体   繁体   中英

Subset a data frame based on values of another column in data frame

It is possible to take one column of numeric values like in dup$Number and subset columns in DG that match dup$number and return this as a new data frame?

dup
Number  Letter
      59                        Q
      91                        Q
      19                        Q
      17                        Q

DG
chr     pos             id ref alt refc altc qual cov line_21 line_26 line_28 line_31 line_32 line_38 line_40 line_41 line_42 line_45 line_48 line_49 line_57 line_59 line_69 line_73 line_75 line_83
1  2R 7006506 2R_7006506_SNP   C   A  169   26  999  29       0       0       0       2       0       0       0       0       0       0       0       0       0       0       0       0       2       -

Try

indx <- grep('line', names(DG))
DG[indx[as.numeric(sub('.*_', '', names(DG)[indx])) %in% dup$Number]]
#  line_59
#1       0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM