# How to split a number into digits in R?

R ProgrammingProgrammingServer Side Programming

To split a number into digits in R, we can use strsplit function by reading the number with as.character and then reading the output with as.numeric.

For example, if we have a number stored into an object called X then we can split the number stored in X into digits by using the command as follows −

as.numeric(strsplit(as.character(X),"")[[1]])

## Example 1

To split a number into digits in R, use the snippet given below −

x1<-2547435
x1

If you execute the above given snippet, it generates the following output −

[1] 2547435

To split a number into digits in R, add the following code to the above snippet −

x1<-2547435
as.numeric(strsplit(as.character(x1),"")[[1]])

## Output

If you execute all the above given codes as a single program, it generates the following output −

[1] 2 5 4 7 4 3 5

## Example 2

To split a number into digits in R, use the snippet given below −

x2<-8789342355
x2

If you execute the above given snippet, it generates the following output −

[1] 8789342355

To split a number into digits in R, add the following code to the above snippet −

x2<-8789342355
as.numeric(strsplit(as.character(x2),"")[[1]])

## Output

If you execute all the above given codes as a single program, it generates the following output −

[1] 8 7 8 9 3 4 2 3 5 5

## Example 3

To split a number into digits in R, use the snippet given below −

x3<-10000
x3

If you execute the above given snippet, it generates the following output −

[1] 10000

To split a number into digits in R, add the following code to the above snippet −

x3<-10000
as.numeric(strsplit(as.character(x3),"")[[1]])

## Output

If you execute all the above given codes as a single program, it generates the following output −

[1] 1 0 0 0 0

## Example 4

To split a number into digits in R, use the snippet given below −

x4<-rpois(1,200)
x4

If you execute the above given snippet, it generates the following output −

[1] 207

To split a number into digits in R, add the following code to the above snippet −

x4<-rpois(1,200)
as.numeric(strsplit(as.character(x4),"")[[1]])

## Output

If you execute all the above given codes as a single program, it generates the following output −

[1] 2 0 7

## Example 5

To split a number into digits in R, use the snippet given below −

x5<-47824135
x5

If you execute the above given snippet, it generates the following output −

[1] 47824135

To split a number into digits in R, add the following code to the above snippet −

x5<-47824135
as.numeric(strsplit(as.character(x5),"")[[1]])

## Output

If you execute all the above given codes as a single program, it generates the following output −

[1] 4 7 8 2 4 1 3 5

## Example 6

To split a number into digits in R, use the snippet given below −

x6<-231404700
x6

If you execute the above given snippet, it generates the following output −

[1] 231404700

To split a number into digits in R, add the following code to the above snippet −

x6<-231404700
as.numeric(strsplit(as.character(x6),"")[[1]])

## Output

If you execute all the above given codes as a single program, it generates the following output −

[1] 2 3 1 4 0 4 7 0 0

## Example 7

To split a number into digits in R, use the snippet given below −

x7<-00142452233
x7

If you execute the above given snippet, it generates the following output −

[1] 142452233

To split a number into digits in R, add the following code to the above snippet −

x7<-00142452233
as.numeric(strsplit(as.character(x7),"")[[1]])

## Output

If you execute all the above given codes as a single program, it generates the following output −

[1] 1 4 2 4 5 2 2 3 3

## Example 8

To split a number into digits in R, use the snippet given below −

x8<-rpois(1,100032474)
x8

If you execute the above given snippet, it generates the following output −

[1] 100035562

To split a number into digits in R, add the following code to the above snippet −

x8<-rpois(1,100032474)
as.numeric(strsplit(as.character(x8),"")[[1]])

## Output

If you execute all the above given codes as a single program, it generates the following output −

[1] 1 0 0 0 3 5 5 6 2

## Example 9

To split a number into digits in R, use the snippet given below −

x9<-rpois(1,2875662365)
x9

If you execute the above given snippet, it generates the following output −

[1] NA

To split a number into digits in R, add the following code to the above snippet −

as.numeric(strsplit(as.character(x9),"")[[1]])

## Output

If you execute all the above given codes as a single program, it generates the following output −

[1] 2 8 7 5 6 7 2 5 3 2

## Example 10

To split a number into digits in R, use the snippet given below −

x10<-rpois(1,924135746)
x10

If you execute the above given snippet, it generates the following output −

[1] 924151778

To split a number into digits in R, add the following code to the above snippet −

x10<-rpois(1,924135746)
as.numeric(strsplit(as.character(x10),"")[[1]])

## Output

If you execute all the above given codes as a single program, it generates the following output −

[1] 9 2 4 1 5 1 7 7 8
Updated on 09-Nov-2021 06:52:58