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