# A tibble: 5 x 3
x wanted length_of_x
<chr> <chr> <int>
1 0 0000000000000 1
2 001570098 0000001570098 9
3 9770025215000 9770025215000 13
4 97725320530072 9.77253205E13 14
5 303401110309080105010401 03.0340111E23 24
Given x
, how can I mutate wanted
with the following conditions:
- When the length of
x
is greater than 13,wanted
gets expressed in scientific notation. See rows 4 and 5. - When the length of
x
is less than 13,wanted
gets padded with 0s. See rows 1 to 3. - Then length of
wanted
is 13.
library(tidyverse)
# Toy data
df <- tibble(x = c("0", "001570098", "9770025215000", "97725320530072",
"303401110309080105010401"),
wanted = c("0000000000000", "0000001570098",
"9770025215000", "9.77253205E13", "03.0340111E23"),
length_of_x = c(1L,9L, 13L, 14L, 24L))