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 data is the list consists of vectors.
The first thing we will define is vectors.
Then use the list[] function to create a list from vectors.
rv1 >> l = list[range[10]]
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
To create a list of integers between N and M [with M>N], the function range[] can still be used: To create a list of floats between [N,M] with a given step, a solution is to use the numpy function called arange: To create a list of floats between [N,M] with a given number of elementsa solution is to use the numpy function linspace:Create a list of floats using arange
Create a list of floats using linspace
Create a list of random integers
The python function randint can be used to generate a random integer in a chosen interval [a,b]:
>>> import random >>> random.randint[0,10] 7 >>> random.randint[0,10] 0A list of random numbers can be then created using python list comprehension approach:
>>> l = [random.randint[0,10] for i in range[5]] >>> l [4, 9, 8, 4, 5]Another solution is to use randrange function [except that can specify a step if you need]:
>>> l = [random.randrange[0,10] for i in range[5]] >>> l [1, 7, 4, 3, 1] >>> l = [random.randrange[0,10,2] for i in range[5]] >>> l [2, 4, 4, 6, 0]A third solution is to create a list of random numbers with no repetition, is to use random.sample function
References
Lists are the R objects which contain elements of different types like − numbers, strings, vectors and another list inside it. A list can also contain a matrix or a function as its elements. List is created using list[] function.
Creating a List
Following is an example to create a list containing strings, numbers, vectors and a logical values.
# Create a list containing strings, numbers, vectors and a logical # values. list_data