[英]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. 我正在尝试对列(mbs_item_number)中的数字进行分类,并以类别的形式在新列(item_cat)中表示它们。
I used the following code for categorizing few numbers to category '1'. 我使用以下代码将一些数字归类为“ 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.