[英]Create a new column based on conditional statement of another column R
I am trying to create a new column that will return Yes or No based on conditions of another column.我正在尝试创建一个新列,它将根据另一列的条件返回是或否。
I have tried to use grepl
, substr
and nchar
, ifelse
.我曾尝试使用grepl
、 substr
和nchar
、 ifelse
。 The issue is that the data has characters and numbers in the column that I am trying to condition.问题是数据在我试图调整的列中包含字符和数字。
(nchar(as.character(expo$BuyerNumber)) == 3)
(substr(as.character(expo$BuyerNumber),1,1 == 6))
I want to create aa new column that will have yes or no if the following conditions are met.如果满足以下条件,我想创建一个新列,该列将具有是或否。 If the first number starts with 6 and is of length 3 then yes if not then no will be returned.如果第一个数字以 6 开头并且长度为 3,则返回 yes,否则返回 no。
Examples:例子:
610
610B
620C
All above would return yes以上都将返回是
426
62B
21C
All above would return no以上所有都将返回 no
One problem, I see, might be that you are comparing substr (which will give a character) to a number.我看到的一个问题可能是您正在将 substr (它将给出一个字符)与一个数字进行比较。 That is likely to give false boolean.这很可能会给出错误的 boolean。 Something like below.像下面的东西。
(nchar(as.character(expo$BuyerNumber)) == 3)
(as.integer(substr(as.character(expo$BuyerNumber),1,1)) == 6)
If it does not work, please let me know.如果它不起作用,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.