在读取具有不同部分的复杂CSV时出现问题

时间:2019-09-06 08:02:48

标签: r csv

我正在尝试将复杂的csv文件解析为数据帧。 CSV包含我要添加为固定值列的不同标题和小节。 这是我的csv文件(filecomplex.csv

在下面显示我的信息或主队和客队的文件时,主场始终是第一组,第二是客队。

,"REPORT TITLE"
,"REPORT SUBTITLE"

,,Total,First,Second
,"Game time",91:03,45:03,45:56

Num,Player,Minutes,TD,Standing,Walking,Jogging,Run,"H S Run",Sprinting,"NoRuns","Speed","AvSpeed"
349,"Anon",91:03,10426.29,11.11,3643.31,4043.68,1946.43,654.45,127.31,63,33.67,6.64
487,"Erers",91:03,9641.31,20.19,3659.56,3700.56,1388.04,610.73,262.23,72,30.67,6.14


Num,Player,Minutes,TD,Standing,Walking,Jogging,Run,"H S Run",Sprinting,"NoRuns","Speed","AvSpeed"
732,"Vani",91:03,8564.36,22.54,4176.19,2698.49,1240.93,298.17,128.04,46,31.85,5.46
643,"Fabes",91:03,11167.94,17.54,3599.27,5176.16,1968.43,304.87,101.68,37,28.19,7.12
217,"Fisa",85:26,10307.61,19.88,2829.12,4861.7,1719.74,647.05,230.12,76,30.79,7.24
706,"Casi",91:03,4135.92,37.53,3211.54,814.59,63.47,8.8,0,1,22.43,2.64



我尝试过两种方式读取文件

dl <-readLines("filecomplex.csv")

# below line removes empty lines not sure if this helps
df <-read.csv("filecomplex.csv",header=FALSE, stringsAsFactors=FALSE)

我的最终要求是拥有与此类似的数据框

"TITLE","SUBTITLE",Total,First,Second,Num,Player,Minutes,TD,Standing,Walking,Jogging,Run,"H S Run",Sprinting,"NoRuns","Speed","AvSpeed"
"REPORT TITLE","REPORT SUBTITLE",91:03,45:03,45:56,HOME,349,"Anon",91:03,10426.29,11.11,3643.31,4043.68,1946.43,654.45,127.31,63,33.67,6.64
"REPORT TITLE","REPORT SUBTITLE",91:03,45:03,45:56,HOME,487,"Erers",91:03,9641.31,20.19,3659.56,3700.56,1388.04,610.73,262.23,72,30.67,6.14
"REPORT TITLE","REPORT SUBTITLE",91:03,45:03,45:56,AWAY,732,"Vani",91:03,8564.36,22.54,4176.19,2698.49,1240.93,298.17,128.04,46,31.85,5.46
"REPORT TITLE","REPORT SUBTITLE",91:03,45:03,45:56,AWAY,643,"Fabes",91:03,11167.94,17.54,3599.27,5176.16,1968.43,304.87,101.68,37,28.19,7.12
"REPORT TITLE","REPORT SUBTITLE",91:03,45:03,45:56,AWAY,217,"Fisa",85:26,10307.61,19.88,2829.12,4861.7,1719.74,647.05,230.12,76,30.79,7.24
"REPORT TITLE","REPORT SUBTITLE",91:03,45:03,45:56,AWAY,706,"Casi",91:03,4135.92,37.53,3211.54,814.59,63.47,8.8,0,1,22.43,2.64

预先感谢

2 个答案:

答案 0 :(得分:1)

假定前四行的信息始终位于同一单元格中,并且玩家分节始终从第7行开始:

library(readr)
library(dplyr)

game_info <- read_csv("filecomplex.csv", col_names = paste0("X", 1:5), n_max = 4)

df <- read_csv("filecomplex.csv", skip = 6) %>%
  mutate(TITLE = game_info$X2[1],
         SUBTITLE = game_info$X2[2],
         Total = game_info$X3[4],
         First = game_info$X4[4],
         Second = game_info$X5[4]) %>%
  mutate(Team = ifelse(cumsum(Num == "Num") == 0, "HOME", "AWAY")) %>%
  filter(Num != "Num")  %>%
  select(TITLE:Team, everything())

df
# A tibble: 6 x 19
  TITLE      SUBTITLE     Total First Second Team  Num   Player Minutes TD     Standing Walking Jogging Run    `H S Run` Sprinting NoRuns Speed AvSpeed
  <chr>      <chr>        <chr> <chr> <chr>  <chr> <chr> <chr>  <chr>   <chr>  <chr>    <chr>   <chr>   <chr>  <chr>     <chr>     <chr>  <chr> <chr>  
1 REPORT TI~ REPORT SUBT~ 91:03 45:03 45:56  HOME  349   Anon   91:03   10426~ 11.11    3643.31 4043.68 1946.~ 654.45    127.31    63     33.67 6.64   
2 REPORT TI~ REPORT SUBT~ 91:03 45:03 45:56  HOME  487   Erers  91:03   9641.~ 20.19    3659.56 3700.56 1388.~ 610.73    262.23    72     30.67 6.14   
3 REPORT TI~ REPORT SUBT~ 91:03 45:03 45:56  AWAY  732   Vani   91:03   8564.~ 22.54    4176.19 2698.49 1240.~ 298.17    128.04    46     31.85 5.46   
4 REPORT TI~ REPORT SUBT~ 91:03 45:03 45:56  AWAY  643   Fabes  91:03   11167~ 17.54    3599.27 5176.16 1968.~ 304.87    101.68    37     28.19 7.12   
5 REPORT TI~ REPORT SUBT~ 91:03 45:03 45:56  AWAY  217   Fisa   85:26   10307~ 19.88    2829.12 4861.7  1719.~ 647.05    230.12    76     30.79 7.24   
6 REPORT TI~ REPORT SUBT~ 91:03 45:03 45:56  AWAY  706   Casi   91:03   4135.~ 37.53    3211.54 814.59  63.47  8.8       0         1      22.43 2.64   

答案 1 :(得分:0)

可以提供更多灵活性的替代解决方案:

library(dplyr)
library(readr)

df <- read_csv("filecomplex.csv", col_names = paste0("X", 1:20)) %>%
  select_if(~!all(is.na(.))) %>%
  rename_all(function(x) filter(., X1 == "Num") %>% slice(1)) %>%
  mutate(TITLE = .[[1,2]],
         SUBTITLE = .[[2,2]],
         Total = .[[4,3]],
         First = .[[4,4]],
         Second = .[[4,5]]) %>%
  mutate(group = cumsum(!is.na(Num) & Num == "Num"),
         Team = case_when(group == 1 ~ "HOME",
                          group == 2 ~ "AWAY")) %>%
  filter(group %in% 1:2, Num != "Num") %>%
  select(TITLE:Team, everything(), -group)

df
# A tibble: 6 x 19
  TITLE        SUBTITLE        Total First Second Team  Num   Player Minutes TD       Standing Walking Jogging Run    `H S Run` Sprinting NoRuns Speed AvSpeed
  <chr>        <chr>           <chr> <chr> <chr>  <chr> <chr> <chr>  <chr>   <chr>    <chr>    <chr>   <chr>   <chr>  <chr>     <chr>     <chr>  <chr> <chr>  
1 REPORT TITLE REPORT SUBTITLE 91:03 45:03 45:56  HOME  349   Anon   91:03   10426.29 11.11    3643.31 4043.68 1946.~ 654.45    127.31    63     33.67 6.64   
2 REPORT TITLE REPORT SUBTITLE 91:03 45:03 45:56  HOME  487   Erers  91:03   9641.31  20.19    3659.56 3700.56 1388.~ 610.73    262.23    72     30.67 6.14   
3 REPORT TITLE REPORT SUBTITLE 91:03 45:03 45:56  AWAY  732   Vani   91:03   8564.36  22.54    4176.19 2698.49 1240.~ 298.17    128.04    46     31.85 5.46   
4 REPORT TITLE REPORT SUBTITLE 91:03 45:03 45:56  AWAY  643   Fabes  91:03   11167.94 17.54    3599.27 5176.16 1968.~ 304.87    101.68    37     28.19 7.12   
5 REPORT TITLE REPORT SUBTITLE 91:03 45:03 45:56  AWAY  217   Fisa   85:26   10307.61 19.88    2829.12 4861.7  1719.~ 647.05    230.12    76     30.79 7.24   
6 REPORT TITLE REPORT SUBTITLE 91:03 45:03 45:56  AWAY  706   Casi   91:03   4135.92  37.53    3211.54 814.59  63.47  8.8       0         1      22.43 2.64