2

I tried to do this:

DT <- data.table(Monthname = month.name, id = 1:3, a = abs(rnorm(12)), b = abs(rnorm(12)), c = abs(rnorm(12)), d = abs(rnorm(12)))
setkey(DT, id)
ANS <- DT[,lapply(.SD, mean)/lapply(.SD, sd), by = 'id', .SDcols = names(DT)[-1]]

but it gives error. So, Are there any ways to do this ? Thank You.

1
  • An alternative to the answer below is to define a function like myfun <- function(x) mean(x)/sd(x) and then use DT[, lapply(.SD, myfun), by = "id", .SDcols = names(DT)[-1]]. Commented Feb 17, 2014 at 16:15

1 Answer 1

2

Just that same as one would use lapply in other contexts:

ANS <- DT[,lapply(.SD, function(x) mean(x)/sd(x) ), by = 'id', .SDcols = names(DT)[-1]]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.