R: How to take the min and max or other functions

2019-07-08 20:27发布

问题:

I have a dataframe of which I put one variable into a vector.

From this vector, I would like to calculate for every 5 values mean, min and max value.

I have managed to calculate the means in this way:

means <- colMeans(matrix(df$values, nrow=5))

I know I can calculate the min and max like this:

max <- max(df$values[1:5])
min <- min(df$values[1:5])

How do I repeat this for every five values?

Edit:

Aditionally, how can I get statistic and p-value from a 1-sample t-test for each n-row?

回答1:

You can use sapply and split for this:

sapply(split(df$value, rep(1:(nrow(df)/5), each=5)), mean)
sapply(split(df$value, rep(1:(nrow(df)/5), each=5)), min)
sapply(split(df$value, rep(1:(nrow(df)/5), each=5)), max)

If you want the outputs in a matrix you can use what @lmo proposed in the comments:

sapply(split(df$value, rep(1:(nrow(df)/5), each=5)),
                       function(x) c(mean=mean(x), min=min(x), max=max(x)))

Update

How to get statistic and p-value from a sample t-test for each n-row: This would be a bit harder to implement. Look below;

#mu=3 for sample t-test
t_test_list <- sapply(split(df$value, rep(1:(nrow(df)/5), each=5)), t.test, mu=3) 

p_value_list <- lapply(as.data.frame(t_test_list),function(x) x$p.value)
statistic_list <- lapply(as.data.frame(t_test_list),function(x) x$statistic)

p_value_list and statistic_list are p.value and statistic for each 5 rows.



回答2:

1) tapply Below g is a grouping variable consisting of fives ones, fives twos and so on. range provides the minimum and maximum resulting in a list output from tapply and then simplify2array reduces that to an array. Omit the simlify2array if you want a list output. out[1, ] would be the minima and out[2, ] would be the maxima.

values <- 1:100 # test input

n <- length(values)
g <- rep(1:n, each = 5, length = n)
out <- simplify2array(tapply(values, g, range))

giving:

> out
     1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19  20
[1,] 1  6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91  96
[2,] 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100

2) aggregate This would also work:

ag <- aggregate(values, list(g = g), range)

giving this data.frame where the first column is g and the second column is the transpose of the matrix in (1). Here ag[[2]][, 1] is the minima and ag[[2]][, 2] is the maxima. If you want to flatten ag try do.call(data.frame, ag) or do.call(cbind, ag) depending on whether you want a 3 column data frame or matrix.

> ag
    g x.1 x.2
1   1   1   5
2   2   6  10
3   3  11  15
4   4  16  20
5   5  21  25
6   6  26  30
7   7  31  35
8   8  36  40
9   9  41  45
10 10  46  50
11 11  51  55
12 12  56  60
13 13  61  65
14 14  66  70
15 15  71  75
16 16  76  80
17 17  81  85
18 18  86  90
19 19  91  95
20 20  96 100


回答3:

Certainly an atypical way to do it, and maybe not the most efficient, but you could try zoo::rollapply. This gives you more info than you need, but you can then filter down to only what you want:

vals <- 1:20
zoo::rollapply(vals, 5, function(x) c(min = min(x), max = max(x), mean = mean(x)))[seq(from = 1, to = length(vals), by = 5),]

    min max mean
[1,]   1   5    3
[2,]   6  10    8
[3,]  11  15   13
[4,]  16  20   18


回答4:

For those who love dplyr and want to preserve the structure of the data, you can use the RcppRoll package

 df <- data.frame(
   Time = 1:10,
   Value = sample(100:200, 10)
 )

> df
   Time Value
#1     1   122
#2     2   185
#3     3   138
#4     4   134
#5     5   167
#6     6   197
#7     7   161
#8     8   171
#9     9   152
#10   10   106

Now finding the max

df%>%mutate(
   ad = RcppRoll::roll_maxr(Value, 3, fill = "0")
 )
   Time Value  ad
#1     1   122   0
#2     2   185   0
#3     3   138 185
#4     4   134 185
#5     5   167 167
#6     6   197 197
#7     7   161 197
#8     8   171 197
#9     9   152 171
#10   10   106 171