sapply() fucntion to generate summary statistics, need to remove a column before I use the function

StackOverflow https://stackoverflow.com/questions/23418952

  •  13-07-2023
  •  | 
  •  

Question

I am trying to generate summary statistics by using sapply on 5 columns with numerical data. There is however 1 column with sex F/M (which is the second column of my dataframe), which I do not need to apply this to. I have tried removing the column by using

data_2 <- data_2[,2]  

and a bunch of other methods but they do not seem to remove the column. I have to work out the mean, sd, min, max and sample size with the sapply function.

Was it helpful?

Solution

In cases like this, I find it easier to use indices, instead of the data itself:

sapply((1:ncol(data_2))[-2], function(i) {
  c(mean(data_2[,i]), sd(data_2[,i])) # add other functions
})

OTHER TIPS

Use

data_2 <- data_2[, -2]  

minus removes the column, without the minus you're just returning the second column.

However overwriting data_2 with data_2[, -2] is not optimal, so better just to run sapply on data_2[, -2].

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top