-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate_csv_files.R
executable file
·56 lines (35 loc) · 1.56 KB
/
generate_csv_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
#!/usr/bin/env Rscript
# Libraries
library(plyr)
library(dplyr)
library(tidyr)
library(nycflights13)
# Setup
dir_dat <- file.path(getwd(), "dat")
## 1. Make mtcars csv files
# Make dat as copy of mtcars
dat <- mtcars
# move rownames to actual column name and remove rowname
dat$name <- rownames(mtcars)
rownames(dat) <- NULL
# Make into datatable, rearrange so colnames is first column
dat <- dat %>% tbl_df %>% select(name, mpg:carb)
# Output files
a_ply(1:55, 1, function(e) {
csv_out <- file.path(dir_dat, sprintf("mtcars_%03d.csv", e))
write.csv(dat, csv_out, row.names=FALSE)
})
## 2. Make transposed file of mtcars csv file
datt <- dat %>% t %>% as.data.frame(stringsAsFactors=FALSE) %>% tbl_df
names(datt) <- datt[1,]
datt <- datt[2:nrow(datt),]
datt$metric <- names(dat)[2:length(names(dat))]
datt <- datt %>% select(metric, 2:length(names(datt)))
write.csv(datt, file.path(dir_dat, "wide_mtcars.csv"), row.names=FALSE)
## 3. Make a long dataset
write.csv(flights %>% head(333), file.path(dir_dat, "long_flights.csv"), row.names=FALSE)
## 4. Make a wide dataset of some subset of flights
write.csv(flights %>% group_by(dest, month) %>% tally %>% spread(dest, n), file.path(dir_dat, "wide_flights_dest_month.csv"), row.names=FALSE)
## 5. Make some datafiles that are joinable
write.csv(data.frame(k=letters[1:3], v=1:3, stringsAsFactors=FALSE), file.path(dir_dat, "join_1.csv"), row.names=FALSE)
write.csv(data.frame(k=letters[2:4], v=2:4, stringsAsFactors=FALSE), file.path(dir_dat, "join_2.csv"), row.names=FALSE)