Reputation: 1162
I am using readLines to read the first 4 lines of csv files to determine what the headers are so I can concatenate files with similar headers. readLines produces difficult output that looks like this (this is only the first line as an example):
output <- readLines(one_min_file, n = 4)
[1] "\"TOA5\",\"Shelton 2SW\",\"CR1000\",\"62704\",\"CR1000.Std.27\",\"CPU:0044-20201101.CR1\",\"35516\",\"OneMinute\""
How do I parse this into a text array or something that looks like this, or the same array that this produces?
c('TOA5','Shelton 2SW','CR1000','62704','CR1000.Std.27','CPU:0044-20201101.CR1','35516','OneMinute')
EDIT:
dput(readLines(one_min_files[h1], n = 1))
"\"TOA5\",\"Shelton 2SW\",\"CR1000\",\"62704\",\"CR1000.Std.27\",\"CPU:0044-20201101.CR1\",\"35516\",\"OneMinute\""
Upvotes: 0
Views: 45
Reputation: 17754
Try removing double quotes and splitting by ,
:
(output <- c("\"TOA5\",\"Shelton 2SW\",\"CR1000\",\"62704\",\"CR1000.Std.27\",\"CPU:0044-20201101.CR1\",\"35516\",\"OneMinute\""))
#> [1] "\"TOA5\",\"Shelton 2SW\",\"CR1000\",\"62704\",\"CR1000.Std.27\",\"CPU:0044-20201101.CR1\",\"35516\",\"OneMinute\""
gsub('"', '', output, fixed = TRUE) |> strsplit(",") |> unlist()
#> [1] "TOA5" "Shelton 2SW" "CR1000"
#> [4] "62704" "CR1000.Std.27" "CPU:0044-20201101.CR1"
#> [7] "35516" "OneMinute"
Or parse the line with read.csv()
:
read.csv(text = output, header = FALSE)[1,] |> unlist() |> unname()
#> [1] "TOA5" "Shelton 2SW" "CR1000"
#> [4] "62704" "CR1000.Std.27" "CPU:0044-20201101.CR1"
#> [7] "35516" "OneMinute"
Created on 2023-06-21 with reprex v2.0.2
Upvotes: 0