How to use na.omit only for specific columns but still display all columns?

Hi everyone!
As this is my first post here, I hope I do everything correctly. Otherwise, I am happy to correct myself.

I am working with a dataframe consisting of about 50 columns and 300 entries which is called "fbdaten". In this data frame, I am interested in all entries that have no NA in either "Vertraeglichkeit", "Offenheit" or "Statistik". I tried the following code with na.omit to create a new dataframe which contains all entries that don't have NA in any one of these columns:

fbdatenNoNA <- na.omit(fbdaten[,c("Statistik", "Offenheit", "Neurotizismus")])
fbdatenNoNA

However, I do get an Output that only contains the columns "Statistik", "Offenheit" and "Neurotizismus". But I still need to see all 50 columns, not only these.

I tried this alternative code i found on the R documentation:

fbdatenNoNA <- na.omit(fbdaten, cols = c("Statistik", "Offenheit", "Neurotizismus"))
fbdatenNoNA

However, this output gives me exactly the same as when I just use

fbdatenNoNAatAll <- na.omit(fbdaten)

which works totally fine but is not what I need since I only need to exclude the entries that have NA in either of these above mentioned columns and not entries with NA's in any column...

Usually, I would get on fine with the R documentation, but this somehow just does not want to work and I am thankful for any input at all!

Thank you very much already!

library(tidyverse)

(mydf <- tribble(
  ~A, ~B, ~C,
  NA, 1, NA,
  2, NA, NA,
  3, 4, NA,
  5, 6, 7
))


na.omit(mydf)
# A tibble: 1 x 3
A     B     C
<dbl> <dbl> <dbl>
  1     5     6     7

# but you only want to omit on A and B,
# so you try
na.omit(mydf %>% select(A,B))

#but you want column c that corresponds
(mydf2 <- mutate(mydf, rn=row_number()))
(step1 <- na.omit(mydf2 %>% select(A,B,rn)))
(final <- step1 %>%
               left_join(select(mydf2,C,rn),
                          by="rn") %>% 
                 select(-rn))

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.