Tato14
Tato14

Reputation: 435

Select rows that have values with an standard deviation between them below a threshold

I have input data like this:

         Condition1(Day1) Condition1(Day2) Condition2(Day1) Condition2(Day2)
Sample1       1.345            2.456            7.898            8.134
Sample2...

I would like to select the samples (rows) that have the standard deviation between days below a threshold in, at least, one of the two conditions.

Thanks!

Upvotes: 0

Views: 68

Answers (1)

pali56
pali56

Reputation: 79

I hope this helps.

sample=data.frame("cond1_day1"=c(1.345,1.456,1.7,2,1,4.2),
              "cond1_day2"=c(2.456,1,5,1,2,2.2),
              "cond2_day1"=c(7.898,7,2,3,4,5),
              "cond2_day2"=c(8.134,4.5,1,2,9,1))
##calculating standard deviation between days for each conditions
sd=t(apply(sample,1,function(x) c(sd(c(x[1],x[2])),sd(c(x[3],x[4])))))
sample$sd_cond1=sd[,1]
sample$sd_cond2=sd[,2]
##getting subsets if sd crosses threshold for any of the condition
threshold=0.8
sample[sample$sd_cond1>threshold | sample$sd_cond2>threshold,]

#output
cond1_day1 cond1_day2 cond2_day1 cond2_day2  sd_cond1  sd_cond2
1.456        1.0          7        4.5      0.3224407 1.7677670
1.700        5.0          2        1.0      2.3334524 0.7071068
1.000        2.0          4        9.0      0.7071068 3.5355339
4.200        2.2          5        1.0      1.4142136 2.8284271

Upvotes: 1

Related Questions