R append to vector

In this article you will learn how to append to a vector in R programming also called vector merging or adding values. How to append a single value, a series, or another vector at the beginning, end or at any desired position in a given vector.

Syntax of R append append() function is used to add elements to a given vector. This function takes atleast two arguments and atmost three arguments. Lets see the syntax

append(vector, data, after)

1. append() is the function which will add elements to a vector.

2. vector is the first argument. It is the vector in which values are going to be added. This argument is necessary.

3. data is the data, value or values, vector to be added in the vector(first argument) of append function. This argument is also needed for append.

4. after is the subscript after which values are to be appended.

R append Example:

Append single value to vector:

If you want to add one value 6 at the end of a given vector a, then the R code will be

> a <- c(1,2,3,4,5)

> append(a,6)
 1 2 3 4 5 6

Here a is a vector consisting of five values and we append 6 at the end of it.

Similarly, another example

> x <- c(letters[1:5])
> x
 "a" "b" "c" "d" "e"
> y <- append(x,letters[6:10])
> y
 "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"

Append vector to a vector:

If you want to add a vector y to x

> x <- c(1:4)
> y <- c(5:8)

> append(x,y)
 1 2 3 4 5 6 7 8

Write the first vector and then the vector which is to be appended. Here x is the first vector which consists of four numbers 1,2,3,4 and y is the second vector consisting 5,6,7,8. Now y is added to x by simply writing append(x,y)

Append at a specific location:

If a value or a vector is to be added in a given vector at a specific location then the third argument "after" is used. It is simply the subscript after which values are to be added. If you want to add after the first value in list, use 1 as argument, if you want to add after the second value, use 2 as argument and so on.

> i <- c('a','f')
> i
 "a" "f"
> append(i, letters[2:5],1)
 "a" "b" "c" "d" "e" "f"

See another example

> s <- c(1:5)
> append(s,20:23,2)
  1  2 20 21 22 23  3  4  5

The first vector s contains numbers from 1 to 5. Four values from 20 to 23 are added after the second value in s.

Learn to Shine

• Programming made Easy for all
• High School and College
• University level
• Professionals doctors, engineers, scientists
• Data Analysts
{"ft_lang_label":"__label__en","ft_lang_prob":0.87041676,"math_prob":0.9530387,"size":2135,"snap":"2019-43-2019-47","text_gpt3_token_len":675,"char_repetition_ratio":0.16330361,"word_repetition_ratio":0.07192575,"special_character_ratio":0.3325527,"punctuation_ratio":0.11821705,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99770284,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,3,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-10-15T17:05:56Z\",\"WARC-Record-ID\":\"<urn:uuid:7432da5b-f833-4f0c-8330-54c6a99e6992>\",\"Content-Length\":\"12896\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:cbf15fed-1b14-4bb8-b418-aa32c6f8c0e1>\",\"WARC-Concurrent-To\":\"<urn:uuid:f37e102a-3650-4bdd-960a-9e230d9a4cc4>\",\"WARC-IP-Address\":\"85.187.136.2\",\"WARC-Target-URI\":\"https://www.learn-r.org/r-tutorial/append-vector.php\",\"WARC-Payload-Digest\":\"sha1:HNTPKEZUC3VUQRAES5YEWN75YLHSNXM5\",\"WARC-Block-Digest\":\"sha1:LHC2DXL4NAZWWZK3GYDKJE22FALBDMHP\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-43/CC-MAIN-2019-43_segments_1570986660067.26_warc_CC-MAIN-20191015155056-20191015182556-00385.warc.gz\"}"}