Reputation: 3
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
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
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