Leitura e Escrita de Arquivos
Escrita
head(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
Arquivos de texto
write.csv(iris, "iris.csv", row.names = F) ## separado por vírgula
write.csv2(iris, "iris-pontoevirgula.csv", row.names = F) ## separado por ponto e vírgula
write.table(iris, "iris.txt", row.names = F) ## separado por espaço
write.table(iris, "iris.tsv", row.names = F, sep ="\t") ## separado por tabulação
write.table(iris, "iris-semcabecalho.csv", row.names = F, col.names = F, sep = ",") ## sem cabeçalho
Planilhas
Requisitos
Instalar o Java JDK:
https://www.oracle.com/technetwork/pt/java/javase/downloads/index.htmlConfigurar as variáveis de ambiente:
JAVA_HOME: diretório do Java JDK
Path: diretório bin do Java JDK (mover para cima)Instalar os pacotes rJava, openxlsx
Carregando os pacotes:
library(rJava)
library(openxlsx)
Excel
Exportando o dataset iris para Excel:
write.xlsx(iris, "iris.xlsx")
Exportando os datasets iris e mtcars para um mesmo arquivo Excel:
planilhas <- list("Dataset Iris" = iris, "Dataset Mtcars" = mtcars)
write.xlsx(planilhas, "iris-mtcars.xlsx")
OpenDocuments
Exportando o dataset iris para OpenDocument:
write.xlsx(iris, "iris.ods")
Exportando os datasets iris e mtcars para um mesmo arquivo Excel:
planilhas <- list("Dataset Iris" = iris, "Dataset Mtcars" = mtcars)
write.xlsx(planilhas, "iris-mtcars.ods")
Leitura
Arquivos de texto
Lendo uma base de dados separada por vírgula:
df_csv <- read.csv("iris.csv")
head(df_csv)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
Lendo uma base de dados separada por ponto e vírgula:
df_csv2 <- read.csv2("iris-pontoevirgula.csv")
head(df_csv2)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
Lendo uma base de dados separada por tabulação:
df_tab <- read.table("iris.tsv")
head(df_tab)
## V1 V2 V3 V4 V5
## 1 Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 2 5.1 3.5 1.4 0.2 setosa
## 3 4.9 3 1.4 0.2 setosa
## 4 4.7 3.2 1.3 0.2 setosa
## 5 4.6 3.1 1.5 0.2 setosa
## 6 5 3.6 1.4 0.2 setosa
df_tab <- read.table("iris.tsv", header = TRUE)
head(df_tab)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
Lendo uma base de dados separada por tabulação, utilizando a função ao read.csv()
::
df_tab <- read.csv("iris.tsv")
head(df_tab)
## Sepal.Length.Sepal.Width.Petal.Length.Petal.Width.Species
## 1 5.1\t3.5\t1.4\t0.2\tsetosa
## 2 4.9\t3\t1.4\t0.2\tsetosa
## 3 4.7\t3.2\t1.3\t0.2\tsetosa
## 4 4.6\t3.1\t1.5\t0.2\tsetosa
## 5 5\t3.6\t1.4\t0.2\tsetosa
## 6 5.4\t3.9\t1.7\t0.4\tsetosa
df_tab <- read.csv("iris.tsv", sep = "\t")
head(df_tab)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
Lendo uma base de dados separada por vírgula sem cabeçalho:
df_csv3 <- read.csv("iris-semcabecalho.csv")
head(df_csv3)
## X5.1 X3.5 X1.4 X0.2 setosa
## 1 4.9 3.0 1.4 0.2 setosa
## 2 4.7 3.2 1.3 0.2 setosa
## 3 4.6 3.1 1.5 0.2 setosa
## 4 5.0 3.6 1.4 0.2 setosa
## 5 5.4 3.9 1.7 0.4 setosa
## 6 4.6 3.4 1.4 0.3 setosa
df_csv3 <- read.csv("iris-semcabecalho.csv", header = F)
head(df_csv3)
## V1 V2 V3 V4 V5
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
df_csv3 <- read.csv("iris-semcabecalho.csv", header = F,
col.names = c("ComprSep", "LargSep", "ComprPet", "LargPet", "Esp"))
head(df_csv3)
## ComprSep LargSep ComprPet LargPet Esp
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
df_csv3 <- read.csv("iris-semcabecalho.csv", header = F)
colnames(df_csv3) <- c("ComprSep", "LargSep", "ComprPet", "LargPet", "Esp")
head(df_csv3)
## ComprSep LargSep ComprPet LargPet Esp
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
Planilhas
Excel
library(openxlsx)
df_iris <- read.xlsx("iris-mtcars.xlsx") ## por padrão a primeira planilha é lida
head(df_iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
df_mtcars <- read.xlsx("iris-mtcars.xlsx", sheet = 2)
head(df_mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## 1 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## 2 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## 3 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## 4 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## 5 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## 6 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
arquivo_completo <- loadWorkbook("iris-mtcars.xlsx")
OpenDocument
df_mtcars <- read.xlsx("iris-mtcars.ods", sheet = 2)
head(df_mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## 1 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## 2 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## 3 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## 4 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## 5 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## 6 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
Arquivos online
https://archive.ics.uci.edu/ml/datasets/wine
df_wine <- read.csv("https://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data")
head(df_wine)
## X1 X14.23 X1.71 X2.43 X15.6 X127 X2.8 X3.06 X.28 X2.29 X5.64 X1.04 X3.92
## 1 1 13.20 1.78 2.14 11.2 100 2.65 2.76 0.26 1.28 4.38 1.05 3.40
## 2 1 13.16 2.36 2.67 18.6 101 2.80 3.24 0.30 2.81 5.68 1.03 3.17
## 3 1 14.37 1.95 2.50 16.8 113 3.85 3.49 0.24 2.18 7.80 0.86 3.45
## 4 1 13.24 2.59 2.87 21.0 118 2.80 2.69 0.39 1.82 4.32 1.04 2.93
## 5 1 14.20 1.76 2.45 15.2 112 3.27 3.39 0.34 1.97 6.75 1.05 2.85
## 6 1 14.39 1.87 2.45 14.6 96 2.50 2.52 0.30 1.98 5.25 1.02 3.58
## X1065
## 1 1050
## 2 1185
## 3 1480
## 4 735
## 5 1450
## 6 1290