create a loop and make a shapiro test on specific columns chosen in a vector in r

Assuming you have a data frame in R with multiple columns and you have identified specific columns that you want to perform the Shapiro test on, you can use a loop to iterate through the columns in your vector and perform the Shapiro test on each column. Here's an example:

main.r
# sample data frame
df <- data.frame(
  x = rnorm(100),
  y = rnorm(100),
  z = rnorm(100)
)

# vector of column names to perform the Shapiro test on
cols <- c("x", "z")

# loop to perform the Shapiro test on each column
for (col in cols) {
  shapiro.test(df[[col]])
}
269 chars
15 lines

In this example, we have a data frame df with columns x, y, and z. We've created a vector cols containing the column names of x and z that we want to perform the Shapiro test on.

We then use a for loop to iterate through each column name in cols. Within the loop, we use the shapiro.test() function on the column by using df[[col]] to reference the column in the data frame.

Note that the shapiro.test() function tests the null hypothesis that the data is normally distributed. It returns a list containing the Shapiro-Wilk test statistic (W) and the p-value of the test (p). You may want to capture these values or perform additional actions based on the outcome of the test.

gistlibby LogSnag