How to split a number into digits in R?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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: 2021-11-09T06:52:58+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements