Sandbox vectors

Let’s define some vectors which can be used for demonstrations:

manyNumbers <- sample( 1:1000, 20 )
manyNumbers
 [1] 517 986  85 829 144 108 536 644 348  81  17 507  42 979 155  91 280 301 585 949
manyNumbersWithNA <- sample( c( NA, NA, NA, manyNumbers ) )
manyNumbersWithNA
 [1] 507  NA 108 585 144 348 517 301  81  42 536 829  91 949  17  85 979 986 155 280 644  NA  NA
duplicatedNumbers <- sample( 1:5, 10, replace = TRUE )
duplicatedNumbers
 [1] 4 2 5 2 5 2 2 3 2 5
letters
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"
LETTERS
 [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
mixedLetters <- c( sample( letters, 5 ), sample( LETTERS, 5 ) )
mixedLetters
 [1] "o" "s" "u" "k" "g" "O" "L" "U" "R" "Z"

Are all/any elements TRUE

all( manyNumbers <= 1000 )
[1] TRUE
all( manyNumbers <= 500 )
[1] FALSE
any( manyNumbers > 1000 )
[1] FALSE
any( manyNumbers > 500 )
[1] TRUE
all( !is.na( manyNumbers ) )
[1] TRUE
any( is.na( manyNumbers ) )
[1] FALSE

Which elements are TRUE

Input: logical vector Output: vector of numbers (positions)

which( manyNumbers > 900 )
[1]  2 14 20
which( manyNumbersWithNA > 900 )
[1] 14 17 18
which( is.na( manyNumbersWithNA ) )
[1]  2 22 23

Filtering vector elements

manyNumbers[ manyNumbers > 900 ] # indexing by logical vector
[1] 986 979 949
manyNumbers[ which( manyNumbers > 900 ) ] # indexing by positions
[1] 986 979 949
somePositions <- which( manyNumbers > 900 )
manyNumbers[ somePositions ]
[1] 986 979 949

Are some elements among other elements

"A" %in% LETTERS
[1] TRUE
c( "X", "Y", "Z" ) %in% LETTERS
[1] TRUE TRUE TRUE
all( c( "X", "Y", "Z" ) %in% LETTERS )
[1] TRUE
all( mixedLetters %in% LETTERS )
[1] FALSE
any( mixedLetters %in% LETTERS )
[1] TRUE
mixedLetters[ mixedLetters %in% LETTERS ]
[1] "O" "L" "U" "R" "Z"
mixedLetters[ !( mixedLetters %in% LETTERS ) ]
[1] "o" "s" "u" "k" "g"
manyNumbers %in% 300:600
 [1]  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE
which( manyNumbers %in% 300:600 )
[1]  1  7  9 12 18 19
sum( manyNumbers %in% 300:600 )
[1] 6

Pick one of two (three) depending on condition

if_else( manyNumbersWithNA >= 500, "large", "small" )
 [1] "large" NA      "small" "large" "small" "small" "large" "small" "small" "small" "large" "large" "small" "large" "small" "small" "large" "large" "small" "small"
[21] "large" NA      NA     
if_else( manyNumbersWithNA >= 500, "large", "small", "UNKNOWN" )
 [1] "large"   "UNKNOWN" "small"   "large"   "small"   "small"   "large"   "small"   "small"   "small"   "large"   "large"   "small"   "large"   "small"   "small"  
[17] "large"   "large"   "small"   "small"   "large"   "UNKNOWN" "UNKNOWN"
# here integer 0L is needed instead of real 0.0 
# manyNumbersWithNA contains integer numbers and the method complains
if_else( manyNumbersWithNA >= 500, manyNumbersWithNA, 0L ) 
 [1] 507  NA   0 585   0   0 517   0   0   0 536 829   0 949   0   0 979 986   0   0 644  NA  NA

Duplicates and unique elements

unique( duplicatedNumbers )
[1] 4 2 5 3
unique( c( NA, duplicatedNumbers, NA ) )
[1] NA  4  2  5  3
duplicated( duplicatedNumbers )
 [1] FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE

Positions of max/min elements

which.max( manyNumbersWithNA )
[1] 18
manyNumbersWithNA[ which.max( manyNumbersWithNA ) ]
[1] 986
which.min( manyNumbersWithNA )
[1] 15
manyNumbersWithNA[ which.min( manyNumbersWithNA ) ]
[1] 17
range( manyNumbersWithNA, na.rm = TRUE )
[1]  17 986

Sorting/ordering of vectors

manyNumbersWithNA
 [1] 507  NA 108 585 144 348 517 301  81  42 536 829  91 949  17  85 979 986 155 280 644  NA  NA
sort( manyNumbersWithNA )
 [1]  17  42  81  85  91 108 144 155 280 301 348 507 517 536 585 644 829 949 979 986
sort( manyNumbersWithNA, na.last = TRUE )
 [1]  17  42  81  85  91 108 144 155 280 301 348 507 517 536 585 644 829 949 979 986  NA  NA  NA
sort( manyNumbersWithNA, na.last = TRUE, decreasing = TRUE )
 [1] 986 979 949 829 644 585 536 517 507 348 301 280 155 144 108  91  85  81  42  17  NA  NA  NA
manyNumbersWithNA[1:5]
[1] 507  NA 108 585 144
order( manyNumbersWithNA[1:5] )
[1] 3 5 1 4 2
rank( manyNumbersWithNA[1:5] )
[1] 3 5 1 4 2
sort( mixedLetters )
 [1] "g" "k" "L" "o" "O" "R" "s" "u" "U" "Z"

Ranking of vectors

manyDuplicates <- sample( 10:15, 10, replace = TRUE )
rank( manyDuplicates )
 [1]  1.5  4.0  1.5  7.0  7.0  4.0 10.0  7.0  4.0  9.0
rank( manyDuplicates, ties.method = "min" )
 [1]  1  3  1  6  6  3 10  6  3  9
rank( manyDuplicates, ties.method = "random" )
 [1]  2  5  1  8  7  4 10  6  3  9

Rounding numbers

v <- c( -1, -0.5, 0, 0.5, 1, rnorm( 10 ) )
v
 [1] -1.00000000 -0.50000000  0.00000000  0.50000000  1.00000000  0.14869382 -1.39818942 -0.06505665  0.83037541  1.75082107  1.42230791  1.17252215 -1.44468445
[14] -0.71492889 -0.36219210
round( v, 0 )
 [1] -1  0  0  0  1  0 -1  0  1  2  1  1 -1 -1  0
round( v, 1 )
 [1] -1.0 -0.5  0.0  0.5  1.0  0.1 -1.4 -0.1  0.8  1.8  1.4  1.2 -1.4 -0.7 -0.4
round( v, 2 )
 [1] -1.00 -0.50  0.00  0.50  1.00  0.15 -1.40 -0.07  0.83  1.75  1.42  1.17 -1.44 -0.71 -0.36
floor( v )
 [1] -1 -1  0  0  1  0 -2 -1  0  1  1  1 -2 -1 -1
ceiling( v )
 [1] -1  0  0  1  1  1 -1  0  1  2  2  2 -1  0  0

Naming vector elements

heights <- c( Amy = 166, Eve = 170, Bob = 177 )
heights
Amy Eve Bob 
166 170 177 
names( heights )
[1] "Amy" "Eve" "Bob"
names( heights ) <- c( "AMY", "EVE", "BOB" )
heights
AMY EVE BOB 
166 170 177 
heights[[ "EVE" ]]
[1] 170

Generating grids

expand_grid( x = c( 1:3, NA ), y = c( "a", "b" ) )
# A tibble: 8 × 2
      x y    
  <int> <chr>
1     1 a    
2     1 b    
3     2 a    
4     2 b    
5     3 a    
6     3 b    
7    NA a    
8    NA b    

Generating combinations

combn( c( "a", "b", "c", "d", "e" ), m = 2, simplify = TRUE )
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "a"  "a"  "a"  "a"  "b"  "b"  "b"  "c"  "c"  "d"  
[2,] "b"  "c"  "d"  "e"  "c"  "d"  "e"  "d"  "e"  "e"  
combn( c( "a", "b", "c", "d", "e" ), m = 3, simplify = TRUE )
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "a"  "a"  "a"  "a"  "a"  "a"  "b"  "b"  "b"  "c"  
[2,] "b"  "b"  "b"  "c"  "c"  "d"  "c"  "c"  "d"  "d"  
[3,] "c"  "d"  "e"  "d"  "e"  "e"  "d"  "e"  "e"  "e"  


Copyright © 2024 Biomedical Data Sciences (BDS) | LUMC