-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.R
More file actions
71 lines (63 loc) · 2.01 KB
/
Copy pathsetup.R
File metadata and controls
71 lines (63 loc) · 2.01 KB
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
68
69
70
# setup.R
# used for generating the sample data for the presentation
suppressPackageStartupMessages({
library(dplyr)
library(tidyr)
library(ggplot2)
library(purrr)
})
#' Create a bent data profile
#'
#' @param N Integer scalar, number of rows in output
#' @param m Numeric scalar, slope of line
#' @param mx Numeric scalar, clipping value
#' @param s Numeric scalar, standard deviation of additive noise
#' @param colname1 Character scalar, name of independent variable
#' @param colname2 Character scalar, name of dependent variable
make_bent_data <- function( x, m, mx, s, colname1, colname2 ) {
y <- pmin( mx, x * m )
y <- y + rnorm( length( x ), 0, s )
DF <- data.frame( x = x
, y = y
)
setNames( DF, c( colname1, colname2 ) )
}
write_spacy_csv <- function( DF, fname ) {
con <- textConnection( "s", "w", local = TRUE )
write.table( DF, file = con, sep = ",", row.names = FALSE, quote = FALSE )
close( con )
con <- file( fname, open = "w" )
writeLines( c( s[ 1 ], "header info" ), con )
writeLines( s[ -1 ], con )
close( con )
}
x <- 0:1200
s <- 2
set.seed(42)
dta <- ( data.frame( fname = paste0( "spacy_", 1:2, ".csv" )
, m = 1:2
, mx = c( 900, 1050 )
)
%>% rowwise()
%>% mutate( data = list( make_bent_data( x
, m = m
, mx = mx
, s = s
, colname1 = "X"
, colname2 = "Y"
)
)
)
%>% ungroup()
)
( dta
%>% unnest( cols = "data" )
%>% ggplot( aes( x = X, y = Y, colour = fname ) ) ) +
geom_point()
( dta
%>% select( fname, data )
%>% pwalk( function( fname, data ) {
write_spacy_csv( data, file.path( "data", fname ) )
}
)
)