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?
c() function in R Language is used to combine the arguments passed to it.
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.
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.
unique() function in R Language is used to remove duplicated elements/rows from a vector, data frame or array.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With