Send and read SEO data to Excel/CSV
⚠️ THIS IS A WORK IN PROGRESS
CSV and Excel file remain one of amongst the most well-used file formats for exchange data.

Read your data from a CSV

1
#setup where to read the file
2
setwd("~/Desktop")
3
# en write the file
4
test <- read.csv(df, "data.csv")
Copied!

Export your data into a CSV

assuming your data is store inside df var, fairly simple:
1
#setup where to write the file
2
setwd("~/Desktop")
3
# en write the file
4
write.csv(df, "data.csv")
Copied!

Read an excel

1
# the file.choose() will prompte a file selector
2
# the 1 say we want to load the first tab
3
test <- xlsx::read.xlsx(file.choose(),1)
Copied!

Export your data into an excel file

A little bit more complex, we’ll use the ‘xlsx’ package
1
#setup where to write the file
2
setwd("~/Desktop")
3
4
# if the package is not instal yet, run this
5
# install.packages("xlsx")
6
7
# Loading the package
8
library(xlsx)
9
10
# we write the file
11
write.xlsx(df, "data.xlsx")
12
Copied!
A few more tips for you:
I’ll like to use the sheetName option to explicitly name the tab. The default name is “Sheet1”. Quite useful to have a record of when the file has been generated for example. Replace last instruction what follows and you’ll be able to know.
1
write.xlsx(df, "data.xlsx", sheetName=format(Sys.Date(), "%d %b %Y"))
Copied!
Another good one that I like is to send the excel file to a Shared folder directly. Replace first instruction by
1
setwd("/Users/me/Dropbox/Public")
Copied!
Of course, replace the file path with yours.

Import and merge a batch of CSV files

Aggregate several CSV files into one using file name as a column
1
library(plyr)
2
library(readr)
3
library(purrr)
4
5
# add the path where the csv's are located
6
setwd("./Downloads/test/")
7
8
# list csv files inside the directory
9
# for each: import the csv (read.csv function)
10
# add filename as a column
11
# and merge
12
13
Tbl <- list.files(path = "./",
14
pattern="*.csv",
15
full.names = T) %>%
16
map_df(function(x) read_csv(x, col_types = cols(.default = "c")) %>%
17
mutate(filename=gsub(".csv","",basename(x))))
18
19
20
View(Tbl)
Copied!