Ask Your Question
4

What is the process for incorporating the 'apply' function to compute numerous 95th percentiles?

asked 2021-05-03 11:00:00 +0000

huitzilopochtli gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-11-14 06:00:00 +0000

ladyg gravatar image

Assuming you have a data set in R and want to compute numerous 95th percentiles using the 'apply' function, the process would be:

  1. Load or generate your data set in R.
  2. Identify the variables/columns you want to calculate the 95th percentile for.
  3. Use the 'apply' function to calculate the 95th percentile for each variable/column.
  4. Wrap the 'apply' function in the 'sapply' function if you want to return a vector of 95th percentiles.
  5. Store the results in a variable for further analysis or output.

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-05-03 11:00:00 +0000

Seen: 16 times

Last updated: Nov 14 '22