简体   繁体   English

在 R 中的列中删除某些数据点的数字

[英]drop a digit for some data points in a column in R

I have a column of numbers (characters) which has either 8 digits or 9 digits data.我有一列数字(字符),其中包含 8 位或 9 位数据。 If the data point has 9 digits, I want to drop the first digit.如果数据点有 9 位数字,我想删除第一个数字。 I'm using the following command:我正在使用以下命令:

file$hscode2 <- if (nchar(file$hscode1 >= 9)) {

   file$hscode2 <- substr(file$hscode1,2,9)

}

where the data frame is "file" and the column with 8/9 digits data is hscode1 and the new column which drops the first digit when it is 9 digit character is hscode2其中数据框是“文件”,具有 8/9 位数据的列是 hscode1,当它是 9 位字符时删除第一个数字的新列是 hscode2

However, I'm not getting the desired result.但是,我没有得到想要的结果。 Any suggestions?有什么建议?

Thanks谢谢

I think there is a bug.我认为有一个错误。 It should be:它应该是:

file$hscode2 <- if (nchar(file$hscode1) >= 9) {

    file$hscode2 <- substr(file$hscode1,2,9)

}

As written, your function was running nchar on "file$hscode1 >= 9" which is a boolean, which if converted to a char would just be 1 character, hence the conditions would always have been true I think (leading to the unexpected results you were seeing).如所写,您的函数在 "file$hscode1 >= 9" 上运行 nchar,这是一个布尔值,如果转换为 char 将只是 1 个字符,因此我认为条件始终为真(导致意外结果你看到了)。

As suggested by @missuse, ifelse() will work fine here.正如@missuse 所建议的, ifelse()在这里可以正常工作。

file$hscode2 <- ifelse(nchar(file$hscode1 >= 9), # test
                       substr(file$hscode1, 2, 9), # value if true
                       file$hscode1) # value if false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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