Search code examples
r

How to give a variable name to output variable in R?


i m trying to average raster daily datasets on monthly basis. For that i want to keep the name of output as varialbe. Can anyone help me out?

require(raster)
hab=list.files(getwd(), pattern="tif$", full.names=FALSE)

for (k in 1:length(hab)){

paste("January", k) <- stack(hab[1], .................., hab[30])

paste("Jan", k) <- mean(paste("January"), k)


.....

}

Solution

  • Use assign and get to reference objects by a character string instead of symbols. Also, bear in mind that you won't be able to use names with spaces, so set sep='' when calling paste.

    assign(paste("January", k, sep=''), stack(hab[1], .................., hab[30]))
    
    assign(paste("Jan", k, sep=''), mean(get(paste("January", k, sep=''))))