I have a sparse data set, one whose number of columns vary in length, in a csv format. Here is a sample of the file text.
12223, University
12227, bridge, Sky
12828, Sunset
13801, Ground
14853, Tranceamerica
14854, San Francisco
15595, shibuya, Shrine
16126, fog, San Francisco
16520, California, ocean, summer, golden gate, beach, San Francisco
When I use
read.csv("data.txt", header = F)
R will interpret the data set as having 3 columns because the size is determined from the first 5 rows. Is there anyway to force r to put the data in more columns?
Deep in the
?read.table
documentation there is the following:Therefore, let's define
col.names
to be length X (where X is the max number of fields in your dataset), and setfill = TRUE
:If the maximum number of fields is unknown, you can use the nifty utility function
count.fields
(which I found in theread.table
example code):Possibly helpful related reading: Only read limited number of columns in R