Search notes:
R function: seq
by=4
Create a
vector, starting with 20, incrementing
by 4, with greatest element not greater than 41:
seq(20, 41, by=4)
# [1] 20 24 28 32 36 40
length=4
Create exactly 4 elements between 10 and 26, inclusively:
seq(10, 26, length=4)
# [1] 10.00000 15.33333 20.66667 26.00000
Creating a sequence of dates
When starting with a »date« value such as created by
ISODate(…)
,
seq
creates a vector of dates.
The by
parameter then understands durations such as week
:
seq(from = ISOdate(2016, 8, 28),
by ="week" , # Dates are a week apart
length.out = 5)
# [1] "2016-08-28 12:00:00 GMT" "2016-09-04 12:00:00 GMT"
# [3] "2016-09-11 12:00:00 GMT" "2016-09-18 12:00:00 GMT"
# [5] "2016-09-25 12:00:00 GMT"
Using seq to plot an x/y function
First seq(…)
is used to fill x values to a vector (named x
). Then, a vector, named y
, is filled by calling a function.
Finally, x and y are ploted;
x11(width=5,height=3)
x <- seq(0, 30, by=.1)
y <- exp(-x*0.2) * sin(x)
# png(file = 'img/x-y-plot.png', width=300, height=180)
par(mar=c(2, 2, 0.1, 0.1))
plot(x, y, type='l')
# dev.off()
# Wait for mouse click or enter pressed
z <- locator(1)
Misc
»Parameter along«
df <- data.frame(
col_1 = c( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ),
col_2 = c('Foo', 'Foo', 'Bar', 'Foo', 'Baz', 'Baz', 'Foo', 'Bar')
)
df.splitted <- split(df,
c( 'A', 'A', 'A', 'B', 'A', 'B', 'C', 'C')
)
df.splitted
seq(along=df.splitted)
# [1] 1 2 3