一年中有两个数字变为逗号

时间:2017-10-06 20:05:22

标签: r

我的最终目的只是保留时间和专注的列,但在我跳过顶部的所有行之前,我想保留日期“9/12/2017”(我稍后将添加到时间列)。

有一个恼人的“评论样本1:”最后,我需要摆脱它。最大的问题是当我试图保存日期时,它没有出现“9/12/2017”,而是“9/12/17 ,,”。我不知道为什么会这样,最后可能会受到评论线的影响,一年中有两个数字变成了逗号。

Sample File E:\CPC 091217\091217CPC3.C07
Model   3007
Sample #    1
Start Date  9/12/2017
Start Time  10:29:57
Sample Length   4:14:37
Averaging Interval (secs)   1
Title   
Instrument ID   3007-03160001 3.1
Instrument Errors   None

Time    Concentration (#/cm³)
10:29:58    4224
10:29:59    97323
10:30:00    14590
10:30:09    73716
10:30:10    70779
10:30:11    60642
10:30:12    56171

.........
Comment for Sample 1:

我尝试的是

cpcFile = choose.files(default = "", caption = "           

                   SELECT  THE  CPC.csv  FILE   ",
                   multi = FALSE, filters = Filters[c("txt", "All"),],
                   index = nrow(Filters))

cpcData <- read.csv(cpcFile, skip = 16)

#helps get rid of the comment line,it works
cpcData<-cpcData[- grep("Comment", cpcData$Time),]

#however,the P6 shows "9/12/17,,"instead of " "9/12/2017"
K=readLines(cpcFile, n = 5) # read first 5 lines
header=K[[5]]                # read only the date and time
P6= substr(header, 12, 25)

期望:

 Time            Concentration (#/cm³)
10:29:58 9/12/2017     4224
10:29:59 9/12/2017     97323
10:30:00 9/12/2017     14590
10:30:09 9/12/2017     73716
 ........

和P6应该是9/12/2017首先,谢谢!

1 个答案:

答案 0 :(得分:0)

我建议回避解析文件中的日期问题,而只是将其解析出文件名,假设它们始终具有日期。

然后,您可以使用readr::read_delimreadr::readfwf,具体取决于您的文件的确切分界,并使用skip参数跳过标题。页脚可能只是无法解析,但你可以检查并删除它,如果它没有。您可以在加载数据框后使用string:str_extract并在文件名上使用正则表达式来设置日期列。