Reputation: 949
I'm trying to import a .dat file into R. The file is not delimited Therefore i need to specify the position and type for each variable. In SAS, I can do this with a code that looks like this.
DATA imported_data;
INFILE " C:\dataset.dat"
INPUT
Var1 $ 1-2
Var2 $ 3-8
Var3 9-18
Var4 19-20
;
RUN;
The variables Var1 and Var2 are character ($) and their position is 1-2, and 3-8 within the data file.
Is there a R code that can be used in same way to import the file to R?
Thanks
Upvotes: 1
Views: 2116
Reputation: 1428
You can do this with readr's read_fwf
(read fixed width file).
library(readr)
data <- read_fwf("C:\dataset.dat",
col_positions = fwf_positions(start = c(1, 3, 9, 19),
end = c(2, 8, 18, 20)))
read_fwf
will try to figure out the types of each column, but if there are mistakes you can specify the types yourself using the col_types
argument.
Upvotes: 1