简体   繁体   English

从 for 循环中的数据框中删除行

[英]deleting rows from data frames within for loop

back with another one和另一个一起回来

i have several dataframes and am trying to remove (i) rows with specific strings in one column and (ii) rows with na's in that same column.我有几个数据框,正在尝试删除 (i) 一列中带有特定字符串的行和 (ii) 同一列中带有 na 的行。 I've put together code of the form below我把下面表格的代码放在一起

   for (i in c(df1, df2, df3)){
      i <- i[!grepl("badString", i["Column"]),]
      i <- i[!is.na(i["Column"], ]
    }

But I keep getting this error但我一直收到这个错误

Error in i[!grepl("badString", i["Column"]), ] : 
  incorrect number of dimensions

I previously tried to specify the column using i$Column , but also got this error message我以前尝试使用i$Column指定列,但也收到此错误消息

Error: $ operator is invalid for atomic vectors

I've also tried using double columns (replacing i["Column"] with i[["Column"]] ), but no dice with this strategy as well我也尝试过使用双列(将i["Column"]替换为i[["Column"]] ),但这种策略也没有骰子

Thank you all again for saving my ass every day!!!再次感谢大家每天都在救我!!!

Put the dataframes in a list use grepl and is.na to remove rows from each.将数据框放入列表中,使用greplis.na从每个列表中删除行。 If you want to change original dataframe with this updated data use list2env .如果您想使用此更新数据更改原始 dataframe,请使用list2env

list_df <- dplyr::lst(df1, df2, df3)
result <- lapply(list_df, function(x) 
                 x[!(grepl('badString', x$Column) | is.na(x$Column)), ])

list2env(result, .GlobalEnv)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM