这就是你想要的吗?
library(tidyverse)
filePattern <- "\\.csv$"
fileList <- list.files(path = "./Csv test/", recursive = FALSE,
pattern = filePattern, full.names = TRUE)
read_file_custom <- function(fileName) {
# skip 6 lines and select only the first 5 columns
dat <- readr::read_csv(file = fileName, skip = 6, col_names = FALSE) %>%
select(., 1:5)
colName <- c("TimeFrame", "Left(squeeze)", "Left(pull)", "Right(squeeze)", "Right(pull)")
names(dat) <- colName
# now read the 3rd and 4th lines & keep only the first 6 columns
indi_info <- readr::read_csv(file = fileName, skip = 2, col_names = TRUE, n_max = 1) %>%
select(., 1:6)
# transfer individual data to dat
dat <- dat %>%
mutate(NAME = indi_info$NAME,
DATE = indi_info$DATE,
TIME = indi_info$TIME,
DEVICE = indi_info$DEVICE,
MODE = indi_info$MODE,
TEST = indi_info$TEST)
return(dat)
}
# Loop through all the files using map_df, read data
# and create a FileName column to store filenames
# Clean up filename: remove file path and extension
# Bind all files together
result <- fileList %>%
purrr::set_names(nm = (basename(.) %>% tools::file_path_sans_ext())) %>%
purrr::map_df(read_file_custom, .id = "FileName")
result
#> # A tibble: 10,460 x 12
#> FileName TimeFrame `Left(squeeze)` `Left(pull)` `Right(squeeze)`
#> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 Arianne-Robill~ 0.0200 -1.00 2.25 -1.25
#> 2 Arianne-Robill~ 0.0400 -1.00 2.25 -1.25
#> 3 Arianne-Robill~ 0.0600 -1.00 2.25 -1.25
#> 4 Arianne-Robill~ 0.0800 -1.00 2.25 -1.25
#> 5 Arianne-Robill~ 0.100 -1.00 2.25 -1.25
#> 6 Arianne-Robill~ 0.120 -1.00 2.00 -1.25
#> 7 Arianne-Robill~ 0.140 -1.00 2.00 -1.00
#> 8 Arianne-Robill~ 0.160 -0.750 2.00 -1.00
#> 9 Arianne-Robill~ 0.180 -0.750 1.75 -0.750
#> 10 Arianne-Robill~ 0.200 -0.750 1.75 -0.750
#> # ... with 10,450 more rows, and 7 more variables: `Right(pull)` <dbl>,
#> # NAME <chr>, DATE <chr>, TIME <time>, DEVICE <chr>, MODE <chr>,
#> # TEST <chr>
创建日期:2018年3月28日
reprex package
(v0.2.0)。