2017-05-08 122 views
1

有什么方法可以像这样过滤数据框吗?我想保留所有行和列出现某个子字符串,以及下一列。如何根据行值选择列

df <- read.table(header=TRUE, stringsAsFactors = FALSE, text = 
         "date col2 col3 col4 col5 col6 
        1 boston 22 new_york 15  atlanta 5 
        2 boston 21 new_york 15  atlanta 0 

        ") 

cities <- c('boston', 'atlanta') 
#filter by cities 
#output 
#col2 col3 col5 col6 
#boston 22 atlanta 5 
#boston 21 atlanta 0 

回答

0

通过数据集的交替列我们循环,检查所有的元素是否%in%城市创建逻辑矢量(“I1”),则根据索引,得到真实的列的列索引和下一列

i1 <- sapply(df[c(TRUE, FALSE)], function(x) all(x %in% cities)) 
df[sort(which(names(df) %in% names(i1)[i1]) + rep(0:1, each = 2))] 

或者另一种选择是通过交替的列索引的序列循环,子集的数据集,检查if所有元素都被在“城市”找到,则子集的列和下一个列(df[i:(i+1)]),Fiter列出的list元素为NULL并且cbind其余元素

do.call(cbind, Filter(Negate(is.null), lapply(seq(1, ncol(df), by = 2), 
       function(i) if(all(df[,i] %in% cities)) df[i:(i+1)])))