Retrieving data from a text file (separated by spaces) using variable lengths in R

I have a text file like this (separated by spaces):

x <- "DF12 This is an example 1 This
DF12 This is an 1232 This is
DF14 This is 12334 This is an
DF15 This 23 This is an example
"

      

and I know the field lengths for each variable (there are 5 variables in this dataset):

varlength <- c(2, 2, 18, 5, 18)

      

How can I import this kind of data into R using the varlength variable as the field separator indicator?

+2


source to share


1 answer


Per Barry Rowlingson on r-help (where you seem to cross):



? Read.fwf

Reading fixed-width files

Description:

 Read a table of *f*ixed *w*idth *f*ormatted data into a
 'data.frame'.

      

Using:

 read.fwf(file, widths, header = FALSE, sep = "\t",
          skip = 0, row.names, col.names, n = -1,
          buffersize = 2000, ...)

      

+2


source







All Articles