-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inconsistency in Results Using sum(. != 3) within Pipeline Operations (%>%) dplyr version 1.1.4 #7037
Comments
Calculate it like this: library(magrittr)
data <- data.frame(
GO.BiologicalProcess = c("-", "-", "A", "B"),
GO.CellularComponent = c("-", "C", "-", "D"),
GO.MolecularFunction = c("-", "-", "-", "E")
)
{data[, c("GO.BiologicalProcess", "GO.CellularComponent", "GO.MolecularFunction")] == "-"} %>%
rowSums() %>%
{. != 3} %>%
sum()
#> [1] 3 Created on 2024-06-10 with reprex v2.1.0 |
initial datas foo<-{data[, c("GO.BiologicalProcess", "GO.CellularComponent", "GO.MolecularFunction")] == "-"} %>% rowSums()
foo
# [1] 3 2 2 0
foo2 <- foo %>% {. != 3}
foo2
# [1] FALSE TRUE TRUE TRUE Therefore At the opposite foo %>% {. != 3} %>% sum(.=TRUE)
# [1] 4 And in basic R or basic R with
or piped function or not piped function: testsum <- function (x) if_else(x!=3,x,0)
foo %>% testsum(.) %>% sum(.)
# [1] 4
sum(testsum(foo))
# [1] 4 |
Read You can use braces to avoid this rowSums(data[, c("GO.BiologicalProcess", "GO.CellularComponent", "GO.MolecularFunction")] == "-") %>% {
sum(. != 3)
} but I think avoiding the pipe entirely is cleaner here sums <- rowSums(data[, c("GO.BiologicalProcess", "GO.CellularComponent", "GO.MolecularFunction")] == "-")
sum <- sum(sums != 3) |
Thanks for the explanation. I've looked for why |
I encountered an inconsistency in the results obtained from different approaches while working with R code. Specifically, when using the sum(. != 3) expression within a pipeline operation (%>%), the outcome varied from expectations, leading to discrepancies compared to direct computations.
The text was updated successfully, but these errors were encountered: