Reading multiple fixed width format records per case with read.fwf and read_fwf

110 views
Skip to first unread message

Bob

unread,
Jun 16, 2015, 10:48:46 AM6/16/15
to manip...@googlegroups.com
Hi All,

When reading multiple fixed-width-format records per case, you can use read.fwf and give it a list that contains the variable widths. Each component of the list is a vector of variables widths for a given record. The number of components in the list is what tells read.fwf how many records each case has. For example, this file contains 8 lines which I can read as 4 cases:

011f1151
022f2141
031f2243
042 31 3
051m3524
062m5455
071m5344
082m4555

using this read.fwf code:

> read.fwf(
+   file  = "mydataFWF.txt",
+   width = list(c( 2, 1, 1, 1, 1, 1, 1),
+                c(-2,-1,-1, 1, 1, 1, 1)),
+   col.names  =   
+     c("id","workshop", "gender",
+       "q1","q2","q3","q4",
+       "q5","q6","q7","q8"),
+   row.names  = "id",
+   na.strings = "",
+   fill = TRUE,
+   strip.white = TRUE )

  workshop gender q1 q2 q3 q4 q5 q6 q7 q8
1        1      f  1  1  5  1  2  1  4  1
3        1      f  2  2  4  3  3  1 NA  3
5        1      m  3  5  2  4  5  4  5  5
7        1      m  5  3  4  4  4  5  5  5

The read_fwf help file doesn't address multiple records, but I've tried to get the same result using the same list approach. I'm obviously not understanding how to specify the multiple records correctly:

> read_fwf("mydataFWF.txt", 
+          list(
+            fwf_positions(
+              c(1,3,4,5,6,7,8),
+              c(2,3,4,5,6,7,8),
+              col_names = c("id","workshop","gender","q1","q2","q3","q4")),
+            fwf_positions(
+              c(5,6,7,8),
+              c(5,6,7,8),
+              col_names = c("q5","q6","q7","q8"))
+            )
+          )
Error: not compatible with requested type

What am I doing wrong?

Thanks,
Bob




Hadley Wickham

unread,
Jun 16, 2015, 10:54:59 AM6/16/15
to Bob, manipulatr
That's not supported by read_fwf.
Hadley
> --
> You received this message because you are subscribed to the Google Groups
> "manipulatr" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to manipulatr+...@googlegroups.com.
> To post to this group, send email to manip...@googlegroups.com.
> Visit this group at http://groups.google.com/group/manipulatr.
> For more options, visit https://groups.google.com/d/optout.



--
http://had.co.nz/

Bob

unread,
Jun 16, 2015, 1:54:21 PM6/16/15
to manip...@googlegroups.com, muen...@tennessee.edu
I'm not sure I've ever seen a FWF file with only one record per case. I guess FWF is nowhere near as prevalent as they used to be. I mostly see them in data archives like ICPSR. For anyone following this thread, such archives often provide SAS input statements that describe the multi-record format. The SAScii package can covert such SAS inputs into read.fwf format very easily. Thanks, Bob
Reply all
Reply to author
Forward
0 new messages