6

I have a dataframe where the row names have different total digits. There are 152 rows. How do I add leading zeroes when different rows need a different number of leads to achieve the max 8 digits?

row.names
4921103 
42106   
19562106    
11102
3435467
espop23
  • 77
  • 2
  • 2
  • 9

2 Answers2

8

We can try with sprintf (assuming that row.names in the example are numerical)

sprintf("%08d", df1$row.names)
#[1] "04921103" "00042106" "19562106" "00011102" "03435467"

If it is not numeric, convert to numeric and use sprintf

sprintf("%08d", as.numeric(df1$row.names))

If we meant row.names as the rownames of the dataset

row.names(df2) <- sprintf("%08d", as.numeric(row.names(df2)))
row.names(df2)
#[1] "04921103" "00042106" "19562106" "00011102" "03435467"

NOTE: No external packages needed.

data

df1 <- structure(list(row.names = c(4921103L, 42106L, 19562106L, 11102L, 
3435467L)), .Names = "row.names", class = "data.frame", row.names = c(NA, 
-5L))

df2 <- data.frame(v1= 1:5)
row.names(df2) <-  c(4921103L, 42106L, 19562106L, 11102L, 3435467L)
Community
  • 1
  • 1
akrun
  • 874,273
  • 37
  • 540
  • 662
  • This error comes up when I try both: `Error in $<-.data.frame(*tmp*, "row.names", value = character(0)) : replacement has 0 rows, data has 152` – espop23 Jul 20 '16 at 21:41
  • @espop23 Have you tried with the updated post – akrun Jul 20 '16 at 21:52
3

Using stringr:

# If row.names is a column
stringr::str_pad(df$row.names, 8, side = "left", pad = 0)

# If row.names means row names of the dataframe  
stringr::str_pad(row.names(df), 8, side = "left", pad = 0)

[1] "04921103" "00042106" "19562106" "00011102" "03435467"

Check:

abc <- data.frame(A = rep(NA, 5))
row.names(abc) <- c(4921103, 42106, 19562106, 11102, 3435467)

abc
          A
4921103  NA
42106    NA
19562106 NA
11102    NA
3435467  NA

row.names(abc) <- stringr::str_pad(row.names(abc), 8, side = "left", pad = 0)

abc 

          A
04921103 NA
00042106 NA
19562106 NA
00011102 NA
03435467 NA
Sumedh
  • 4,835
  • 2
  • 17
  • 32