Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
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 −
x1If you execute the above given snippet, it generates the following output −
[1] 2547435To split a number into digits in R, add the following code to the above snippet −
x1Output
If you execute all the above given codes as a single program, it generates the following output −
[1] 2 5 4 7 4 3 5Example 2
To split a number into digits in R, use the snippet given below −
x2If you execute the above given snippet, it generates the following output −
[1] 8789342355To split a number into digits in R, add the following code to the above snippet −
x2Output
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 5Example 3
To split a number into digits in R, use the snippet given below −
x3If you execute the above given snippet, it generates the following output −
[1] 10000To split a number into digits in R, add the following code to the above snippet −
x3Output
If you execute all the above given codes as a single program, it generates the following output −
[1] 1 0 0 0 0Example 4
To split a number into digits in R, use the snippet given below −
x4If you execute the above given snippet, it generates the following output −
[1] 207To split a number into digits in R, add the following code to the above snippet −
x4Output
If you execute all the above given codes as a single program, it generates the following output −
[1] 2 0 7Example 5
To split a number into digits in R, use the snippet given below −
x5If you execute the above given snippet, it generates the following output −
[1] 47824135To split a number into digits in R, add the following code to the above snippet −
x5Output
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 5Example 6
To split a number into digits in R, use the snippet given below −
x6If you execute the above given snippet, it generates the following output −
[1] 231404700To split a number into digits in R, add the following code to the above snippet −
x6Output
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 0Example 7
To split a number into digits in R, use the snippet given below −
x7If you execute the above given snippet, it generates the following output −
[1] 142452233To split a number into digits in R, add the following code to the above snippet −
x7Output
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 3Example 8
To split a number into digits in R, use the snippet given below −
x8If you execute the above given snippet, it generates the following output −
[1] 100035562To split a number into digits in R, add the following code to the above snippet −
x8Output
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 2Example 9
To split a number into digits in R, use the snippet given below −
x9If you execute the above given snippet, it generates the following output −
[1] NATo 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 2Example 10
To split a number into digits in R, use the snippet given below −
x10If you execute the above given snippet, it generates the following output −
[1] 924151778To split a number into digits in R, add the following code to the above snippet −
x10Output
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
