如何删除一个数据框中的空行,同时删除另一个数据框中的相应行?

时间:2020-10-27 20:48:35

标签: r dataframe

我有两个数据框,其中一个(df_B)的行为空:

df_A = data.frame(Text = c("Hello World", "Hello", "Hello Hello Hello", "Hey Ciao Hi"), stringsAsFactors = F)

               Text
1       Hello World
2             Hello
3 Hello Hello Hello
4       Hey Ciao Hi


df_B = data.frame(Text = c("", "Why not asking SE for a solution", "SE will fix this", ""), stringsAsFactors = F)

                             Text
1                                 
2 Why not asking SE for a solution
3                 SE will fix this
4        

我想删除df_B的空白行,并同时删除df_A的相应行。这就是我想要得到的:

# df_A

               Text

2             Hello
3 Hello Hello Hello


# df_B

                             Text

2 Why not asking SE for a solution
3                 SE will fix this

有人可以帮我吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

就这么简单

newB<-df_B[df_B$Text != "",]
newA<-df_A[df_B$Text != "",]

第二个选项使用 magrittr和dplyr

df_B%>%filter(df_B$Text != "")
df_A%>%filter(df_B$Text != "")