AnaS90 - 1 year ago 77

R Question

I have a time series data frame in the table below. I would like to do a paired-test in R between the 00:00 column and the 00:15 column. Then between the 00:15 and the next one, and so on.

My data looks like this.

Can someone help me with that?

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

You can do this with a for loop:

```
res=NULL
for(i in 2:ncol(df)){
res<-c(res, t.test(df[, i-1], df[, i], paired=T)$p.value)
}
print(res)
#### [1] 0.87346300 0.29767460 0.07232177
```

Or with the `apply`

family of functions:

```
sapply(2:ncol(df), function(x) t.test(df[, x-1], df[, x], paired=T)$p.value)
#### [1] 0.87346300 0.29767460 0.07232177
```

I see a third solution using `rowr::rollApply`

, it applies a function to a rolling window, here by 2 (to the transposed of your data)

```
library(rowr)
rollApply(t(df), function(x) t.test(x[1, ], x[2, ], paired=T)$p.value, minimum=2, window=2)
#### [1] 0.87346300 0.29767460 0.07232177
```

I recommend to adjust the confidence level to take into account the multiplicity of comparisons (something like `alpha/(ncol(df)-1)`

)

your data:

```
set.seed(1)
df=as.matrix(data.frame(rnorm(4),rnorm(4), rnorm(4), rnorm(4)))
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**