How to select rows in a column and add a zero to selected rows
2
0
Entering edit mode
4 months ago
Phylicia ▴ 10

Hello! I have already written a code for this question, but my code is very complex. I want to enquiry, is there any simpler way to achieve the same goal. Thanks a lot!

Background: There is a column named id, in which there are 5 digit numbers and 6 digit numbers.

Aim: I want to select 5 digit numbers and add a zero in front of them and merge added-0 items with the original 6 digit numbers.

df is the name of the data frame.

#Select the columns whose ID digit is only 5 and add 0 in front of it.
df1 <- subset(df, nchar(as.character(df$id)) == 5)

df1 <- df1 %>% mutate("NewID"= paste0("0", df1$id))

#Select the columns whose ID digit is only 6 and merge it with the first subset
df2 <- subset(df, nchar(as.character(df$id)) == 6)

df2 <- df2 %>% mutate("NewID"= df2$id)
#Merge them
df3 <- rbind(df1, df2)
data clean • 548 views
ADD COMMENT
3
Entering edit mode
4 months ago

Example data.

df <- data.frame(id=c(12345, 123456), val=1:2)

> df
      id val
1  12345   1
2 123456   2

Using sprintf from base R.

df$id <- sprintf("%06d", df$id)

With the str_pad function from stringr.

df <- mutate(df, id=str_pad(id, 6, "left", 0))

Result of either method.

> df
      id val
1 012345   1
2 123456   2
ADD COMMENT
2
Entering edit mode

a typo or copy/paste issue in this line df <- mutate(df, id=str_pad(id, 6, "left")). In addition, for long numbers, user must set scipen so that number is not turned to e notation, with str_pad. str_pad has some weird behavior (from user point of view, if scipen is not set)

> str_pad(10000,8, "right", pad=0)
[1] "10000000"
> str_pad(100000, 7, "right", pad=0)
[1] "1e+0500"
> str_pad(1234000000, 7, "right", pad=0)
[1] "1.234e+09"
ADD REPLY
0
Entering edit mode

a typo or copy/paste issue in this line df <- mutate(df, id=str_pad(id, 6, "left"))

Forgot to add 0 padding to it, thanks for pointing it out!

ADD REPLY
0
Entering edit mode

Thanks a lot for your kind help!

ADD REPLY
2
Entering edit mode
4 months ago
erwan.scaon ▴ 870

Hello,

# Packages
library(magrittr)
library(dplyr)
# Dummy data
test <- data.frame(
  id = c(12345, 123456, 54321, 654321),
  stuff = LETTERS[1:4]
)
> test
      id stuff
1  12345     A
2 123456     B
3  54321     C
4 654321     D
# Mutate the "id" column
# If less than 6 digits, return "suffix + id", else return "id"
test %<>%
  mutate(id = ifelse(nchar(id) < 6, paste0(0, id), id))
> test
      id stuff
1 012345     A
2 123456     B
3 054321     C
4 654321     D
ADD COMMENT
0
Entering edit mode

Thanks a lot for your kind help!

ADD REPLY

Login before adding your answer.

Traffic: 1787 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6