简体   繁体   中英

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

在这里,我们可以使用%in% (当要比较的vector的长度大于1时使用,如果为1 ,则== ),而不是赋值运算符( =

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM