I ran a loop which goes over many folders to convert TIF file to a data frame, but I want to insert part of the file name as a column value. Is it possible?.
For example, if my file name is "Rain_02_02_2010_southRegion", I want to take only "2010" and make it as a value in a new column. The second file might have "Rain_02_02_2017_southRegion", so I want to take 2017 and so on. I tried dat["Date"] <-, but I couldn't insert the code that makes it work.
ls = list.files()
for(i in ls){
rs=list.files(paste0(getwd(),"/",i),pattern=".tif$",full.names=T)
rs = lapply(rs,raster)
rs=do.call(stack,rs)
dat=as.data.frame(rs, xy =TRUE)
dat= dat[complete.cases(dat), ]
dat["Date"] <-
}