Error "longer object length is not a multiple of shorter object length" when using pipe operator with vectors

Viewed 16

I am working on a guided r project to display covid-19 trends. I got the output right by writing the following code:

positive_tested_top_3 <- positive_cases_vector/tested_cases_vector

positive_tested_top_3_sort <- sort(positive_tested_top_3, decreasing = TRUE)

head(positive_tested_top_3_sort, 3)

This is the correct output I get:

## United Kingdom  United States         Turkey 
##     0.11326062     0.10861819     0.08071172

However when I try to get the same output by using the pipe operator:

positive_tested_top_3 <- positive_cases_vector/tested_cases_vector %>%
  
  sort(decreasing = TRUE) %>%
  
  head(3)

...the output is wrong:

##  United States         Russia          Italy          India         Turkey 
##   0.1086181907   0.0385465516   0.0615233676   0.0035272376   0.0155508313 
##         Canada United Kingdom      Australia           Peru         Poland 
##   0.0222113264   0.0096577650   0.0006829651   0.0145423535   0.0013879468

...and is accompanied with the following error:

## Warning in positive_cases/tested_cases %>% sort(decreasing = TRUE) %>% head(3):
## longer object length is not a multiple of shorter object length

Why is that so? Aren't the two pieces of code equivalent?

0 Answers
Related