Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

dplyr summarise across when column is sometimes missing

This works:

df <- data.frame(a=c(1,2,3),b=c(4,2,3),c=c(2,5,1))

df %>% summarise(across( c(a,b,c), sum, na.rm=TRUE))

But this doesn’t, because d doesn’t exist:

df <- data.frame(a=c(1,2,3),b=c(4,2,3),c=c(2,5,1))

df %>% summarise(across( c(a,b,c,d), sum, na.rm=TRUE))

This is as part of a function which I then apply to a whole bunch of dataframes in a list using lapply, where d is present nearly all the time, so I can’t just remove d.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’m trying to get to something like this:

df <- data.frame(a=c(1,2,3),b=c(4,2,3),c=c(2,5,1))

df %>% summarise(across( any_of(c(a,b,c,d)), sum, na.rm=TRUE))

>Solution :

You were nearly there, any_of expects a character vector:

library(dplyr)

df <- data.frame(a=c(1,2,3),b=c(4,2,3),c=c(2,5,1))

df %>% summarise(across( any_of(c("a","b","c","d")), sum, na.rm=TRUE))
#>   a b c
#> 1 6 9 8

Created on 2023-01-03 by the reprex package (v1.0.0)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading