如何根据行值选择列

时间:2017-05-08 18:12:20

标签: r dataframe

有没有办法过滤像这样的data.frame?我想保留所有行和列的位置,以及下一列。

 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

1 个答案:

答案 0 :(得分:0)

我们循环遍历数据集的备用列,检查是否所有元素都是%in%个城市来创建逻辑向量(&#39; i1&#39;),然后根据索引获取列索引TRUE列和下一列

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在&#39; cities&#39;中找到所有元素,然后对列和下一列进行子集化(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)])))
相关问题