如何在R和未对齐的行和列中导入CSV文件?

时间:2019-04-25 09:46:40

标签: r csv dataframe rows

我正在尝试将建模结果导入R。该模型在每个时间步中吐出68个条目。但是,.txt文件的格式(空格分隔)已关闭。前5个条目放置在第一行,然后每1-6个条目从新行开始。 因此,将其作为CSV导入将导致一个数据列具有5列16行,而不是具有68列的单行。

定义列标题并使用fill选项将导致一个包含68列的数据帧,其中第5位之后的所有内容均填充有NA。

col.names = paste0("V",seq_len(68)))

我觉得必须有一个选项可以按固定的行数对这个时间序列进行排序。我想念什么?

数据如下:

        2013           1           1  0.0000000E+00  0.7999797    
  0.0000000E+00  0.7976342      0.7976342       4.600000      0.0000000E+00
   5.400000      0.9950000      0.0000000E+00   969.0231      0.0000000E+00
  0.0000000E+00
   134.2833      0.0000000E+00  7.5260596E-03  0.0000000E+00  0.0000000E+00
  0.2500000       453.5565       397.1019     -1.1351937E-04  1.1351937E-04
   37.85871       134.2833     -8.6918109E-05  0.3779435      0.8803711    
  0.0000000E+00 -1.1351937E-04  1.1351937E-04  -180.3156      1.0273202E-03
  0.0000000E+00  0.0000000E+00  0.0000000E+00  0.6856970      0.1142827    
  0.0000000E+00  0.0000000E+00   1.000000       1.000000      0.0000000E+00
  0.0000000E+00  -165.6710      0.0000000E+00   5.400000       77.18394    
  0.0000000E+00  0.0000000E+00  0.0000000E+00  0.0000000E+00  0.0000000E+00
  0.0000000E+00  0.0000000E+00  0.0000000E+00  0.0000000E+00  0.0000000E+00
  0.0000000E+00  0.0000000E+00  0.0000000E+00  0.0000000E+00  0.0000000E+00
  0.0000000E+00  1.1351937E-04  0.0000000E+00
  0.0000000E+00

1 个答案:

答案 0 :(得分:0)

不导入为csv。将其读取为单个字符串,然后进行解析:

library(readr)
library(dplyr)
tx <- read_file('        2013           1           1  0.0000000E+00  0.7999797    
  0.0000000E+00  0.7976342      0.7976342       4.600000      0.0000000E+00
   5.400000      0.9950000      0.0000000E+00   969.0231      0.0000000E+00
  0.0000000E+00
   134.2833      0.0000000E+00  7.5260596E-03  0.0000000E+00  0.0000000E+00
  0.2500000       453.5565       397.1019     -1.1351937E-04  1.1351937E-04
   37.85871       134.2833     -8.6918109E-05  0.3779435      0.8803711    
  0.0000000E+00 -1.1351937E-04  1.1351937E-04  -180.3156      1.0273202E-03
  0.0000000E+00  0.0000000E+00  0.0000000E+00  0.6856970      0.1142827    
  0.0000000E+00  0.0000000E+00   1.000000       1.000000      0.0000000E+00
  0.0000000E+00  -165.6710      0.0000000E+00   5.400000       77.18394    
  0.0000000E+00  0.0000000E+00  0.0000000E+00  0.0000000E+00  0.0000000E+00
  0.0000000E+00  0.0000000E+00  0.0000000E+00  0.0000000E+00  0.0000000E+00
  0.0000000E+00  0.0000000E+00  0.0000000E+00  0.0000000E+00  0.0000000E+00
  0.0000000E+00  1.1351937E-04  0.0000000E+00
  0.0000000E+00') %>%  # Just put the filename here instead
  trimws()%>%
  strsplit('\\s+') %>%
  unlist()%>%
  as.numeric()

# tx 
# [1]  2.013000e+03  1.000000e+00  1.000000e+00  0.000000e+00  7.999797e-01  0.000000e+00  7.976342e-01  7.976342e-01  4.600000e+00
# [10]  0.000000e+00  5.400000e+00  9.950000e-01  0.000000e+00  9.690231e+02  0.000000e+00  0.000000e+00  1.342833e+02  0.000000e+00
# [19]  7.526060e-03  0.000000e+00  0.000000e+00  2.500000e-01  4.535565e+02  3.971019e+02 -1.135194e-04  1.135194e-04  3.785871e+01
# [28]  1.342833e+02 -8.691811e-05  3.779435e-01  8.803711e-01  0.000000e+00 -1.135194e-04  1.135194e-04 -1.803156e+02  1.027320e-03
# [37]  0.000000e+00  0.000000e+00  0.000000e+00  6.856970e-01  1.142827e-01  0.000000e+00  0.000000e+00  1.000000e+00  1.000000e+00
# [46]  0.000000e+00  0.000000e+00 -1.656710e+02  0.000000e+00  5.400000e+00  7.718394e+01  0.000000e+00  0.000000e+00  0.000000e+00
# [55]  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00
# [64]  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  1.135194e-04  0.000000e+00  0.000000e+00