R:如何读取和重新排列多个文件

时间:2019-07-09 11:59:38

标签: r multiple-files

所以我要做的是将多个文件读入一个小标题或数据帧。我的文件如下所示。对于一个ID,可以有多个具有不同度量值的文件

File_name:test01.csv
ID:1243
View:d
Unit:mm
length:555

File_name:test02.csv
ID:1243
View:v
Unit:mm
volume:111
width:333

File_name:  test03.csv
ID:1235
View:l
Unit:mm
length:666
height:444
width:222

最后我想要的是这样的

ID, Unit, Value,    Measure,    
1243,   mm, 555,    length
1243,   mm, 111,    volume
1243,   mm, 333,    width
1235,   mm, 666,    length  
1235,   mm, 444,    height
1235,   mm, 222,    width

到目前为止,我尝试的是制作一个文件列表并将所有文件读入一个表中

    csv_list %>%
  map_df(~ read.table(.,skip = 1, sep = ':')) -> data

    data_csv = ldply(xls_list, read.table, sep = ':', fill = T, header = F, skip = 1)

我得到了一张长桌子,一个文件一个接一个,但是我不确定如何重新安排它。

ID       :  1243
View     :  d
Unit     :  mm
length   :  555
ID       :  1243
View     :  v
Unit     :  mm
volume   :  111
width    :  333
ID       :  1235
View     :  l
Unit     :  mm
length   :  666
height   :  444
width    :  222

我想到的另一个想法是仅通过循环分别读取每个文件,但这会花费很长时间

#Complete Code
path = "D:/Scripts/R_projects/Pictures"
setwd(path)
xls_list = list.files(path, pattern = ".csv", full.names = T)

data_csv = ldply(xls_list, read.table, sep = ':', fill = T, header = F, skip = 1)

#or

xls_list %>%
  map_df(~ read.table(.,skip = 1, sep = ':')) -> data
glimpse(data)

1 个答案:

答案 0 :(得分:0)

您可能想要类似以下内容,其中我使用read_delim中的readr的map_df读取并合并文件,然后使用tidyr的spread将数据重新排列为感觉:

library(tidyverse)

map_df(list.files(pattern = "csv$"),
       read_delim,
       delim = ":",
       col_names = F,
       trim_ws = T,
       .id = "df" # <- necessary for spread
       ) %>%
    spread(X1, X2, convert = T) # <- spread and coerce

哪个返回:

# A tibble: 3 x 9
  df    File_name  height    ID length Unit  View  volume width
  <chr> <chr>       <int> <int>  <int> <chr> <chr>  <int> <int>
1 1     test01.csv     NA  1243    555 mm    d         NA    NA
2 2     test02.csv     NA  1243     NA mm    v        111   333
3 3     test03.csv    444  1235    666 mm    l         NA   222

如果需要整齐(长)格式,只需添加gather

map_df(list.files(pattern = "csv$"),
       read_delim,
       delim = ":",
       col_names = F,
       trim_ws = T,
       .id = "df"
       ) %>%
    spread(X1, X2, convert = T) %>% 
    gather(measure_name, measure_val, height, length, volume, width)

哪个返回:

# A tibble: 12 x 7
   df    File_name     ID Unit  View  measure_name measure_val
   <chr> <chr>      <int> <chr> <chr> <chr>              <int>
 1 1     test01.csv  1243 mm    d     height                NA
 2 2     test02.csv  1243 mm    v     height                NA
 3 3     test03.csv  1235 mm    l     height               444
 4 1     test01.csv  1243 mm    d     length               555
 5 2     test02.csv  1243 mm    v     length                NA
 6 3     test03.csv  1235 mm    l     length               666
 7 1     test01.csv  1243 mm    d     volume                NA
 8 2     test02.csv  1243 mm    v     volume               111
 9 3     test03.csv  1235 mm    l     volume                NA
10 1     test01.csv  1243 mm    d     width                 NA
11 2     test02.csv  1243 mm    v     width                333
12 3     test03.csv  1235 mm    l     width                222