0

I have found quite a few threads on each part of the code snippet I am trying to create/use.. but not in the way(s) I am trying to do it.

I have a dataframe of customer information.

1 column is a customer ID (CID), the 2nd column is the customer specific identifier (CSI)

That means customer a single customer id can represent many specific customers from a bigger pool, and the CSI tells me which specific customer from that pool I am looking at.

Data would look like this:

data.frame("CID"=c("1","2","3","4","1","2","3","4"),
           "Customer_Pool"=c("Art_Supplies", "Automotive_Supplies", "Office_Supplies", "School_Supplies",
                             "Art_Supplies", "Automotive_Supplies", "Office_Supplies", "School_Supplies"),
           "CSI"=c("01","01","01","01","02","02","02","02"),
           "Customer_name"=c("Janet","Jane", "Jill", "Jenna", "Joe", "Jim", "Jack", "Jimmy"))

I am trying to combine the CID and CSI numbers.. the problem is I need all the CID to be double digit (01 instead of 1 for example) to match the CID from 10-99

Here is what I have been trying:

DF <- DF %>% mutate(CID = if_else(str_length(CID = 1),
                                 str_pad(CID, width = 2, side = "left), CID))

The error I am getting says: error in str_length(CID = 1): unused argument (CID = 1)

How would I correct this?

Progman
  • 16,827
  • 6
  • 33
  • 48
Checht
  • 45
  • 10
  • Also `df$CID <- sprintf('%02s', df$CID)`. See https://stackoverflow.com/questions/5812493/how-to-add-leading-zeros – Ronak Shah Oct 27 '20 at 01:44

2 Answers2

1

You have some syntax issues here. Try

DF <- DF %>% mutate(CID = if_else(str_length(CID) == 1,
                                  str_pad(CID, width = 2, side = "left", pad="0"), CID))

When you call str_length(CID = 1), it looks like you are passing a parameter named "CID" to str_length which it knows nothing about. Rather, you wan to take the string length of CID and then compare that to 1 with == to test for equality (not = which is for parameter names and assignments).

But really the if_else isn't necessary here. If everyhing has to be 2 digits, then just do

DF <- DF %>% mutate(CID = str_pad(CID, width = 2, side = "left", pad="0"))

str_pad will only pad when needed.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • This 'runs' without error.. but its not changing the value of my column at all (in my real data the column is 'character'.. not a factor like the test data.. will that make a difference? Your suggested edit runs without error** clarifying that. – Checht Oct 26 '20 at 22:03
  • Oh, it looks like you were padding with spaces in your attempts but you wanted to be padding with zeros. I've updated the code. – MrFlick Oct 26 '20 at 22:06
  • Thank you.. I feel REALLY dumb now.. I have been trying to get that if_else working for so long I forgot about the actual padded value... Many appreciations! – Checht Oct 26 '20 at 22:10
0

Base R solution:

df$p_key <- with(df, paste(ifelse(nchar(CID) == 1, paste0("0", CID), CID), CSI, sep = "-"))

Tidyverse using Mr Flick's clean solution:

library(tidyverse)
df %>%
  mutate(p_key = str_c(str_pad(CID, width = 2, side = "left", , pad = "0"), CSI, sep = "-"))
hello_friend
  • 5,682
  • 1
  • 11
  • 15