Home > Mobile >  R read csv with ="" formatting
R read csv with ="" formatting

Time:02-27

I have a csv where each value is wrapped in a quote with an equals sign before it, like so:

="#",="NAME",="SM",="SRM",="SNM",="TMG",="PIL",="TIP",="TIZ"
="1",="A.Aaronsen",="0",="0",="0",="0",="0",="0",="0"
="2",="J.Jeff",="0",="0",="0",="0",="3",="0",="0"
="4",="C.Corr",="0",="0",="0",="0",="1",="0",="0"
="5",="F.Taylor",="0",="0",="0",="0",="2",="0",="0"
="6",="J.Morris",="0",="0",="0",="0",="3",="0",="0"

I can't work out how to read this in so that I just have the values and the columns are of the appropriate type. The first column is the col names.

CodePudding user response:

You can read the file with readLines, remove the = symbol and read it with read.csv.

read.csv(text = gsub('=', '', readLines('data.csv'), fixed = TRUE))

Testing this as text -

text <- '="#",="NAME",="SM",="SRM",="SNM",="TMG",="PIL",="TIP",="TIZ"
="1",="A.Aaronsen",="0",="0",="0",="0",="0",="0",="0"
="2",="J.Jeff",="0",="0",="0",="0",="3",="0",="0"
="4",="C.Corr",="0",="0",="0",="0",="1",="0",="0"
="5",="F.Taylor",="0",="0",="0",="0",="2",="0",="0"
="6",="J.Morris",="0",="0",="0",="0",="3",="0",="0"'

read.csv(text = gsub('=', '', text, fixed = TRUE))

#  X.       NAME SM SRM SNM TMG PIL TIP TIZ
#1  1 A.Aaronsen  0   0   0   0   0   0   0
#2  2     J.Jeff  0   0   0   0   3   0   0
#3  4     C.Corr  0   0   0   0   1   0   0
#4  5   F.Taylor  0   0   0   0   2   0   0
#5  6   J.Morris  0   0   0   0   3   0   0
  • Related