Thelonious Monk
Thelonious Monk

Reputation: 466

R - Take average of previous 4 rows and expand table

I have a table like :

Texas Colorado

3      1
4      2
3      3
4      4

I want to extend this table by 30 rows such that, each new row takes the average of the previous 4 rows. Expected output:

Texas Colorado

3     1
4     2
3     3
4     4
3.5   2.5
3.625 2.875

My main issue is I don't know how to dynamically create rows

Upvotes: 1

Views: 395

Answers (3)

akrun
akrun

Reputation: 887213

We can use rollmean from zoo

library(zoo)
for(i in 1:30) df1 <- rbind(df1, rollmeanr(tail(df1, 4), k = 4))
df1
#      Texas Colorado
#1  3.000000 1.000000
#2  4.000000 2.000000
#3  3.000000 3.000000
#4  4.000000 4.000000
#5  3.500000 2.500000
#6  3.625000 2.875000
#7  3.531250 3.093750
#8  3.664062 3.117188
#9  3.580078 2.896484
#10 3.600098 2.995605
#11 3.593872 3.025757
#12 3.609528 3.008759
#13 3.595894 2.981651
#14 3.599848 3.002943
#15 3.599785 3.004777
#16 3.601264 2.999533
#17 3.599198 2.997226
#18 3.600024 3.001120
#19 3.600068 3.000664
#20 3.600138 2.999636
#21 3.599857 2.999661
#22 3.600022 3.000270
#23 3.600021 3.000058
#24 3.600009 2.999906
#25 3.599977 2.999974
#26 3.600007 3.000052
#27 3.600004 2.999997
#28 3.599999 2.999982
#29 3.599997 3.000001
#30 3.600002 3.000008
#31 3.600000 2.999997
#32 3.600000 2.999997
#33 3.600000 3.000001
#34 3.600000 3.000001

Or using tidyverse

library(tidyverse)
for(i in 1:2) {
              df1 <- df1 %>%
                        slice((n() - 3):n()) %>%
                        summarise_all(mean) %>%
                        bind_rows(df1, .)
 }

Or with accumulate

seq_len(30) %>%
    accumulate(.,  ~ .x %>% 
    slice(tail(row_number(), 4)) %>% 
    summarise_all(mean) %>%
    bind_rows(.x, .), .init = df1) %>%
    .[[30]]
#    Texas Colorado
#1  3.000000 1.000000
#2  4.000000 2.000000
#3  3.000000 3.000000
#4  4.000000 4.000000
#5  3.500000 2.500000
#6  3.625000 2.875000
#7  3.531250 3.093750
#8  3.664062 3.117188
#   ...

data

df1 <- structure(list(Texas = c(3L, 4L, 3L, 4L), Colorado = 1:4), 
 class = "data.frame", row.names = c(NA, 
  -4L))

Upvotes: 2

Jilber Urbina
Jilber Urbina

Reputation: 61164

You can use while and colMeans from R base.

while(nrow(df1)<=30){ 
  df1 <- rbind(df1, colMeans(tail(df1, 4)))
}
df1

       Texas Colorado
1  3.000000 1.000000
2  4.000000 2.000000
3  3.000000 3.000000
4  4.000000 4.000000
5  3.500000 2.500000
6  3.625000 2.875000
7  3.531250 3.093750
8  3.664062 3.117188
9  3.580078 2.896484
10 3.600098 2.995605
...

Upvotes: 2

Onyambu
Onyambu

Reputation: 79238

In base R you can do:

 Reduce(function(x,y)rbind(x,colMeans(tail(x,4))),1:30, init = df)
      Texas Colorado
1  3.000000 1.000000
2  4.000000 2.000000
3  3.000000 3.000000
4  4.000000 4.000000
5  3.500000 2.500000
6  3.625000 2.875000
7  3.531250 3.093750
8  3.664062 3.117188
9  3.580078 2.896484
10 3.600098 2.995605
11 3.593872 3.025757
...

Upvotes: 3

Related Questions