See the FAQ: How to do a minimal reproducible example reprex
for beginners for when looking for more specific answers.
Using the built-in mtcars
data
sum(mtcars$carb == 3)
#> [1] 3
median(mtcars[which(mtcars$carb == 3),"mpg"])
#> [1] 16.4
The second example is an exemplary pattern for the second and third questions.