R notes[2]
文章目录
- sequence
- references
sequence
- organized equence is a regular facility of R language,it can be generated by the
seq()
function.of couse ,in oder to make arbitrary list, thec()
is used to achieve the task,that is explained at the previous note.
the seq funtcion forms as follows.
seq(from_value,to_value,step)
> seq(1,10,2)
[1] 1 3 5 7 9
> seq(1,10,1)[1] 1 2 3 4 5 6 7 8 9 10
> seq(1,10,7)
[1] 1 8
> seq(10,1,-1)[1] 10 9 8 7 6 5 4 3 2 1
you may assign arguments in keyword,for example, seq(to=1,from=10,by=-1)
.
the rep
function also make sequence which is different from seq,rep
puts the same elements together into a list with specified number of times by default.
> rep(-1,3)
[1] -1 -1 -1
> rep(1,each=2)
[1] 1 1
> rep(1,times=2)
[1] 1 1
> rep(1,2)
[1] 1 1
We need to pay attention that if given element is a list,the rep
have a argument named each
which will repeatedly generate each element of given list for specified number of times and then arrange them in the original order of those elements .
> a<-c(1,2)
> rep(a,3)
[1] 1 2 1 2 1 2
> rep(a,times=3)
[1] 1 2 1 2 1 2
> rep(a,each=3)
[1] 1 1 1 2 2 2
references
- https://cran.r-project.org/doc/manuals/