I have a csv file that looks like this (see below first few lines of the data) and I've used this line to import it,
read.csv2('hospital_finance_2008.csv', sep=';', check.names = F)
But it all came in one column only. Also how to get rid of the quotations when the first variable doesn't have them?
Content of .csv:
Estabelecimento - CNES;"Privado com fins lucrativos clientela mista";"Privado com fins lucrativos clientela privada";"Privado sem fins lucrativos clientela mista";"Privado sem fins lucrativos clientela privada";"Público clientela fechada";"Público clientela mista";"Público clientela SUS";"Total"
0000027 CASA DE SAUDE SANTA HELENA;"1";"0";"0";"0";"0";"0";"0";"1"
0000035 HOSPITAL MENDO SAMPAIO;"0";"0";"0";"0";"0";"0";"1";"1"
0000094 MATERNIDADE PADRE GERALDO LEITE BASTOS;"0";"0";"0";"0";"0";"0";"1";"1"
0000183 HOSPITAL SAMARITANO;"0";"1";"0";"0";"0";"0";"0";"1"
0000221 HOSPITAL SAO SEBASTIAO;"0";"1";"0";"0";"0";"0";"0";"1"
0000396 HOSPITAL DAS CLINICAS;"0";"0";"0";"0";"0";"0";"1";"1"
0000418 HOSPITAL AGAMENON MAGALHAES;"0";"0";"0";"0";"0";"0";"1";"1"
0000426 HOSPITAL OTAVIO DE FREITAS;"0";"0";"0";"0";"0";"0";"1";"1"
0000434 IMIP;"0";"0";"1";"0";"0";"0";"0";"1"
0000477 HOSPITAL OSWALDO CRUZ;"0";"0";"0";"0";"0";"0";"1";"1"
0000485 FUNDACAO ALTINO VENTURA;"0";"0";"1";"0";"0";"0";"0";"1"
CodePudding user response:
There appears to be a line break missing in first line of .csv. You could use readLines
and gsub
the linebreak into first line. Also there are weird quotes, you could set them to nothing using quote=''
and delete them using gsub
thereafter. Finally delete the commas in last column.
rl <- readLines('~/Downloads/hospital_finance_2008.csv')
rl[1] <- gsub('0000027', '\n0000027', rl[1])
dat <- read.csv2(text=rl, sep=';', quote='')
dat[] <- lapply(dat, gsub, pat='"', rep='')
dat <- type.convert(dat, as.is=TRUE) ## convert to numeric
dat[[length(dat)]] <- as.numeric(gsub(',', '', dat[[length(dat)]]))
head(dat)
# X.Estabelecimento...CNES X..Privado.com.fins.lucrativos....clientela.mista..
# 1 0000027 CASA DE SAUDE SANTA HELENA 1
# 2 0000035 HOSPITAL MENDO SAMPAIO 0
# 3 0000094 MATERNIDADE PADRE GERALDO LEITE BASTOS 0
# 4 0000183 HOSPITAL SAMARITANO 0
# 5 0000221 HOSPITAL SAO SEBASTIAO 0
# 6 0000396 HOSPITAL DAS CLINICAS 0
# X..Privado.com.fins.lucrativos....clientela.privada.. X..Privado.sem.fins.lucrativos....clientela.mista..
# 1 0 0
# 2 0 0
# 3 0 0
# 4 1 0
# 5 1 0
# 6 0 0
# X..Privado.sem.fins.lucrativos....clientela.privada.. X..P.fa.blico....clientela.fechada.. X..P.fa.blico....clientela.mista..
# 1 0 0 0
# 2 0 0 0
# 3 0 0 0
# 4 0 0 0
# 5 0 0 0
# 6 0 0 0
# X..P.fa.blico....clientela.SUS.. X..Total...
# 1 0 1
# 2 1 1
# 3 1 1
# 4 0 1
# 5 0 1
# 6 1 1