在将csv文件保存到R中的环境之前对其进行转换

在将csv文件保存到R中的环境之前对其进行转换,r,R,我正在处理多个长格式的csv文件。每个文件的列数不同,但行数相同。我试图读取所有文件并将它们合并到一个df中,但我做不到。 到目前为止,我使用此代码分别读取每个文件: try <- read.table('input/SMPS/new_format/COALA_SMPS_20200218.txt', #set the file to read sep = ',', #separator header = F, # d

我正在处理多个长格式的csv文件。每个文件的列数不同,但行数相同。我试图读取所有文件并将它们合并到一个df中,但我做不到。 到目前为止,我使用此代码分别读取每个文件:

try <- read.table('input/SMPS/new_format/COALA_SMPS_20200218.txt', #set the file to read
                  sep = ',',  #separator
                  header = F, # do not read the header
                  skip = 17, # skip 17 firdt lines of information 
                  fill = T) %>% #fill all empty spaces in the df
        t()%>%                  #transpose the data
        data.frame()%>%         #make it a df
        select(1:196)           #select the useful data
try%#填充df中的所有空格
t()%>%#转换数据
data.frame()%>%#将其设为df
选择(1:196)#选择有用的数据
我的计划是使用类似于此代码的东西,但我不知道在哪里包含转置函数才能使其工作

smps_files_new <- list.files(pattern = '*.txt',path = 'input/SMPS/new_format/')#Change the path where the files are located
myfiles <-do.call("rbind",  ##Apply the bind to the files
        lapply(smps_files_new, ##call the list
               function(x)  ##apply the next function
                 read.csv(paste("input/SMPS/new_format/", x, sep=''),sep = ',',  #separator
                          header = F, # do not read the header
                          skip = 17, # skip 17 first lines of information 
                          stringsAsFactors = F,
                          fill = T))) ##

smps\u文件\u new在
lappy
中使用与单个文件相同的代码:

do.call(rbind,  ##Apply the bind to the files
    lapply(smps_files_new, ##call the list
           function(x)  ##apply the next function
             read.csv(paste("input/SMPS/new_format/", x, sep=''),sep = ',',  
                      header = F, # do not read the header
                      skip = 17, # skip 17 first lines of information 
                      stringsAsFactors = FALSE,
                      fill = TRUE) %>%
                      t()%>%        
                      data.frame()%>%
                      select(1:196)))
另一种方法是使用
purrr::map_df
map_dfr
而不是
lappy
+
do.call(rbind

purrr::map_df(smps_files_new, 
           function(x)  
             read.csv(paste("input/SMPS/new_format/", x, sep=''),sep = ',',  
                      header = F, 
                      skip = 17, 
                      stringsAsFactors = FALSE,
                      fill = TRUE) %>%
                      t()%>%        
                      data.frame()%>%
                      select(1:196)))