Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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'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

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

Thank you all again for saving my ass every day!!!

question from:https://stackoverflow.com/questions/65931018/deleting-rows-from-data-frames-within-for-loop

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
235 views
Welcome To Ask or Share your Answers For Others

1 Answer

Put the dataframes in a list use grepl and is.na to remove rows from each. If you want to change original dataframe with this updated data use 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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...