[R] Importing fixed-width data

Ian Gow iandgow at gmail.com
Wed May 25 17:47:05 CEST 2011


Everything looks OK. Does this help?

> test <- 
>data.frame(alpha=as.factor(c("A","A","B","B","C")),number=c(1,2,3,4,5))
> mode(test)
[1] "list"
> class(test)
[1] "data.frame"
> sapply(test, mode)

    alpha    number
"numeric" "numeric"
> sapply(test, class)
    alpha    number
 "factor" "numeric"


On 5/25/11 10:42 AM, "James Rome" <jamesrome at gmail.com> wrote:

>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
>
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list