Assuming you have a data set in R and want to compute numerous 95th percentiles using the 'apply' function, the process would be:
Here's an example code snippet:
# Generate sample data
x <- matrix(rnorm(100), ncol = 5)
# Calculate 95th percentile for each column
apply(x, 2, function(y) quantile(y, 0.95))
# Return vector of 95th percentiles
sapply(x, function(y) quantile(y, 0.95))
In this example, we use the 'apply' function to calculate the 95th percentile for each of the five columns in the 'x' matrix. The output returns a matrix with one row and five columns, where each column represents a variable and its corresponding 95th percentile.
Asked: 2021-05-03 11:00:00 +0000
Seen: 16 times
Last updated: Nov 14 '22