Shorthand to add elements to a vector and save as the same name
Arguments
- .
first vector
- add
vector to add
- unique
remove duplicated entries
- rm.na
remove NA values
- rm.empty
remove empty values
Details
Note that two vectors are required in order to use this function. Also, note that the final result replaces the content of the first vector. This means that the original content of the 'first vector' will no longer exist after this function executes.
Use case
This function allows the combination of two vectors in one short line of code. It allows specification of further downstream filtering of the resulting vector such as selecting only unique items, removing NA or empty values. It simplifies a code chunk with many lines of code to concatenate and filter various vectors.
Examples
num1 <- number(10, seed = 45)
num2 <-"rpkg.net"
num1
#> [1] 572805451 438717498 978488843 792658267 510648679 249329062 776169756
#> [8] 922428497 682900711 554652132
num2
#> [1] "rpkg.net"
#Task: add num2 to num1 and re-save as num1
vector_push(num1,num2)
num1 #updated with num2
#> [1] "572805451" "438717498" "978488843" "792658267" "510648679" "249329062"
#> [7] "776169756" "922428497" "682900711" "554652132" "rpkg.net"
num2 #not updated
#> [1] "rpkg.net"
#Task: concatenate two vectors and remove duplicates
vector1 = number(4,seed = 5)
vector2 = number(8,seed = 5)
vector3 = number(12,seed = 5)
vector1 #length is 4
#> [1] 859942763 716720335 449491833 120114901
vector2 #length is 8
#> [1] 859942763 716720335 449491833 120114901 886905927 100040083 293768998
#> [8] 54080431
vector3 #length is 12
#> [1] 859942763 716720335 449491833 120114901 886905927 100040083 293768998
#> [8] 54080431 590668620 235919258 602192000 907688393
# with duplicated
vector_push(vector1,vector2, unique = FALSE)
vector1 #return modified vector
#> [1] 859942763 716720335 449491833 120114901 859942763 716720335 449491833
#> [8] 120114901 886905927 100040083 293768998 54080431
length(vector1) #length is 12 because nothing was removed
#> [1] 12
#duplicates in vector1 is 886905927 100040083 293768998 54080431
# without duplicated
vector_push(vector2,vector3, unique = TRUE)
vector2 #return modified vector
#> [1] 859942763 716720335 449491833 120114901 886905927 100040083 293768998
#> [8] 54080431 590668620 235919258 602192000 907688393
length(vector2) #length is 12 instead of 20
#> [1] 12
#Total of 8 duplicated numbers were removed
#Task: concatenate two vector and remove NA values
vector1 = number(5)
vector2 = c(4,NA,5,NA)
vector3 = number(5)
# with NA
vector_push(vector1,vector2, rm.na = FALSE)
vector1 #return modified vector
#> [1] 601193180 804976458 609547386 836308787 108005370 4 NA
#> [8] 5 NA
# without NA
vector_push(vector3,vector2, rm.na = TRUE)
vector3 #return modified vector
#> [1] 62225592 408656141 634283415 283786294 330139794 4 5
#Task: concatenate two vector and remove empty values
vector1 = number(5)
vector2 = c(4,'',5,'',NULL,' ')
vector3 = number(5)
# with empty
vector_push(vector1,vector2, rm.empty = FALSE)
vector1 #return modified vector
#> [1] "768576704" "43520394" "187277824" "577999096" "738720212" "4"
#> [7] "" "5" "" " "
# without empty
vector_push(vector3,vector2, rm.empty = TRUE)
vector3 #return modified vector
#> [1] "786129300" "23871979" "845619948" "532425851" "500104518" "4"
#> [7] "5" " "