使用case_when填写字符串

时间:2018-09-29 19:27:22

标签: r

我正在尝试使用case_when来根据字符串的长度pad在R中的字符串。

我以长度为11、12和13的以下3个示例为例:

V1                    V2
74300000330          00074300000330
811693200042         08011693200042
8829999820128        88029999820128

V1是我要与V2匹配的列

V1中的第一行有11位数字,如果该行有11位数字,则在数字开头添加3个零。

我尝试了以下代码,但没有遇到任何麻烦(我也尝试过使用paste0());

df %>% 
  mutate(col3 = case_when(length(col1) == 11 ~ str_pad(14, width = 3, pad = "0")))

第二个数字有12个数字,所以我应该在数字的开头添加一个零,然后在(从左数起)第一个数字和(从右数起)第11个数字之间添加另一个零,因此第2行从81169...0801169...

第三行有13位数字,因此我应该在(从左数起)第二个数字和(从右数起)第11个数字之间粘贴一个零。因此,序列的开头从88299880299

序列中的总位数应恰好为14。

数据:

df <- structure(list(col1 = structure(c(1L, 1L, 1L, 2L, 2L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 
4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 11L, 12L, 12L, 13L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 21L, 
21L, 21L, 22L, 22L, 22L, 22L, 22L, 23L, 23L, 23L, 23L, 23L, 23L, 
23L, 23L, 23L, 24L, 24L, 24L, 24L, 25L, 26L, 27L, 27L, 27L, 27L, 
27L, 27L, 27L, 27L, 27L, 27L, 28L, 28L, 28L, 29L, 30L, 30L, 30L, 
31L, 32L, 33L, 33L, 33L, 33L, 33L, 34L, 34L, 34L, 34L, 35L, 36L, 
36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 37L, 38L, 38L, 38L, 38L, 
38L, 39L, 39L, 39L, 39L, 40L, 41L, 41L, 41L, 42L, 42L, 43L, 44L, 
45L, 45L, 45L, 45L, 45L, 46L, 46L, 47L, 47L, 47L, 47L, 47L, 47L, 
47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 
47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 
47L, 48L, 49L, 49L, 49L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 
50L, 50L, 50L, 50L, 50L, 50L, 51L, 51L, 51L, 51L, 51L, 51L, 51L, 
51L, 51L, 51L, 51L, 52L, 52L, 53L, 53L, 53L, 53L, 54L, 55L, 56L, 
56L, 56L, 56L, 56L, 56L, 56L, 56L, 57L, 58L, 59L, 59L, 60L, 60L, 
60L, 60L, 60L, 60L, 60L, 60L, 60L, 60L, 60L, 61L, 61L, 61L, 61L, 
61L, 62L, 62L, 63L, 64L, 65L, 66L, 66L, 66L, 66L, 66L, 66L, 66L, 
66L, 66L, 66L, 66L, 66L, 66L, 66L, 66L, 66L, 66L, 67L, 67L, 68L, 
68L, 69L, 69L, 69L, 70L, 70L, 70L, 70L, 70L, 70L, 71L, 71L, 71L, 
71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 
71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 72L, 72L, 
72L, 72L, 72L, 72L, 72L, 72L, 72L, 72L, 72L, 72L, 72L, 72L, 72L, 
73L, 73L, 73L, 73L, 73L, 73L, 73L, 73L, 73L, 73L, 73L, 73L, 74L, 
74L, 74L, 74L, 74L, 75L, 75L, 75L, 76L, 77L, 77L, 78L, 79L, 80L, 
81L, 82L, 83L, 83L, 83L, 83L, 83L, 83L, 83L, 83L, 83L, 84L, 84L, 
84L, 85L, 86L, 86L, 87L, 87L, 87L, 87L, 88L, 89L, 90L, 91L, 92L, 
93L, 93L, 93L, 94L, 94L, 95L, 95L, 95L, 95L, 95L, 96L, 97L, 97L, 
97L, 98L, 99L, 100L, 100L, 100L, 100L, 101L, 102L, 102L, 103L, 
104L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 106L, 
107L, 107L, 108L, 109L, 109L, 109L, 109L, 109L, 109L, 109L, 110L, 
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 111L, 111L, 111L, 
111L, 112L, 112L, 112L, 112L, 112L, 112L, 112L, 113L, 113L, 113L, 
113L, 113L, 113L, 114L, 114L, 114L, 114L, 114L, 114L, 114L, 114L, 
115L, 116L, 116L, 117L, 117L, 117L, 118L, 118L, 118L, 118L, 118L, 
118L, 118L, 118L, 118L, 118L, 119L, 119L, 119L, 119L, 119L, 119L, 
119L, 119L, 119L, 120L, 120L, 120L, 121L, 122L, 122L, 122L, 122L, 
122L, 122L, 122L, 123L, 123L, 123L, 123L, 123L, 123L, 123L), .Label = c("11114110010", 
"11114110022", "11114110029", "11114110036", "11114110210", "11114110230", 
"11114110261", "11114110271", "11114110281", "11114110291", "11114110316", 
"11114110526", "11780900029", "11780900050", "11780900660", "11780900661", 
"12451500878", "12451567602", "12550000033", "12550000365", "12550000366", 
"12550000367", "12550000371", "12550000376", "12550000377", "12550000384", 
"12550000388", "12550000392", "12550000393", "12550000397", "12550000401", 
"12550000402", "12550000538", "12550006763", "12550006764", "12550020040", 
"12550020042", "12550020043", "12550020044", "12550020188", "12550020204", 
"12550020212", "12550090015", "12800046631", "12800063141", "12800070612", 
"14300002922", "14300002923", "14300002924", "14300002925", "14300002934", 
"14300002940", "14300002941", "14300002942", "14300003300", "14300004091", 
"14300004296", "14300004299", "14300004301", "14300004648", "14300004650", 
"14300004651", "14300070522", "15543760143", "15543760145", "15543760186", 
"15543760235", "15543760253", "17089302817", "17103800044", "17103800047", 
"17103800048", "17103800053", "17103800056", "17103800058", "17103800059", 
"17103801173", "17103801175", "17232305018", "17447100091", "17510100575", 
"17510100576", "17510121064", "17510121065", "17510181458", "17732447059", 
"17762300048", "17762300060", "18903644280", "19955508003", "19955508050", 
"19955508060", "19955508061", "19955508531", "19955508534", "19955508758", 
"19955508792", "19955508800", "19955508801", "19955508832", "19955508992", 
"19955509803", "19955538570", "19955538696", "19955538725", "19955538792", 
"21291912261", "21780900078", "22550081121", "22550081122", "22800025406", 
"22800030050", "24300070590", "25543760142", "25543760521", "29955539550", 
"31291912240", "39955508520", "41114110525", "57103800060", "74300000330", 
"8,11693E+11", "8,83E+12"), class = "factor"), col2 = structure(c(1L, 
1L, 1L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 7L, 
7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 11L, 12L, 12L, 13L, 13L, 14L, 15L, 
16L, 17L, 18L, 19L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 
20L, 20L, 20L, 20L, 21L, 21L, 21L, 22L, 22L, 22L, 22L, 22L, 23L, 
23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 24L, 24L, 24L, 24L, 25L, 
26L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 28L, 28L, 
28L, 29L, 30L, 30L, 30L, 31L, 32L, 33L, 33L, 33L, 33L, 33L, 34L, 
34L, 34L, 34L, 35L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 
37L, 38L, 38L, 38L, 38L, 38L, 39L, 39L, 39L, 39L, 40L, 41L, 41L, 
41L, 42L, 42L, 43L, 44L, 45L, 45L, 45L, 45L, 45L, 46L, 46L, 47L, 
47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 
47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 47L, 
47L, 47L, 47L, 47L, 47L, 47L, 48L, 49L, 49L, 49L, 50L, 50L, 50L, 
50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 51L, 51L, 
51L, 51L, 51L, 51L, 51L, 51L, 51L, 51L, 51L, 52L, 52L, 53L, 53L, 
53L, 53L, 54L, 55L, 56L, 56L, 56L, 56L, 56L, 56L, 56L, 56L, 57L, 
58L, 59L, 59L, 60L, 60L, 60L, 60L, 60L, 60L, 60L, 60L, 60L, 60L, 
60L, 61L, 61L, 61L, 61L, 61L, 62L, 62L, 63L, 64L, 65L, 66L, 66L, 
66L, 66L, 66L, 66L, 66L, 66L, 66L, 66L, 66L, 66L, 66L, 66L, 66L, 
66L, 66L, 67L, 67L, 68L, 68L, 69L, 69L, 69L, 70L, 70L, 70L, 70L, 
70L, 70L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 
71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 
71L, 71L, 71L, 72L, 72L, 72L, 72L, 72L, 72L, 72L, 72L, 72L, 72L, 
72L, 72L, 72L, 72L, 72L, 73L, 73L, 73L, 73L, 73L, 73L, 73L, 73L, 
73L, 73L, 73L, 73L, 74L, 74L, 74L, 74L, 74L, 75L, 75L, 75L, 76L, 
77L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 83L, 83L, 83L, 83L, 83L, 
83L, 83L, 83L, 84L, 84L, 84L, 85L, 86L, 86L, 87L, 87L, 87L, 87L, 
88L, 89L, 90L, 91L, 92L, 93L, 93L, 93L, 94L, 94L, 95L, 95L, 95L, 
95L, 95L, 96L, 97L, 97L, 97L, 98L, 99L, 100L, 100L, 100L, 100L, 
101L, 102L, 102L, 103L, 104L, 105L, 105L, 105L, 105L, 105L, 105L, 
105L, 105L, 105L, 106L, 107L, 107L, 108L, 109L, 109L, 109L, 109L, 
109L, 109L, 109L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 
110L, 111L, 111L, 111L, 111L, 112L, 112L, 112L, 112L, 112L, 112L, 
112L, 113L, 113L, 113L, 113L, 113L, 113L, 114L, 114L, 114L, 114L, 
114L, 114L, 114L, 114L, 115L, 116L, 116L, 117L, 117L, 117L, 118L, 
118L, 118L, 118L, 118L, 118L, 118L, 118L, 118L, 118L, 119L, 119L, 
119L, 119L, 119L, 119L, 119L, 119L, 119L, 120L, 120L, 120L, 121L, 
123L, 122L, 123L, 123L, 123L, 123L, 123L, 127L, 124L, 126L, 126L, 
127L, 127L, 125L), .Label = c("00011114110010", "00011114110022", 
"00011114110029", "00011114110036", "00011114110210", "00011114110230", 
"00011114110261", "00011114110271", "00011114110281", "00011114110291", 
"00011114110316", "00011114110526", "00011780900029", "00011780900050", 
"00011780900660", "00011780900661", "00012451500878", "00012451567602", 
"00012550000033", "00012550000365", "00012550000366", "00012550000367", 
"00012550000371", "00012550000376", "00012550000377", "00012550000384", 
"00012550000388", "00012550000392", "00012550000393", "00012550000397", 
"00012550000401", "00012550000402", "00012550000538", "00012550006763", 
"00012550006764", "00012550020040", "00012550020042", "00012550020043", 
"00012550020044", "00012550020188", "00012550020204", "00012550020212", 
"00012550090015", "00012800046631", "00012800063141", "00012800070612", 
"00014300002922", "00014300002923", "00014300002924", "00014300002925", 
"00014300002934", "00014300002940", "00014300002941", "00014300002942", 
"00014300003300", "00014300004091", "00014300004296", "00014300004299", 
"00014300004301", "00014300004648", "00014300004650", "00014300004651", 
"00014300070522", "00015543760143", "00015543760145", "00015543760186", 
"00015543760235", "00015543760253", "00017089302817", "00017103800044", 
"00017103800047", "00017103800048", "00017103800053", "00017103800056", 
"00017103800058", "00017103800059", "00017103801173", "00017103801175", 
"00017232305018", "00017447100091", "00017510100575", "00017510100576", 
"00017510121064", "00017510121065", "00017510181458", "00017732447059", 
"00017762300048", "00017762300060", "00018903644280", "00019955508003", 
"00019955508050", "00019955508060", "00019955508061", "00019955508531", 
"00019955508534", "00019955508758", "00019955508792", "00019955508800", 
"00019955508801", "00019955508832", "00019955508992", "00019955509803", 
"00019955538570", "00019955538696", "00019955538725", "00019955538792", 
"00021291912261", "00021780900078", "00022550081121", "00022550081122", 
"00022800025406", "00022800030050", "00024300070590", "00025543760142", 
"00025543760521", "00029955539550", "00031291912240", "00039955508520", 
"00041114110525", "00057103800060", "00074300000330", "08011693200041", 
"08011693200042", "88029999819907", "88029999820074", "88029999820083", 
"88029999820128"), class = "factor")), row.names = c(NA, -513L
), class = "data.frame")

1 个答案:

答案 0 :(得分:1)

这里有几个问题。您的列似乎是因素,当您将字符串函数应用于它们时,它们可能会造成混乱的问题。您希望他们成为角色,而不是因素。检查字符串长度的正确方法是使用console.log(UserMurderCoins); ReferenceError: UserMurderCoins is not defined (扰流警报:不适用于因子数据!)。

您的填充规则似乎有些武断,但是下面的方法应该起作用。为了在数字字符串“内”填充sql.get(`SELECT * FROM monney WHERE userId ="${message.author.id}"`) .then(row => {var UserMurderCoins = row.coins}); console.log(UserMurderCoins); 和正则表达式,可以产生奇迹。

nchar