counting NAs across several columns of various types


when I want to identify complete cases across several columns using dplyr it will not work if columns are of different types. How to code to solve this error?
My code looks like this:


dbase <- starwars |> rowwise() |> 
mutate(compcases = ifelse(complete.cases(c_across(c(name : mass,  birth_year)))==TRUE, 1, NA))

I get the message:
Caused by error in vec_c():
! Can't combine name and height .

I would like to code using pipe operator and cannot figure out how to overcome this. Help appreciated.

#> [1] 87 14
pruned <- tidyr::drop_na(starwars)
#> [1] 29 14

this works

res <- starwars |> mutate(compcases = 
                            select(starwars,name:mass,birth_year) |> complete.cases())

1 Like

Yeah, thought about drop_na() but could not come up with an idea how to use it on a subset of columns, that is, eliminate NAs based only on a subset of variables, while retaining all columns and all rows in the dataframe..

Yes, thanks again :wink:

PS. I somehow cannot rely this to my data frame, the error message is "compcases must be size 1 not ..." but I guess it is something with my data frame, it works fine with starwars

however, this notation works:

res <- starwars
res$compcases  <- select(starwars,name:mass,birth_year) |> complete.cases()

This topic was automatically closed 7 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.