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

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

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

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

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...