How to import . dat file to R

Asked

Viewed 1,281 times

5

I downloaded a file . dat which is basically an array with data.

However, it seems to me that the only way to work with him is by doing the following command (I’m using the R and I don’t have much experience with the R):

   setwd("C:")
    load("dat73_1")

After this command "load" this file is "hidden" by R??

The code to read this file is as follows::

mylist<-c("CAN","FRA","DEU","ITA","JPN","GBR","G6") 

If I want the information regarding "CAN", just modify "cno in 1:7" for "cno in 1:1".

  for (cno in 1:7){
      init_date<-c(1973,3)
      start_date <- init_date+c(6,3)
      end_date<-c(2009,10) 
      country<-mylist[cno]
      Cdat<-data.frame(window(ts(get(country),start=init_date,freq=12),start=start_date, end=end_date))}

I did this and managed to recover this file to all countries of mylist and created a new file in Excel. But I can’t adapt the code to the excel file. There is how to do this?

If I can "find" this file. dat tbm would help a lot.

OBS (Edited): When I use the read.table command the output is this (Rstudio):

inserir a descrição da imagem aqui

  • To read dat files, you use the read.table(file) function. Only checks if you need to use other arguments, such as header=T, if you want the original column names and Skip=number of rows to avoid. Dat files usually have extra information, so the Skip argument serves to ignore them.

  • @Jose thanks!. I edited the theme to show the output when I use the read.table. command I can’t pull right.

    1. Where did you get this file from? In the source there was no information about separator or how to read the file? 2) The command load() should be used in binary files Rextended .RDA or .Rdata, shouldn’t be what you need.
  • Hi, @Laura. You know which program generated this file .data? From what I saw of the result after using the read.table() it seems to me that your file is binary (but not in format .RData, that could be read by the function load()). Files like this usually require a treatment outside the R to be converted into a data table in format .csv or .txt, for example. Only then can they be read with the function read.table() within the R.

  • put the example file here

2 answers

2

You can read directly from the R base using read.delim():

dados <- read.delim("arquivo.dat")

0

To open files in the ". dta" format another alternative is to use the Haven package. Then look for the read_dta function.

install.packages("haven")
library("haven")
read_dta()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.