Skip to contents

Shorthand to remove elements from a vector and save as the same name

Usage

vector_pop(., n = 1, el = NULL, ret = FALSE)

Arguments

.

parent vector

n

number of elements to remove

el

vector to remove

ret

TRUE or FALSE. whether to return value instead of setting it to the parent vector

Value

vector with elements removed

Examples

num1 <- sample(330:400,10)
name1 <- "ObinnaObianomObiObianom"

#task: remove 1 element from the end of the vector and set it to the vector name
num1 #num1 vector before pop
#>  [1] 342 389 383 386 333 379 336 357 343 364
vector_pop(num1) #does not return anything
num1 #num1 vector updated after pop
#> [1] 342 389 383 386 333 379 336 357 343

#task: remove 5 elements from the end, but do not set it to the vector name
num1 #num1 vector before pop
#> [1] 342 389 383 386 333 379 336 357 343
vector_pop(num1,5, ret = TRUE) #return modified vector
#> [1] 342 389 383 386
num1 #num1 vector remains the same after pop
#> [1] 342 389 383 386 333 379 336 357 343


#task: remove 6 elements from a word, set it back to vector name
name1 #name1 before pop
#> [1] "ObinnaObianomObiObianom"
vector_pop(name1,6) #does not return anything
name1 #name updated after pop
#> [1] "ObinnaObianomObiO"

#task: remove 3 elements from a word, Do not set it back to vector name
name1 #name1 before pop
#> [1] "ObinnaObianomObiO"
vector_pop(name1,3, ret = TRUE) #returns modified name1
#> [1] "ObinnaObianomO"
name1 #name1 not updated after pop
#> [1] "ObinnaObianomObiO"

#task: remove 4 elements from the end of a vector and return both the removed content and remaining
v_f_num <- paste0(number(20),c("TI")) #simulate 20 numbers and add TI suffix
v_f_num #show simulated numbers
#>  [1] "866698546TI" "902358467TI" "886650742TI" "306851957TI" "509968762TI"
#>  [6] "487274754TI" "53693383TI"  "703880359TI" "746617211TI" "808960926TI"
#> [11] "916007042TI" "396251736TI" "250197935TI" "954900876TI" "664845264TI"
#> [16] "731667101TI" "557189988TI" "163955404TI" "576890078TI" "503808426TI"
vector_pop(v_f_num, n = 4, ret = TRUE) #get the modified vector
#>  [1] "866698546TI" "902358467TI" "886650742TI" "306851957TI" "509968762TI"
#>  [6] "487274754TI" "53693383TI"  "703880359TI" "746617211TI" "808960926TI"
#> [11] "916007042TI" "396251736TI" "250197935TI" "954900876TI" "664845264TI"
#> [16] "731667101TI"
vector_pop(v_f_num, n = 4, ret = "removed") #get the content removed
#> [1] "557189988TI" "163955404TI" "576890078TI" "503808426TI"

#task: remove specific items from vector
#note that this aspect of the functionality ignores the 'n' argument
v_f_num_2 <- paste0(number(6, seed = 33),c("AB")) #simulate 6 numbers using seed and add AB suffix
v_f_num_2 #show numbers
#> [1] "841573640AB" "403211378AB" "803690460AB" "66592309AB"  "820265648AB"
#> [6] "966547367AB"
vector_pop(v_f_num_2, el = c("403211378AB")) #remove 1 specific entries
v_f_num_2 #show results
#> [1] "841573640AB" "803690460AB" "66592309AB"  "820265648AB" "966547367AB"
vector_pop(v_f_num_2, el = c("803690460AB","66592309AB")) #remove 2 specific entries
v_f_num_2 #show results
#> [1] "841573640AB" "820265648AB" "966547367AB"