I am trying to perform a simple loop, adding new columns to a data frame, by using existing columns. Consider the following data set, for an elderly population in the years 1986 to 1989:
col.names <- c("Age","Pop86","Pop87","Pop88","Pop89")
col1 <- c(76,77,78,79,80,81,82,83,84,85)
col2 <- c(102,106,91,85,67,77,54,47,36,39)
col3 <- c(111,96,96,76,76,62,60,49,42,35)
col4 <- c(108,101,88,88,69,67,56,49,42,32)
col5 <- c(141,100,96,80,78,60,49,43,38,25)
df1 <- data.frame(col1,col2,col3,col4,col5)
colnames(df1) <- col.names
Age Pop86 Pop87 Pop88 Pop89
1 76 102 111 108 141
2 77 106 96 101 100
3 78 91 96 88 96
4 79 85 76 88 80
5 80 67 76 69 78
6 81 77 62 67 60
7 82 54 60 56 49
8 83 47 49 49 43
9 84 36 42 42 38
10 85 39 35 32 25
I have written a simple loop showing roughly what I want:
num_iterations <- 4
for (i in 1:num_iterations) {
df1[[paste0("Col", i)]] <- lag(df1$Pop86,default = 0)- df1$Pop87
}
Age Pop86 Pop87 Pop88 Pop89 Col1 Col2 Col3 Col4
1 76 102 111 108 141 -111 -111 -111 -111
2 77 106 96 101 100 6 6 6 6
3 78 91 96 88 96 10 10 10 10
4 79 85 76 88 80 15 15 15 15
5 80 67 76 69 78 9 9 9 9
6 81 77 62 67 60 5 5 5 5
7 82 54 60 56 49 17 17 17 17
8 83 47 49 49 43 5 5 5 5
9 84 36 42 42 38 5 5 5 5
10 85 39 35 32 25 1 1 1 1
Now, each added column is identical and based columns Pop86 and Pop87. I of course want to make the loop move across columns, using pairs (Pop86,Pop87) and then (Pop87,Pop88) and so on, but my attempts at this have failed:
num_iterations <- 4 # Adjust as needed
for (i in 1:num_iterations) {
col1 <- paste0("Pop", 87 + i - 1)
col2 <- paste0("Pop", 87 + i)
df1[[paste0("Col", i)]] <- lag(df1[[col1]],default = 0) - df1[[col2]]
}
Error in `[[<-.data.frame`(`*tmp*`, paste0("Col", i), value = numeric(0)) :
replacement has 0 rows, data has 10
I am looking for the right way to loope across these columns. Since these columns are part of a larger dataset, I would like the loop to work through all PopXX columns.
Without loop: