1

我有许多txt文件在由 ; 分隔的列中包含相同类型的数值数据。但是有些文件的列标题带有空格,而有些则没有(由不同的人创建)。有些有我不想要的额外列。

例如,一个文件可能有如下标题:

ASomeName; BSomeName; C(someName%) 

而另一个文件头可能是

A Some Name; B Some Name; C(someName%); D some name

在调用“读取”命令之前,如何清除名称中的空格?

#These are the files I have

filenames<-list.files(pattern = "*.txt",recursive = TRUE,full.names = TRUE)%>%as_tibble()

#These are the columns I would like:

colSelect=c("Date","Time","Timestamp" ,"PM2_5(ug/m3)","PM10(ug/m3)","PM01(ug/m3)","Temperature(C)",  "Humidity(%RH)", "CO2(ppm)")

#This is how I read them if they have the same columns

ldf <- vroom::vroom(filenames, col_select = colSelect,delim=";",id = "sensor" )%>%janitor::clean_names()

清理标题脚本

我编写了一个破坏性脚本,它将读取整个文件,清理空格标题,删除文件并重新写入(vroom 有时抱怨无法打开 X 数千个文件)使用相同的文件姓名。不是一种高效的做事方式。

cleanHeaders<-function(filename){
  d<-vroom::vroom(filename,delim=";")%>%janitor::clean_names()
  #print(head(d))
  if (file.exists(filename)) {
    #Delete file if it exists
    file.remove(filename)
  }
  vroom::vroom_write(d,filename,delim = ";")
}

lapply(filenames,cleanHeaders) 
4

1 回答 1

1

fread 的select参数允许整数索引。如果所需的列始终位于同一位置,则您的工作就完成了。

colIndexes = c(1,3,4,7,9,18,21)
data = lapply(filenames, fread, select = colIndexes)

我想 vroom 也有这个功能,但是由于你已经在选择你想要的列,我认为懒惰地评估你的字符列根本没有帮助,所以我建议你坚持使用 data.table。

但是,对于更强大的解决方案,由于您无法控制表的结构:您可以读取每个文件的一行,捕获并清理列名,然后将它们与colSelect向量的干净版本进行匹配。

library(data.table)
library(janitor)
library(purrr)

filenames <- list.files(pattern = "*.txt",
                        recursive = TRUE,
                        full.names = TRUE)

# read the first row of data to capture and clean the column names
clean_col_names <- function(filename){
  colnames(janitor::clean_names(fread(filename, nrow = 1)))
}

clean_column_names <- map(.x = filenames, 
                          .f = clean_col_names)

# clean the colSelect vector
colSelect <- janitor::make_clean_names(c("Date",
                                         "Time",
                                         "Timestamp" ,
                                         "PM2_5(ug/m3)",
                                         "PM10(ug/m3)",
                                         "PM01(ug/m3)",
                                         "Temperature(C)",
                                         "Humidity(%RH)",
                                         "CO2(ppm)"))

# match each set of column names against the clean colSelect
select_indices <- map(.x = clean_column_names, 
                      .f = function(cols) match(colSelect, cols))

# use map2 to read only the matched indexes for each column
data <- purrr::map2(.x = filenames, 
                    .y = select_indices, 
                    ~fread(input = .x, select = .y))
                  

(这里的 purrr 可以很容易地用传统的 lapply 替换,我选择了 purrr 因为它的公式符号更清晰)

于 2021-04-01T15:16:02.780 回答