r - Loop to assign new values to multiple spatial dataframes -


i working in rstudio (v 0.99.467) running r (v3.2.2) on windows10. have collection of vector maps arcgis read r loop using rgdal create objects of class 'spatialpolygonsdataframe'.

there several data qaqc checks , fixes perform in same loop. example, wish remove spaces entries in data frame. can outside loop, struggle referencing , name assignments within loop. outside of loop, command removes spaces in data:

# function want apply each map after read as.data.frame(apply(get(mymap)@data[],2,function(x)gsub('\\s+', '',x)))   

this function works within loop, can't reassign new no-spaces data replace original data. here example code show trying , stuck.

# vector of names map objects farmnames <- c("gardner","mistletoe","omni","sturgill")   library(rgdal)   # create vector of filenames based on farmnames filenames <- paste0(farmnames[],"_farm_fields_final")   # loop read maps , correct data (i in 1:length(farmnames)){       name <- farmnames[i]       assign(name, readogr(".", filenames[i])) # create map object       get(name)@data[]<- as.data.frame(apply(get(name)@data[],2,function(x)gsub('\\s+', '',x))) # remove spaces map dataframe }   

but last line returns error: "error in get(name)@data[] <- as.data.frame(apply(get(name)@data[], 2,: not find function "get<-". i'm guessing can't put function on left side of assignment? because if type "get(name)@data[]" r returns correct answer without errors.

i've tried various methods creating temporary dataframe, changing temporary dataframe, , using assign function replace dataframe within map object. again, works outside loop (assign(mymap@data[],d)) , not within loop (assign(get(name)@data[],d)).

# method 2 d <- as.data.frame(apply(d,2,function(x)gsub('\\s+', '',x)))   assign(get(name)@data[],d)   

but method returns error: "error in assign(get(name)@data[], d) : invalid first argument". presumably because first argument in assign supposed single element (variable name), not dataframe?

i've created subloop iterate through variable names making corrections column column - seems inefficient when perform corrections single line in main loop - if assignments work. i'm hoping has solution using either of 2 examples pasted above.

i not sure how attach example raw data object of class spatialpolygonsdataframe within stackoverflow. if there not enough information here me, can perhaps create public folder on github , provide link? expect pretty basic assignment problem still new r , programming.

the solution jhoward may work, using 'assign' (and get) not approach. clearer , easier use list. although, not specify want these objects after loop, hard sure. do:

farmnames <- c("gardner", "mistletoe", "omni", "sturgill")   x <- list() (i in 1:length(farmnames)){    temp <- readogr(".", paste0(farmname[i], "_farm_fields_final") )   temp@data <- as.data.frame(lapply(temp@data,function(x)gsub('\\s+', '',x)))   x[i] <- temp } 

or perhaps using trim function raster package cleaner code (i assuming call "vector maps" in fact shapefiles):

library(raster) farmnames <- c("gardner", "mistletoe", "omni", "sturgill")   x <- list() (i in 1:length(farmnames)){      filename <- paste0(farmname[i], "_farm_fields_final.shp")     temp <- shapefile(filename)     temp@data <- trim(temp@data)     x[i] <- temp } 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -