[R] Importing fixed-width data
James Rome
jamesrome at gmail.com
Wed May 25 17:42:37 CEST 2011
I have a data set where the lines look like:
2011-05-13 00:00:00 EONAAL330 dfa13002516PSCNONA
2011-05-13 00:00:01 EONAAL223 laa13044510AS.NONM
Some lines are missing the field before and after the NON:
2011-05-13 00:00:05 EONBHS229 mia13001621NON
I read them into R using
df = read.fwf(file, widths=c(19,-4,7,3,8,2,1,3,1),
col.names=c("DateTime","Flight","Dest","ArrTime","MsgType","Conf","Runway","Source"),
colClasses=c("POSIXct",NA,"factor","factor","character","factor","factor","factor"))
The documentation for read.fwf says that the data are read into a
dataframe. Yet, I get a list, and the conversions I specified do not
seem to have been obeyed:
> df[1:20,]
DateTime Flight Dest ArrTime MsgType Conf
Runway Source
1 2011-05-13 00:00:00 AAL330 dfa 13002516 PS C NON A
2 2011-05-13 00:00:01 AAL223 laa 13044510 AS . NON M
. . .
> sapply(df, mode)
DateTime Flight Dest ArrTime MsgType Conf
"numeric" "numeric" "numeric" "numeric" "character" "numeric"
Runway Source
"numeric" "numeric"
> dfn = df[!is.na(df$Source),]
> mode(df)
[1] "list"
What am I doing wrong?
Thanks,
Jim Rome
More information about the R-help
mailing list