Read csv file into R that contains brackets at at the start and end of each line
csv, r
Solution
I'd probably go the `readLines` route, since the file needs to be manipulated first. Then you can still use the `text` argument in `read.csv/table`
> writeLines(c("(abc,123)", "(def,456)", "(ghi,789)"), "yourfile.txt")
## put your data in a file
> txt <- gsub("[()]", "", readLines("yourfile.txt"))
> read.csv(text = txt, header = FALSE)
# V1 V2
# 1 abc 123
# 2 def 456
# 3 ghi 789
or
> read.table(text = txt, sep = ",")
# V1 V2
# 1 abc 123
# 2 def 456
# 3 ghi 789
Problem
I have a text file that looks as follows ``` (abc,123) (def,456) (ghi,789) ... ``` In R, I would like to read this file as a csv. Therefore I need to get rid of the opening and closing brackets at the end of the lines. Do you have an idea how to achieve that? Reading the file, removing the brackets and writing to a temporary file should be avoided if possible.