简体   繁体   中英

get unique entries from a complex list in r

I have a list of lists:

$markers                
        mi  chr phys.pos    group
    19  mG001   8   4803507 1
    20  mG002   8   4841939 1
    3   mG003   8   5268581 2
    1   mG004   8   5278404 2
    6   mG005   8   8352013 3
    7   mG006   8   8352443 3
    9   mG008   8   8354875 3
    10  mG009   8   8356039 4
    11  mG010   8   8357610 4
    13  mG012   8   8361049 5
    14  mG013   8   8361845 5
    15  mG014   8   1640281 6
    16  mG015   8   1640663 7

    $plants             
        plant   group       
    9   DL750   1       
    11  D827    1       
    17  DL26    1       
    25  D901    2       
    30  DR202   3       
    35  D356    3       
    52  F015    4

I would like to get a list of entries that are unique in "group", and a list of leftovers. the results are expected as:

(a) unique list:

$markers                
        mi  chr phys.pos    group
    19  mG001   8   4803507 1
    3   mG003   8   5268581 2
    6   mG005   8   8352013 3
    10  mG009   8   8356039 4
    13  mG012   8   8361049 5
    15  mG014   8   1640281 6
    16  mG015   8   1640663 7

    $plants             
        plant   group       
    9   DL750   1       
    25  D901    2       
    30  DR202   3       
    52  F015    4

(b) leftover list:

$markers                
        mi  chr phys.pos    group
    20  mG002   8   4841939 1
    1   mG004   8   5278404 2
    7   mG006   8   8352443 3
    9   mG008   8   8354875 3
    11  mG010   8   8357610 4
    14  mG013   8   8361845 5

    $plants             
        plant   group       
    11  D827    1       
    17  DL26    1       
    35  D356    3

Appreciate any helps.

If myList is your list, then simply

unique <- lapply(myList, function(x) x[!duplicated(x$group), ])
unique
# $markers
#       mi chr phys.pos group
# 19 mG001   8  4803507     1
# 3  mG003   8  5268581     2
# 6  mG005   8  8352013     3
# 10 mG009   8  8356039     4
# 13 mG012   8  8361049     5
# 15 mG014   8  1640281     6
# 16 mG015   8  1640663     7
# 
# $plants
#    plant group
# 9  DL750     1
# 25  D901     2
# 30 DR202     3
# 52  F015     4

leftovers <- lapply(myList, function(x) x[duplicated(x$group), ])
leftovers 
# $markers
#       mi chr phys.pos group
# 20 mG002   8  4841939     1
# 1  mG004   8  5278404     2
# 7  mG006   8  8352443     3
# 9  mG008   8  8354875     3
# 11 mG010   8  8357610     4
# 14 mG013   8  8361845     5
# 
# $plants
#    plant group
# 11  D827     1
# 17  DL26     1
# 35  D356     3

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