I wrote a simple function below:
mcs <- function(v) { ifelse(sum((diff(sort(v)) > 6) > 0), NA, sd(v)) }
It is supposed to take a vector, sort it, and then check if there is a difference greater than 6 in each successive difference. It returns NA if the difference is greater than 6 and standard deviation, if none.
I would like to apply this function to all rows of a data table (selecting only certain columns), and then add the return value for each row as a new column record in the data table.
For example, for a data table such as
> dat <- data.table(A=c(1,2,3,4,5), B=c(2,3,4,10,6), C=c(3,4,10,6,8),
D=c(3,3,3,3,3))
> dat
A B C D
1: 1 2 3 3
2: 2 3 4 3
3: 3 4 10 3
4: 4 10 6 3
5: 5 6 8 3
I would like to generate the output below. (I applied the function in columns 2, 3, and 4 of each row.)
> dat
A B C D sd
1: 1 2 3 3 0.5773503
2: 2 3 4 3 0.5773503
3: 3 4 10 3 3.7859389
4: 4 10 6 3 3.5118846
5: 5 6 8 3 2.5166115
I found out that a row operation can be performed using data tables using the following method:
> dat[, sd:=apply(.SD, 1, mcs), .SDcols=(c(2,3,4))]
, , . , script. . ~ 300 000 , , ~ 800 , . , , R -, . script , ( , ), , . , , . , .
, , , . R, , . , , , , , , . .
Edit
, mcs
. .
2
, , , .