bebraw
9/11/2010 - 2:30 PM

plot.r

> # create a new vector (c as in combine)
> vec = c(2, 4, 5, 234, 2)
>
> # create another vector
> vec2 = vec * 2
> vec2
[1] 4 8 10 468 4
>
> # index selector
> vec3 = vec2[vec2 < 100]
> vec3
[1] 4 8 10 4
>
> # let's treat a vector as a set for a moment
> set = unique(vec3)
> set
[1] 4 8 10
>
> # I guess it might be cool to get sqrt of each item
> sapply(set, sqrt)
[1] 2.000000 2.828427 3.162278
>
> # "c" works with multiple vectors too
> vec4 = c(vec2, vec3)
> vec4
[1] 4 8 10 468 4 4 8 10 4
>
> # let's select just four first items (note the indexing!)
> vec4[1:3]
[1] 4 8 10
>
> # get first three items in reverse
> vec4[3:1]
[1] 10 8 4
>
> # get vector without those fours in the middle (note the -!)
> vec4[-c(5, 6)]
[1] 4 8 10 468 8 10 4
> # plot a parabola in range[-5, 5]
> plot(function(x) {return(x**2 + x + 3)}, -5, 5)
>
> # scatter plot with rainbow colors at dots
> # input: 10 random colors from range [1, 10]
> plot(sample(1:10, 10, replace=T), col=rainbow(10))
>
> # histogram
> data(morley) # pre-existing data set of measurements
> hist(morley$Speed) # refers to Speed segment of the list