Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenating two vectors in R [duplicate]

I want to concatenate two vectors one after the other in R. I have written the following code to do it:

> a = head(tracks_listened_train)
> b = head(tracks_listened_test)
> a
[1] cc1a46ee0446538ecf6b65db01c30cd8 19acf9a5cbed34743ce0ee42ef3cae3e
[3] 9e7fdbf2045c9f814f6c0bed5da9bed7 3441b1031267fbb6009221bf47f9c5e8
[5] 206c8b79bd02beeea200879afc414879 1a7a95e3845a6815060628e847d14362
18585 Levels: 0001a423baf29add84af6ec58aeb5b90 ...
> b
[1] 7251a7694b79aa9a39f9a1a5f5c8a253 2f362377ef0e7bca112233fdda22a79c
[3] c1196625b1b733b62c43935334e1d190 58e41e462af4185b08231a41453c3faf
[5] 1cc2517fa9c037e02a14ce0950a28f67
10186 Levels: 0001a423baf29add84af6ec58aeb5b90 ...
> res = c(a,b)
> res
[1] 14898  1898 11556  3859  2408  1950  4473  1865  7674  3488  1130

However, I get the unexpected result of the resultant vector. What could the problem be?

like image 490
Asmita Poddar Avatar asked Jun 16 '17 06:06

Asmita Poddar


People also ask

How do I concatenate vectors in R?

To concatenate a vector or multiple vectors in R use c() function. This c() function is used to combine the objects by taking two or multiple vectors as input and returning the vector with the combined elements from all vectors.

How do I make an empty vector in R?

Create Empty Vector using c() One of the most used way to create a vector in R is by using c() combined function. Use this c() function with out any arguments to initialize an empty vector. Use length() function to get the length of the vector. For empty vector the length would be 0.


1 Answers

We need to convert the factor class to character class

c(as.character(a), as.character(b))

The reason we get numbers instead of the character is based on the storage mode of factor i.e. an integer. So when we do the concatenation, it coerces to the integer mode

like image 173
akrun Avatar answered Sep 23 '22 01:09

akrun