Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does c do in R? [duplicate]

Tags:

r

Consider the code below:

k <- c(.5, 1)

What does c do here? I think it must be a list or vector. If it is, how can I extend this vector to contain 1024 values?

like image 587
MOON Avatar asked Aug 12 '14 15:08

MOON


People also ask

What is the use of c () in R?

c() function in R Language is used to combine the arguments passed to it.

How do you duplicate values in R?

In R, the easiest way to repeat rows is with the REP() function. This function selects one or more observations from a data frame and creates one or more copies of them. Alternatively, you can use the SLICE() function from the dplyr package to repeat rows.

What does unique () do in R?

The unique() function in R is used to eliminate or delete the duplicate values or the rows present in the vector, data frame, or matrix as well. The unique() function found its importance in the EDA (Exploratory Data Analysis) as it directly identifies and eliminates the duplicate values in the data.

Which function removes duplicates in R?

unique() function in R Language is used to remove duplicated elements/rows from a vector, data frame or array.


1 Answers

In R, the c() function returns a vector (a one dimensional array).

In your example:

k <- c(0.5, 1) # k is a vector
k[1] # is 0.5 (remember, R indices start on 1)
k[2] # is 1

If you want to create a vector with 1024 entries (assuming 0.5 increments), you have at least two ways to do it:

# One way
k <- (1:1024) / 2 # this will be 0.5, 1, 1.5, 2, ... , 512
# Another way:
k <- seq(0.5, 512, 0.5)

Also you can use c() to concatenate two vectors:

k <- c(0.5, 1)         # k = 0.5, 1
k <- c(k, 1.5)         # k = 0.5, 1, 1.5
k <- c(k, c(2, 2.5))   # k = 0.5, 1, 1.5, 2, 2.5
k <- c(k, k)           # k = 0.5, 1, 1.5, 2, 2.5, 0.5, 1, 1.5, 2, 2.5

Please check the help for c() and seq function (in R: ?c and ?seq)


Reference:

  • Quick-R: Data types
like image 163
Barranka Avatar answered Oct 07 '22 23:10

Barranka