Skip to content
Prev 285224 / 398502 Next

Getting codebook data into R

Just to follow up on Dan's code - once you have a data.frame listing column
positions, then it's just a couple steps to download the file...

x <- data.frame(name=c('caseid', 'nbrnaliv', 'babysex',
'birthwgt_lb','birthwgt_oz','prglength',
'outcome', 'birthord',  'agepreg',  'finalwgt'),
begin = c(1, 22, 56, 57, 59, 275, 277, 278, 284, 423),
end =  c(12, 22, 56, 58, 60, 276, 277, 279, 287, 440)
)


x$width <- x$end - x$begin + 1
x$skip <-  (-c(x$begin[-1]-x$end[-nrow(x)]-1,0))

widths <- c(t(x[,4:5]))
widths <- widths[widths!=0]

ftp<-
"ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/Datasets/NSFG/2002FemPreg.dat"
# drop the n=10 option to get all lines
y<- read.fwf(ftp, widths, n=10)
names(y) <- x$name
y
   caseid nbrnaliv babysex birthwgt_lb birthwgt_oz prglength outcome
birthord agepreg  finalwgt
1       1        1       1           8          13        39       1       
1    3316  6448.271
2       1        1       2           7          14        39       1       
2    3925  6448.271
3       2        3       1           9           2        39       1       
1    1433 12999.542
4       2        1       2           7           0        39       1       
2    1783 12999.542
5       2        1       2           6           3        39       1       
3    1833 12999.542
6       6        1       1           8           9        38       1       
1    2700  8874.441
7       6        1       2           9           9        40       1       
2    2883  8874.441
8       6        1       2           8           6        42       1       
3    3016  8874.441
9       7        1       1           7           9        39       1       
1    2808  6911.880
10      7        1       2           6          10        35       1       
2    3233  6911.880


Chris Stubben




Daniel Nordlund-4 wrote
--
View this message in context: http://r.789695.n4.nabble.com/Getting-codebook-data-into-R-tp4374331p4386135.html
Sent from the R help mailing list archive at Nabble.com.