Tuesday, November 3, 2015

R Basics 3 - Atomic Vectors

Atomic vectors:
- An object with contiguous, indexed values
- Indexed from 1 to length(vector)
- All values of the same basic atomic type
- Vectors do not have a dimension attribute
- Has a fixed length once created

Six basic atomic types:
- logical
- integer
- numeric
- complex
- character
- raw

No scalars
In R, the basic types are always in a vecotr. Scalars are just length=1 vectors.

Creation (length determined at creation)
#Default value vectors of length=4
> u <- vector(mode='logical', length=4)
> print(u)
[1] FALSE FALSE FALSE FALSE
> v <- vector(mode= 'integer', length=4)
> print(v)
[1] 0 0 0 0

#Using the sequence operator
> i <- 1:5; i
[1] 1 2 3 4 5
> j <- 1.4:6.4; j
[1] 1.4 2.4 3.4 4.4 5.4 6.4
> k <- seq(from=0, to =1, by=0.1);k
 [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0

#Using the c() function
> l <- c(TRUE,FALSE); l
[1]  TRUE FALSE
> n <- c(1.3, 7, 7/20); n
[1] 1.30 7.00 0.35
> z <- c(1+2i, 2, -3+4i); z
[1]  1+2i  2+0i -3+4i

#Other things
> v1 <- c(a=1,b=2,c=3); v1
a b c
1 2 3
> v2 <- rep(NA, 3); v2
[1] NA NA NA
> v3 <- c(v1, v2); v3
 a  b  c        
 1  2  3 NA NA NA
> v4 <- append(1:5, 2:10, after=5); v4
 [1]  1  2  3  4  5  2  3  4  5  6  7  8  9 10

Conversion
> as.vector(v4)
 [1]  1  2  3  4  5  2  3  4  5  6  7  8  9 10
> as.logical(v4)
 [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
> as.integer(v4)
 [1]  1  2  3  4  5  2  3  4  5  6  7  8  9 10
> as.numeric(v4)
 [1]  1  2  3  4  5  2  3  4  5  6  7  8  9 10
> as.character(v4)
 [1] "1"  "2"  "3"  "4"  "5"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10"
> unlist(l)
[1]  TRUE FALSE

Basic information about atomic vectors
> dim(v)
NULL
> is.atomic(v)
[1] TRUE
> is.vector(v)
[1] TRUE
> is.list(v)
[1] FALSE
> is.factor(v)
[1] FALSE
> is.recursive(v)
[1] FALSE
> length(v)
[1] 14
> names(v)
NULL
> mode(v)
[1] "numeric"
> class(v)
[1] "integer"
> typeof(v)
[1] "integer"
> attributes(v)
NULL
> is.numeric(v);
[1] TRUE
> is.character(v);
[1] FALSE
Trap: lists are vectors (but not atomic)
Trap: array/matrix are atomic (not vectors)
Tip: use(is.vector(v) && is.atomic(v)

The content of a vector
> cat(v)
1 2 3 4 5 2 3 4 5 6 7 8 9 10
> print(v)
 [1]  1  2  3  4  5  2  3  4  5  6  7  8  9 10
> str(v)
 int [1:14] 1 2 3 4 5 2 3 4 5 6 ...
> dput(v)
c(1L, 2L, 3L, 4L, 5L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L)
> head(v)
[1] 1 2 3 4 5 2
> tail(v)
[1]  5  6  7  8  9 10

Indexing: [ and [[ but not $
 -[x] selects a vecor for the cell/range x
-[[x]] selects a length=1 vector for the single cell index x (rarely used)
- $ operator invalid for atomic vectors

Index by positive numbers
> v[c(1,2,3)]
[1] 1 2 3
> v[1:2]
[1] 1 2
> v[[7]]
[1] 3
> v[which(v == 'M')]

integer(0)

Index by negative numbers
> v[-1] #get all but the first element
 [1]  2  3  4  5  2  3  4  5  6  7  8  9 10
> v[-length(v)]
 [1] 1 2 3 4 5 2 3 4 5 6 7 8 9
> v[-c(1,3,5,7,9)]
[1]  2  4  2  4  6  7  8  9 10

Index by name(only with named vectors)
> names(v)[1:3] = c('alpha', 'beta','z')
> v[['alpha']]
[1] 1
> v[['beta']]
[1] 2
> v[c('alpha','beta')]
alpha  beta
    1     2
> v[!(names(v) %in% c('c', 'b'))]
alpha  beta     z  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>
    1     2     3     4     5     2     3     4     5     6     7     8     9    10

Sorting
> upsorted = sort(v); upsorted;
alpha  beta  <NA>     z  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>
    1     2     2     3     3     4     4     5     5     6     7     8     9    10
> v[order(v)]
alpha  beta  <NA>     z  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>
    1     2     2     3     3     4     4     5     5     6     7     8     9    10
> d = sort(v, decreasing = TRUE);d
 <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>     z  <NA>  beta  <NA> alpha
   10     9     8     7     6     5     5     4     4     3     3     2     2     1

Raw vectors
> s <- charToRaw('raw');
> r <- as.raw(c(114,97,119))
> print(r)
[1] 72 61 77


No comments:

Post a Comment

Blog Archive