user3511563
user3511563

Reputation: 397

cbind returns index of factor vector

I'm trying to concatenate 2 vectors, a vector of factors and a vector of character with the following code:

results2 <- cbind(customer, result)

The problem is that the vector customers is being concatenated with the row numbers, not the actual value of the customer factor.

It should return this

customer result
10         a
22         b
25         a

But instead it's returning this:

customer result
1         a
2         b
3         a

Upvotes: 1

Views: 1396

Answers (1)

talat
talat

Reputation: 70266

That is because by using cbind you convert your data to a matrix object and matrices in R can only contain one type of objects / class. So your factor variable customer is converted and only the factor levels remain (the 1, 2, 3 are not row names but the "numbering" of factor levels). If you just want to create a data.frame you could use

data.frame(customers, results)

instead, bc data.frames allow different types of variables combined in one data.frame.

Upvotes: 6

Related Questions