且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在列表的列中查找特定值

更新时间:2021-08-27 21:36:40

我们可以遍历列表,检查是否有任何$ 齿轮列中的c $ c>元素等于4,使用它来对列表中的名称进行子集

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"






或使用%in%创建逻辑索引

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