I am sure this could be simple, but I can't find a solution.. I have a series of buffers for multiple years. In each buffer, I record the extent of forest cover. The forest cover declines over years. In each buffer, I have also a type of management.
I want to add two new columns to my dataset: the one regarding the original forest extent from 2003 for management type 0 and 1. The second relating the rate of forest extent to the original one, from 2003, for two types of management.
Based on these answers, I would assume to use something like: R: fill new columns in data.frame based on row values by condition?
df$area2003<-with(df, area[year == 2003 & manag == 0][match(buff, buff[year== 2003 & manag == 0])])
but it does not work how expected.
My example:
# create data frame
buff<-c(c(rep(c(1:2), each = 2)),
c(rep(c(1:2), each = 2)))
area<-seq(800, 1, by = -100)
year<-rep(2003:2004, each = 4)
manag<-rep(0:1, 4)
# create data frame
df<-data.frame(buff, area, year, manag)
# create values for original forest extent - how to code this???
df$area2003<-with(df, area[year == 2003 & manag == 0][match(distance, distance[year== 2003 & manag == 0])])
df$area2003<-with(df, area[year == 2003 & manag ==1 ][match(distance, distance[year== 2003 & manag == 1])])
# calculate forest rate:
df$rate<-df$area * 100/ df$area2003
What I expect to obtain:
buff area year manag area2003 rate
1 1 800 2003 0 800 100.00
2 1 700 2003 1 700 100.00
3 2 600 2003 0 600 100.00
4 2 500 2003 1 500 100.00
5 1 400 2004 0 800 50.00
6 1 300 2004 1 700 42.86
7 2 200 2004 0 600 33.34
8 2 100 2004 1 500 20.00