0

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

  • In your `save` function, you forgot to specify a name for the file you save. Something like `file = "/Donnees_pour_tests/input.Rdata"`. It's why you have this error. I did not look at the rest of the code. – DJack Apr 16 '18 at 15:19
  • Thx a lot, it was very useful! The error does not appear anymore.I still got a problem to rename the columns, i'm working on it :) – Ally U. Apr 16 '18 at 15:45

2 Answers2

0

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)
}
Edward Carney
  • 1,372
  • 9
  • 7
  • Thx for your answer! I tried to use your code but I still go an error : Error in names(x) <- c("X", "Y", "Z", "Classe", "Intensite_signal_retour", : 'names' attribute [6] must be the same length as the vector [1] . I tried to solve it but I got an other error : Error in read.table(file = file, header = header, sep = sep, quote = quote, : 'file' doit être une chaîne de caractères ou une connexion – Ally U. Apr 17 '18 at 13:16
  • The files you are reading very likely do not have comma separated columns, resulting in a problem when you read them in using `read.csv`. When read in by `read.csv`, without specifying the separator character, you will have only one column. This explains the mismatch between vector [1] and the names attribute [6]. Many `xyz` Lidar files only have three columns separated by tab or space characters.Try reading the file with `x<-read.table` and looking at the result using `head(x)`. Set the column names according to the number of columns you have. – Edward Carney Apr 18 '18 at 00:03
0

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!