遍历数据框列表,并获取R中每个数据框的子集 [英] Iterate over a list of dataframes and get a subset of each dataframe in R

查看:239
本文介绍了遍历数据框列表,并获取R中每个数据框的子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据帧列表(mylist),我想遍历它们,并从每个数据帧中获取在特定列中没有某些值(上游"或下游")的所有行!我会想要将数据帧的子集(其余值)返回到列表! 目前我正在这样做:

I have a list of dataframes (mylist) and i want to iterate over them and get all the rows from each dataframe that in a specific column don't have some values("upstream" or "downstream")!I would like to return the subsets (for the rest of the values) of dataframes back to the list! For the moment i am doing that:

mylist<- lapply(mylist, function(df){
  if (df$column != "upstream" | "downstream"){
     mylist <- df
  }
})

哪个出现以下错误:

Error in df$insideFeature : object of type 'symbol' is not subsettable

在此先感谢您提供的帮助!

Thanks you in advance for any help provided!

推荐答案

正如PeterDee所指出的:您错误地使用了|.使用%in%进行以下操作

As PeterDee also noted: you are using | incorrectly. Use %in% to do this as follows

myfun <- function(df, words) {
  good_col <- which(! df$column %in% words)
  df[good_col,]
}

然后

lapply(mylist, myfun, c("upstream", "downstream"))

这篇关于遍历数据框列表,并获取R中每个数据框的子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆