简体   繁体   中英

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. If the data point has 9 digits, I want to drop the first digit. 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

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).

As suggested by @missuse, ifelse() will work fine here.

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

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