Ally U.
Ally U.

Reputation: 1

Download multiple CSV files (Lidar data), give a name to the columns and join every files

Hi to the R community!

I'm a new R user and after hours of searching how to do, I hope you would help me to solve my problem and understand where is/are the mistake(s).

So, I have Lidar data from the french Litto3D programm. I need to use twelve tiles to produce a Digital Field Model using several kind of interpolation (I'm gonna compare the different ways of interpolation). For that, I begin to use Surfer because of the height of the data and to transfer the method to my colleagues and to everyone who would need it.

What I did and what I tried to do : To begin, I changed the file extension of the XYZ files (which contain the XYZ Lidar points) and I used a code to charge the 12 tiles in R. It seems to have worked but now I want to give a name to the colums of all my files in the same time (because they do not have any name at the moment), and this is exactly the point where it doesn't work anymore. I have an error which appears : "Error during wrapup: 'file' must be a character string or connection"

The entire code :

List.filesxyz   = list.files("Donnees_pour_tests",pattern = ".xyz",recursive=F,full.names = T)

xyztest=List.filesxyz

old_filenames <- List.filesxyz

new_filenames <- replace_extension(List.filesxyz,"txt")

file.rename (old_filenames,new_filenames)

List.filestxt = list.files("Donnees_pour_tests",pattern = ".txt",recursive=F,full.names = T)

csvtest=List.filestxt

old_filenames <- List.filestxt

new_filenames <- replace_extension(List.filestxt,"csv")

file.rename (old_filenames,new_filenames)

setwd("C:/Mydirectory/Rspatial2018/data/Donnees_pour_tests")

list.files(pattern=".csv$")

list.filenames<-list.files(pattern=".csv$")

list.filenames

list.data<-list()

for (i in 1:length(list.filenames))
{
  list.data[[i]]<-read.csv(list.filenames[i])
}

names(list.data)<-list.filenames

list.data[1]

import.multiple.csv.files<-function(mypath,mypattern,...)
{
  tmp.list.1<-list.files(mypath, pattern=mypattern)
  tmp.list.2<-list(length=length(tmp.list.1))
  for (i in 1:length(tmp.list.1)){tmp.list.2[[i]]<-read.csv(tmp.list.1[i],...)}
  names(tmp.list.2)<-tmp.list.1
  tmp.list.2
}

csv.import<-import.multiple.csv.files("C:/Mydirectory/Rspatial2018/data/Donnees_pour_tests/",".csv$",sep=" ", dec=".")

save(import.multiple.csv.files,file="C:/Mydirectory/Rspatial2018/data/Donnees_pour_tests/")

data = read.csv(list.data,header=FALSE,sep=" ", dec = ".")

setnames(data, old=c("V1","V2","V3", "V4", "V5", "V6"),     new=c("X","Y","Z","Classe","Intensite_signal_retour","Temps_GPS_absolu"))
  }

I wish you could help me. I'm still going to find he solution on the web :) Thx a lot! Ally

Upvotes: 0

Views: 407

Answers (2)

Ally U.
Ally U.

Reputation: 1

I finally found a simple way to do the same things. I first charged and merged all my CSV files and I created a dataframe with all of them (it was not the case with the first code, only the first file was taken). Then I give a name to the columns of the dataframe, that I can now export if I need as a single table.

I share the code in case of someone would need it :

## import and merging of the CSV files in a single table (dataframe)##

multmerge <- function(mypath = getwd()){ #getwd correspond to the call of the directory where are the data## 
  require(dplyr)
  dataset <- list.files(path=mypath, full.names=TRUE, pattern="*.csv") %>% lapply(read.csv, header=F, sep=" ") %>% bind_rows()
  dataset
}
AllCSVFiles <-  multmerge(mypath=getwd())

###############################################################

#### give a name to the columns ####
head(AllCSVFiles) #to read the first rows and columns#
setnames(AllCSVFiles,old=c("V1","V2","V3","V4", "V5", "V6"), new=c("X","Y","Z","Classe","Intensite_signal_retour","Temps_GPS_absolu"))
head(AllCSVFiles) #to check the new names of the columns#

See ya!

Upvotes: 0

Edward Carney
Edward Carney

Reputation: 1392

If I understand your intent correctly, a simple for loop will do what you want.

# list of files
filelist <- dir(pattern='csv$')
# read files and rename the columns
for (i in filelist) {
    x <- read.csv(i)
    names(x) <- c("X","Y","Z","Classe","Intensite_signal_retour","Temps_GPS_absolu")
    write.csv(x, i, row.names=F)
}

Upvotes: 0

Related Questions