Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12141

Discrepancy with summarise and summarise_at results - R language

$
0
0

I have a dataset that looks like this:

a <- data.frame(cat = c("a","b","b","c","a","c"),           num1 = c(-2,1,2,4,3,2),           num2 = c(-2,3,1,5,3,2),           weight = c(3.12,2,14,1.12,1,12))

I'm trying to get a weighted mean for each "num" column (on my real dataset I have many "num" columns, all named the same).

I started coding it like this:

a %>% summarise(num1 = weighted.mean(num1,weight,na.rm=T),                num2 = weighted.mean(num2,weight,na.rm=T))

Which returns:

num1num2
1.6618531.394705

But since I have to process many columns I moved to this way:

a %>% summarise(across(contains("num"), ~weighted.mean(.x,na.rm=T)))

or:

a %>% summarise_at(.vars=vars(contains("num")),                   function(x,weight) weighted.mean(x,weight))

They both return:

num1num2
1.6666672

Why am I getting different results here? Which way should I trust?


Viewing all articles
Browse latest Browse all 12141

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>