Mostrando entradas con la etiqueta read_csv2. Mostrar todas las entradas
Mostrando entradas con la etiqueta read_csv2. Mostrar todas las entradas

2019-12-29

Read compressed files in R using readr

Problem

We need to read compressed files in R.

Solution

We will use the package readr:

Files ending in .gz, .bz2, .xz, or .zip will be automatically uncompressed. Files starting with http://, https://, ftp://, or ftps:// will be automatically downloaded. Remote gz files can also be automatically downloaded and decompressed.

In our example we use the file title.ratings.tsv.gz.

library(readr)
df_ratings <- read_tsv('title.ratings.tsv.gz', na = "\\N", quote = '')
df_ratings %>% head()
We can provide the URL and it will be automatically downloaded and decompressed

df_ratings <- read_tsv('https://datasets.imdbws.com/title.ratings.tsv.gz', na = "\\N", quote = '')
df_ratings %>% head()

Results

# A tibble: 6 x 3
  tconst    averageRating numVotes
                   
1 tt0000001           5.8     1423
2 tt0000002           6.4      168
3 tt0000003           6.6     1016
4 tt0000004           6.4      100
5 tt0000005           6.2     1713
6 tt0000006           5.5       88

2018-10-14

Importar ficheros comprimidos en R con readr

Problema

Queremos importar ficheros comprimidos en R.

Solución

Empleamos el paquete readr que descomprime automáticamente los siguientes tipos de ficheros: gz, .bz2, .xz, o .zip. Podemos emplearlo con la read_delim o los casos especiales read_csv, read_csv2 o read_tsv. En nuestro ejemplo utilizamos el fichero title.ratings.tsv.gz.

library(readr)
df_ratings <- read_tsv('title.ratings.tsv.gz', na = "\\N", quote = '')
df_ratings %>% head()
También podemos indicar la dirección del fichero y automáticamente descargará y descomprimirá el mismo.

df_ratings <- read_tsv('https://datasets.imdbws.com/title.ratings.tsv.gz', na = "\\N", quote = '')
df_ratings %>% head()

Resultados

# A tibble: 6 x 3
  tconst    averageRating numVotes
                   
1 tt0000001           5.8     1423
2 tt0000002           6.4      168
3 tt0000003           6.6     1016
4 tt0000004           6.4      100
5 tt0000005           6.2     1713
6 tt0000006           5.5       88

Entradas relacionadas

Referencias

Nube de datos