Y Zhang Y Zhang - 3 months ago 10
R Question

textconnection yielding additional space [R]

I have a string like the content of a csv file:

'"number 1", "number 2"'
, and I want to parse it.

I found
textconnection
can convert string into file handle. Parsing worked using it. But it generates some mysterious space in my content. Any one konw why and how to fix? Thanks

con = textConnection('"number 1", "number 2"')
dat = read.csv(con, header = F, stringsAsFactors = F)
close(con)

dat
# V1 V2
# 1 number 1 number 2

dat[1,1]
# [1] "number 1"

dat[1,2]
# [1] " number 2" --- why there's a space???

Answer

The space is already there, so it's not really a mystery. Due to the way read.csv() parses the file, the space between the comma and the the quote is left behind. You can resolve this by using strip.white = TRUE in your call to read.csv().

con = textConnection('"number 1", "number 2"')
dat = read.csv(con, header = F, stringsAsFactors = F, strip.white = TRUE)
dat[1,2]
# [1] "number 2"

By the way, you don't need to use textConnection() in read.csv(). If you have a string, you can use the text argument of read.csv() to pass it in.