A list in R is a specific type of vector with the difference that it can store mixed types like numbers, strings, vectors, another list, a matrix, or even a function as its elements. Decimal values are called numerics in R. It is the default computational data type of R. If we assign a decimal value to a variable then it will be of numeric type. If we assign an integer to a variable, it is still being saved as a numeric value.
Convert R List to Numeric Value
To convert R List to Numeric value, use the combination of the unlist() function and as.numeric() function. The unlist() function in R produces a vector that contains all the atomic components. To convert factorial value to numeric value in R, use the as.numeric() function.
Code snippet for converting list to numeric value
The first thing we will define is vectors.
rv1 <- 1:5 rv2 <- 6:10
Then use the list() function to create a list from vectors.
rv1 <- 1:5 rv2 <- 6:10 data <- list(rv1, rv2) data
[]  1 2 3 4 5 []  6 7 8 9 10
Finally, use the unlist() and as.numeric() function to convert a list to a numeric value.
rv1 <- 1:5 rv2 <- 6:10 data <- list(rv1, rv2) num <- as.numeric(unlist(data)) num
 1 2 3 4 5 6 7 8 9 10
You can see that the final output is a numeric value, and to check its data type, use the typeof() function.
It will give us the double as an output that means it’s a numeric value. The numeric data type is identical to double (and real ). It creates a double-precision vector of the specified length with each element equal to 0.
If the values are of type factor, then you should convert them using the following code snippet.
That is it for converting a list to a numeric value in R programming.
Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has written many programming blogs, which showcases his vast expertise in this field.