R365: Day 29 – each, times, and length.out argument

When you are making dataframes, you need all of your datasets to be the same length. But sometimes you don’t want to bother dealing with entering every last variable in a vector. We actually used the length.out argument during the Day 27 – dataframes post to specify how many individuals we needed. The each argument lets you specify how many times to repeat each numeric, the times indicates how many times to repeat the group as a whole, and the length.out argument indicates the total length of the desired vector. You can use the arguments together in one rep(), or individually.

## make a vector with repeating numbers of length 20
rep(c(3,5,6),length.out=20)
## you can do the same thing with the argument 'len' instead of 'length.out'
rep(c(3,5,6),len=20)
## make a vector with each number repeated three times
rep(c(3,5,6),each=3, len=20)
##Lets try to break R, tell it to repeat something until length 24, but also tell it to stop at length 20
## the 'times' argument tells R to repeat the whole group a certain set of times, whereas
##the 'each' argument tells R to repeat each numberic a set number of times
##It does both, and repeats the group, but stops at length 20.
rep(c(3,5,6),each=3, times=3,len=20)
##now lets try this with seq
seq(1, 9, length.out=20)
##but seq does not recognize the 'each' or 'times arguments
#seq(1, 9, length.out=20, each=3)
#seq(1, 9, length.out=20, times=3)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s