user3594635
user3594635

Reputation: 3

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

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.

Upvotes: 0

Views: 549

Answers (2)

Scott Ritchie
Scott Ritchie

Reputation: 10543

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
})

Upvotes: 1

James King
James King

Reputation: 6365

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].

Upvotes: 1

Related Questions