user7307305
user7307305

Reputation:

matching between two data frame based on columns

I have two data frames of one row, each data frame has the same columns names. one of the data frames has NA value in one or more of the columns. I want to remove the columns that has NA values in one of the data frame and remove the same columns in the second data frame.

sample:

data frame 1:

age height education average
 NA  1.80   college    NA

data frame 2:

age height education  average
 36  1.95   college     85

result:

data frame 1:

 height education
  1.80   college

data frame 2:

height education
 1.95   college

how do I do this?

Upvotes: 0

Views: 59

Answers (1)

Julia Silge
Julia Silge

Reputation: 11663

It sounds like these are data frames, not vectors. If you put them together into the same data frame (perhaps with bind_rows()), you can use dplyr to handle them all at once and find the columns you want without NA values:

library(dplyr)

df <- tribble(
    ~age, ~height, ~education, ~average,
      NA,    1.80,  "college",       NA,
      36,    1.95,  "college",       85
)

df %>% 
    select(which(!colSums(is.na(df))))

#> # A tibble: 2 x 2
#>   height education
#>    <dbl>     <chr>
#> 1   1.80   college
#> 2   1.95   college

Upvotes: 1

Related Questions