简体   繁体   English

如何在R的新列中将选定的数字归为一列并以类别的形式表示它们?

[英]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.

相关问题 如何将 1 列中的数据分类为 R 中的新变量? - How to categorize data from 1 column into a new variable in R? R函数按列分类? - R function categorize by column? 如何使用R在第三列中根据日期顺序查找一列中的重复项并在新列中标记它们 - How to find duplicates in one column and tag them in a new column depending on date order in a third column using R 如何使用 R 中的多个列来表示一列的值,其中一个新列是有条件的? - How can I represent one column's values using multiple columns in R where one new column is conditional? 将值分类为一列,然后将其打印到R中的新列中 - Categorising values in one column and printing them in a new column in R 如何生成一个新的数字列来表示R中的字符序列? - How to generate a new numeric column to represent a sequence of characters in R? 如何根据 R 中的其他列对特定列中的值进行分类 - How to categorize value in particular column based on other column in R 如何添加具有 6 个类别的新列? - How to add a new column with 6 categories? 如何通过 r 中的选定行创建新列并添加列名 - How create new column an add column names by selected row in r 在 R 中创建包含三个字符串类别的新列 - Make new column with three string categories in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM