1

I have a list which includes 2 data.frames and would like to apply some analysis to each data.frame. I performed my analysis to a single data.frame and I want to do the same steps over my list. I am a bit confused on how to do that but I found that the easiest way to do it is by creating a function and then to use lapply. My code for the single data.frame is below

setwd("mydir")

#import .txt file
d = read.table("vourlionas.txt",
               header = FALSE,
               sep="", 
               col.names=c("x", "y"), 
               fill=FALSE, 
               strip.white=TRUE,
               stringsAsFactors = FALSE)

#remove the 1st row of the .txt file
d2 = d[-1,]

d3 = substr(d2$x, 1, 6)   
d4 = substr(d2$y, 1, 7)

df <- cbind(d3, d4)

df1 <- as.data.frame(df)

b = apply(as.matrix(df1),2,as.character)

#rename the column names of the matrix
colnames(b) [1] = paste("x")

mode(b) = "numeric"

b2 <- as.data.frame(b)

ch <- chull(b2)
coords <- b2[c(ch, ch[1]), ]

sp_poly <- SpatialPolygons(list(Polygons(list(Polygon(coords)), ID=1)))

proj4string(sp_poly) <- CRS("+init=epsg:2100")

sp_poly_df <- SpatialPolygonsDataFrame(sp_poly, data=data.frame(ID=1))

test1 = spTransform(sp_poly_df, "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")

writeOGR(obj=test1, dsn="mydir", layer = "r"
         driver="ESRI Shapefile", overwrite_layer = TRUE)

What I want is the above code, from the point remove the 1st row of the .txt file to be included in my function and to apply it in each element of my list. What I have managed to do so far is to insert my elements as a list

temp = list.files(pattern="*.txt")
myfiles = lapply(temp, read.delim, sep = "")

and to write the lapply code

lapply(myfiles, testfun)

The files of the list can be downloaded from here enter link description here

2
  • What does your function look like? Or what isn't working? Do all of your files have the same structure? I would also recommend transitioning away from sp and use sf instead for spatial operations, and would probably opt for using purrr and map instead of an lapply (personal preference but also easier to read)
    – GISHuman
    Feb 13 '20 at 16:09
  • I want to insert all these operations mentioned inside my function. What do you mean by «same structure»? Same number of rows and columns? If so, then no they don't have the same structure. I will check the libraries.
    – niktziok
    Feb 14 '20 at 7:28
0

This is untested but here is how I would approach it:

library(dplyr)
library(purrr)
library(tibble)
library(sp)
#Get list of files
mydir="C:/path/to/textfiles/"
temp = list.files(mydir,pattern="*.shp$",full.names = TRUE)
fn = list.files(mydir,pattern="*.txt$")

temp=enframe(temp)%>%
  mutate(savedir=fn)

temp = temp %>%
  #load in your table
  mutate(data = map(value,~read.table(.,header = FALSE,
                                      sep="", 
                                      col.names=c("x", "y"), 
                                      fill=FALSE, 
                                      strip.white=TRUE,
                                      stringsAsFactors = FALSE)))

#Your function

myfunction = function(d,save_name){

  #remove the 1st row of the .txt file
  d2 = d[-1,]

  d3 = substr(d2$x, 1, 6)   
  d4 = substr(d2$y, 1, 7)

  df <- cbind(d3, d4)

  df1 <- as.data.frame(df)

  b = apply(as.matrix(df1),2,as.character)

  #rename the column names of the matrix
  colnames(b) [1] = paste("x")

  mode(b) = "numeric"

  b2 <- as.data.frame(b)

  ch <- chull(b2)
  coords <- b2[c(ch, ch[1]), ]

  sp_poly <- SpatialPolygons(list(Polygons(list(Polygon(coords)), ID=1)))

  proj4string(sp_poly) <- CRS("+init=epsg:2100")

  sp_poly_df <- SpatialPolygonsDataFrame(sp_poly, data=data.frame(ID=1))

  test1 = spTransform(sp_poly_df, "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")

  writeOGR(obj=test1, dsn=paste0(mydir,save_name), layer = "r"
           driver="ESRI Shapefile", overwrite_layer = TRUE)

}

map2(temp$data,temp$savedir,~myfunction(.x,.y))
1
  • I think your answer has a mistake. When I run the code until the part before the function I am getting the following error: Error in mutate(., data = map(value, ~read.table(., header = FALSE, sep = "", : is.data.frame(.data) || is.list(.data) || is.environment(.data) is not TRUE. Also, if I change the *.shp$ part with .txt$ and I continue with the code at the end I am getting only 1 out of the 2 shp that I want to create.
    – niktziok
    Mar 17 '20 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.