Copy and paste the following code to your R Studio platform or R version:

# Creating vectors

firstVector <- c(3, 45, 56, 732) # the combine function (c)
firstVector
is.numeric(firstVector)
# meaning that the vector is indeed numeric
is.integer(firstVector)
# returns FALSE, since, R, by default, will store numbers as a double type
is.double(firstVector)

# creating an integer vector
inteVector <- c(3L, 12L, 243L, 0L)
inteVector
is.numeric(inteVector)
is.integer(inteVector)
is.double(inteVector)
# returns FALSE as expected

# creating a character vector
charVector <- c(“Hey”, “James”, “how”, “are you”, “?”)
charVector
is.character(charVector)
is.numeric(charVector)

# Using sequence & replicate

seq() # sequence – like “:”
rep() # for replication

seq(1, 15)
1:15
# identical functions, sequence gives flexibility though (steps for instance)

seq(0, 15, 3)

z <- seq(0, 20, 4)
z

rep(3, 50) # replicating number 3 50 times

y <- rep(2, 10)
y

rep(“A”, 5) # replicate characters
x <- c(80, 20)
x
rep(x, 10) # replicate other vectors

Posted by Dimitris

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s