# r – After multiplying two variables, how can I filter those products based on the value of the original variables?

I am interested in multiplying subsets of two variables, `Z` and `x` in my dataset, but the two subsets are of different lengths so I can’t subset both before multiplying them. How can I filter the product based on the value of the original variables?

Here is a simplified data sample. I want to multiply `Z` and `x`, but keep only the products for which `Z` is greater than the 50th percentile and for which `abs(x)` is less than 100. My code below generates 10 values for each. There are 5 values for `Z` > 50th percentile, but only 4 values for `abs(x) < 100`.

``````## Create dataframe
set.seed(47610)
Z <- rnorm(10, mean = 5, sd = 1)
x <- rnorm(10, mean = -101, sd = 3)
df <- data.frame(cbind(Z, x))

## Keep only Z above 50th percentile, multiply by x
Z <- df %>% filter(Z > quantile(Z, 0.5)) %>% select(Z)
Zx <- Z*df\$x
``````

`Z` and `Zx` are both 5×1.

If I try to filter after obtaining `Zx` through multiplication, I get the following error message telling me the dimensions are wrong:

Error: Problem with `filter()` input `..1`.
x Input `..1` must be of size 5 or 1, not size 10.
ℹ Input `..1` is `abs(df\$x) < 100`.

How can I achieve this? Thanks for the help.

Posted on