Home > OS >  get all the rows descending by age and also only the ones with income=2?
get all the rows descending by age and also only the ones with income=2?

Time:09-12

lets say i have the following data:

enter image description here

how can i get all the rows descending by age and also only the ones with income=2? Is it possible to do this without using additional libraries?

CodePudding user response:

You could do:

set.seed(123)

df <- data.frame(
  age = sample(0:100, 20, replace = TRUE),
  income = sample(1:3, 20, replace = TRUE)
)

df <- df[df$income == 2,]
df <- df[order(df$age),]

df
#>    age income
#> 18   8      2
#> 1   30      2
#> 7   49      2
#> 3   50      2
#> 5   66      2
#> 13  90      2
  • Related