Find specific value in a column of a list

后端 未结 1 1268
Happy的楠姐
Happy的楠姐 2021-01-14 05:12

I\'m trying to get which objects of a list have an specific value in one of their columns.

In order to explain my case, please run the following simple example:

<
1条回答
  •  执念已碎
    2021-01-14 05:37

    We could loop through the list, check whether there is any element in 'gear' column that is equal to 4, use that to subset the names of the list

    names(mt_list)[sapply(mt_list, function(x) any(x$gear == 4))]
    #[1] "4" "6"
    

    Or use %in% to create the logical index

    names(mt_list)[sapply(mt_list, function(x) 4 %in% x$gear)]
    #[1] "4" "6"
    

    0 讨论(0)
提交回复
热议问题