Home > Enterprise >  Removing elements from three R vectors of the same length when one of the vectors meets a condition
Removing elements from three R vectors of the same length when one of the vectors meets a condition

Time:07-15

Three vectors of the same length x, y and z.

x <- c(3, 6, 12, 24, 23)
y <- c(34, 26, 33, 41, 54)
z <- c("A", "B", "C", "D", "E")

I want to remove elements from x and/or y that are below 10 as well as any values from z that have the same position of the removed x and y elements. Ideally, I want to end up having:

> x
[1]  12 24 23
> z
[1] "C" "D" "E"
> y
[1] 33 41 54

CodePudding user response:

It sounds like it would make sense to have these three vectors in a dataframe.

dat <- data.frame(x, y, z) |>
    subset(x >= 10 & y >= 10)
dat

Output

#>    x  y z
#> 3 12 33 C
#> 4 24 41 D
#> 5 23 54 E

You can then isolate them with dat$x etc.

CodePudding user response:

You can try this

df <- data.frame(x,y,z) |> subset(x >= 10 & y >= 10)

x <- df$x
y <- df$y
z <- df$z
  • Related