-
Notifications
You must be signed in to change notification settings - Fork 0
/
Import_export_convert_data_files.R
67 lines (44 loc) · 1.42 KB
/
Import_export_convert_data_files.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#-----------------------------------------------------------#
# Script for importing, exporting and converting data files # ----
#-----------------------------------------------------------#
## {rio} package ----
if(!require('rio')) {install.packages("rio")}
library(rio)
## Data ----
data <- mtcars
## Export (excel, text, csv, SPSS) ----
# to text separated by 'tab'
export(data, file = "prueba_tab.txt")
# to text separated by ';'
export(data, file = "prueba_semicolon.txt", format = ';')
# to csv separated by ','
export(data, file = "prueba_csv.csv")
# to excel
export(data, file = "prueba_excel.xlsx")
# to SPSS
export(data, file = "prueba_spss.sav")
## Import (excel, text, csv, SPSS) ----
# from excel, by default the first sheet
data <- import(file = "prueba_excel.xlsx")
head(data)
# from excel, specifying the sheet
data <- import(file = "prueba_excel.xlsx", which = 2) # error only 1 sheet
head(data)
# from text separated by ';'
data <- import(file = "prueba_semicolon.txt", format = ';')
head(data)
# from text separated by 'tab'
data <- import(file = "prueba_tab.txt", format = '\t')
head(data)
# from csv separated by ','
data <- import(file = "prueba_csv.csv")
head(data)
# from SPSS
data <- import(file = "prueba_spss.sav")
head(data)
## Convert (overwrites the files) ----
# SPSS to Excel
convert("prueba_spss.sav", "prueba_excel.xlsx")
# csv to Excel
convert("prueba_csv.csv", "prueba_excel.xlsx")
# and so on