Allani Pavan
Allani Pavan

Reputation: 1

How to categorize selected numbers in one column and represent them in the form of categories in a new column in R?

I am trying to categorize numbers in the column (mbs_item_number) and represent them in a new column (item_cat) in the form of category.

I used the following code for categorizing few numbers to category '1'.

df$item_cat[mbs_item_number = c(10,12,13,15)] <- 1

I am experiencing the following error message:

Error in `$<-.data.frame`(`*tmp*`, item_cat, value = c(NA, NA, NA, 1,  : 
  replacement has 10943 rows, data has 122412

Upvotes: 0

Views: 40

Answers (1)

akrun
akrun

Reputation: 887213

Here, we can use %in% (used when the length of the vector to be compared is more than 1, if it is 1, then ==) instead of the assignment opertor (=)

df$item_cat[mbs_item_number %in% c(10,12,13,15)] <- 1

Upvotes: 2

Related Questions