I have a tab delimited text file which contains string names that include whitespace. It is the output of a python routine. The text file looks like this, with hidden '\t' characters.

Mother's Day    Breakfast in Bed    Breakfast in Bed    Arkadia Tea Chai Spice 240g 6.69    False
Mother's Day    Breakfast in Bed    Breakfast in Bed    Coles Crumpets 6 pack   2.29    False
Mother's Day    Entertainment   Entertainment   4 Ingredients Book 1 by Kim McCosker & Rachael Bermingham 1 each    14.00   False
Pantry  Baking  Cake Mix    White Wings Red Velvet Cake 425g    4.61    False

As the file is tab delimited, excel - for example - recognises that there are six columns - which is as i desire.

I wish for it to be in a table in R that has six columns, with the columns treated as factors.

I have read the docs, and it seems that specifying the delimiter ought to work -- however i cannot get this the following to work:

fullSurvey <- read.table(file="C:/1.txt", check.names=FALSE, header=FALSE, sep="\t", stringsAsFactors=TRUE)

it returns:

Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  : 
line 2 did not have 15 elements

Is this possible in R?

有帮助吗?

解决方案

(Moving comments to an answer to hopefully mark this as resolved).

Generally, when your input is tab delimited, you can try using read.delim, which, like read.csv for csv files, would have generally sensible defaults for read.table.

When your data includes apostrophes, single quotes, or double quotes, you may need to specify the value to use for quoting characters, or specify quote = "" to preserve your embedded quotes.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top