'Subsetting an unnamed vector in R
I'm getting a vector of numbers as output from one function, and am wanting to drop all the values higher than 2900, then pipe the remainder directly into a second function. (They'll be sorted, if that helps.) Is there a clever way to do this seemingly simple thing without having to stop and define an intermediate variable?
Solution 1:[1]
Here is a way without creating a temp vector.
- The functions
fandgare simple test functions that output a sequence of integers from1 to their argumentn. FunctiongassignsNAto half of the output vector. - Function
hsums its input vector. - In the middle of the pipe, there's an anonymous function that subsets the output of
forgand pipes the resulting vector to functionh. - In the case of the pipe from
g, extra code is needed to removeNA's, if that's what the user wants.
f <- function(n) seq.int(n)
g <- function(n){
y <- seq.int(n)
is.na(y) <- sample(n, n/2)
y
}
h <- function(x, na.rm = FALSE) sum(x, na.rm = na.rm)
set.seed(2022)
f(3000) |> (\(x) x[x <= 2900])() |> h()
#> [1] 4206450
set.seed(2022)
g(3000) |> (\(x) x[x <= 2900])() |> h()
#> [1] NA
set.seed(2022)
g(3000) |> (\(x) x[x <= 2900])() |> h(na.rm = TRUE)
#> [1] 2080026
set.seed(2022)
g(3000) |> (\(x) x[which(x <= 2900)])() |> h()
#> [1] 2080026
Created on 2022-03-12 by the reprex package (v2.0.1)
Edit
Following Mikael Jagan's comment, the input can be piped to the first function like below.
input <- 3000
input |> f() |> (\(x) x[x <= 2900])() |> h()
#> [1] 4206450
Created on 2022-03-12 by the reprex package (v2.0.1)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
