[R] Reading a tab delimted file of varying length using read.table
pradeep.bisht0303 at gmail.com
Sun Jan 17 23:52:25 CET 2016
A Big thanks to everyone to help me solve this problem .
My bad I assumed the file is delimited by tab which it was not . Its a
fixed width file and the code that Uwe gave is just perfect .
It was cleaver to skip the first row since the delimiter cannot be
specified in this case .I added few more things to it and got the desired
Here is the code
url <- "http://data.princeton.edu/wws509/datasets/divorce.dat"
widths <- c(9, 13, 10, 8, 10, 6)
f5 <- read.fwf(url, widths = widths,
skip = 1,
strip.white = TRUE,
colClasses = c("numeric", "character",
"character","character", "double", "character" )
On Sun, Jan 17, 2016 at 4:48 PM, Uwe Ligges <ligges at statistik.tu-dortmund.de
> This is not a tab delimited file (as you apparently assume given the
> code), but a fixed width format, hence I'd try:
> url <- "http://data.princeton.edu/wws509/datasets/divorce.dat"
> widths <- c(9, 13, 10, 8, 10, 6)
> f5 <- read.fwf(url, widths = widths, skip = 1, strip.white = TRUE)
> names(f5) <- as.character(unlist(read.fwf(url, widths = widths,
> strip.white=TRUE, n=1)))
> Not sure why reading it simply with header=TRUE des not work, but no time
> to investiagte this now.
> Uwe Ligges
> On 17.01.2016 16:31, Pradeep Bisht wrote:
>> Hello Experts ,
>> Being a SAS developer I am finding it difficult to perform some of data
>> cleaning in R that are quite easy to perform in SAS .
>> I have been trying to read a .dat file and after a lot of attempts have
>> failed to find a solution . Maybe R doesn't have the functionality right
>> now or I am not looking in the right place . Here is my code .
>> colClasses = c("numeric", "character", "character","character", "double",
>> "character" ) )
>> The error i get i
>> this .
>> Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,
>> scan() expected 'a real', got '912-15yearsNoNo10.546No'
>> Also does read.table always calls scan in background to do its job . If so
>> why use read.table in first place .
>> [[alternative HTML version deleted]]
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> PLEASE do read the posting guide
>> and provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
More information about the R-help