Learn R Programming

quickcode (version 1.0.6)

vector_pop: Remove last n elements or specified elements from a vector like array_pop in PHP

Description

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

Usage

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

Value

vector with elements removed

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

Examples

Run this code
# basic example: pop off the last 2 values from vector
c(0,3,"A","Apple", TRUE) #before
vector_pop(c(0,3,"A","Apple", TRUE)) #after 1 pop
vector_pop(c(0,3,"A","Apple", TRUE), n=3) #after 3 pop

# using objects
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
vector_pop(num1) #does not return anything
num1 #num1 vector updated after pop

#task: remove 5 elements from the end, but do not set it to the vector name
num1 #num1 vector before pop
vector_pop(num1,5, ret = TRUE) #return modified vector
num1 #num1 vector remains the same after pop


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

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

#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
vector_pop(v_f_num, n = 4, ret = TRUE) #get the modified vector
vector_pop(v_f_num, n = 4, ret = "removed") #get the content removed

#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
vector_pop(v_f_num_2, el = c("403211378AB")) #remove 1 specific entries
v_f_num_2 #show results
vector_pop(v_f_num_2, el = c("803690460AB","66592309AB")) #remove 2 specific entries
v_f_num_2 #show results

Run the code above in your browser using DataLab