Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize a vector with fixed length in R

How to initialize a vector with fixed length in R ??

For example, I want a vector of characters with length of 10??

like image 726
ToBeGeek Avatar asked Feb 28 '14 20:02

ToBeGeek


People also ask

How do I create a fixed length vector in R?

To create a vector of specified data type and length in R we make use of function vector().

How do you set the length of a vector in R?

Setting length of the object in R Programming Here we are going to set the length of the vector in R Programming, for this we will use length() function. Parameters: x: vector or object.

How do I make a vector of the same number in R?

There are two methods to create a vector with repeated values in R but both of them have different approaches, first one is by repeating each element of the vector and the second repeats the elements by a specified number of times. Both of these methods use rep function to create the vectors.

How do you define length in R?

length() function gets or sets the length of a vector (list) or other objects. When resets a list or matrix, if the list is shortened, extra values will be discarded, if the list is lengthened, NAs (or nul ) is added to the list. length() function can be used for all R objects.


4 Answers

It's good that you ask because pre-allocating long vectors before for-loops that will be assigning results to long objects are made more efficient by not needing to successively lengthen vectors.

?vector  X <- vector(mode="character", length=10) 

This will give you empty strings which get printed as two adjacent double quotes, but be aware that there are no double-quote characters in the values themselves. That's just a side-effect of how print.default displays the values. They can be indexed by location. The number of characters will not be restricted, so if you were expecting to get 10 character element you will be disappointed.

>  X[5] <- "character element in 5th position"  >  X  [1] ""                                  ""                                   [3] ""                                  ""                                   [5] "character element in 5th position" ""                                   [7] ""                                  ""                                   [9] ""                                  ""  >  nchar(X)  [1]  0  0  0  0 33  0  0  0  0  0  > length(X) [1] 10 

Technically, lists are "vectors" in R, so this is a recommended (even necessary) practice for constructing lists with for-loops:

obj <- list( length(inp_vec) ) for ( i in seq_along(inp_vec) ){        obj[[i]] <- some_func( inp_vec[i] )        } 

@Tom reminds us that the default for 'mode' is logical and that R has a fairly rich set of automatic coercion methods, although I find his suggestion vector(,10) to be less clear than would be logical(10) which is its equivalent.

like image 147
IRTFM Avatar answered Sep 28 '22 05:09

IRTFM


If you want to initialize a vector with numeric values other than zero, use rep

n <- 10
v <- rep(0.05, n)
v

which will give you:

[1] 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05
like image 35
Alexander Avatar answered Sep 28 '22 03:09

Alexander


The initialization method easiest to remember is

vec = vector(,10); #the same as "vec = vector(length = 10);"

The values of vec are: "[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE" (logical mode) by default.

But after setting a character value, like

vec[2] = 'abc'

vec becomes: "FALSE" "abc" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE" "FALSE"", which is of the character mode.

like image 29
Tom Avatar answered Sep 28 '22 05:09

Tom


Just for the sake of completeness you can just take the wanted data type and add brackets with the number of elements like so:

x <- character(10)

like image 25
vonjd Avatar answered Sep 28 '22 04:09

vonjd