Транспонирование CSV-файлов перед сохранением их в среде в R - PullRequest
0 голосов
/ 06 апреля 2020

Я работаю с несколькими 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

Я планировал использовать нечто похожее на этот код, но я не знаю, где включить функцию транспонирования, чтобы он работал.

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))) ##

1 Ответ

1 голос
/ 06 апреля 2020

Используйте тот же код в lapply, который вы использовали для отдельных файлов:

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 вместо lapply + 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)))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...