R create list of integers
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. Show
Convert R List to Numeric ValueTo 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 valueThe 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 <- 1:5 rv2 <- 6:10 data <- list(rv1, rv2) data Output[[1]] [1] 1 2 3 4 5 [[2]] [1] 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 OutputYou 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. as.numeric(as.character(unlist(data))) That is it for converting a list to a numeric value in R programming. See alsoR List to Array R List to data frame R List to Vector R List to String R List to Matrix 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.
seq.int() function in R Language is used to return a list of numbers in the specified range.
Example 1:
Output: [1] 0 1 2 3 4 5 [1] -2 -1 0 1 2 3 4 5 6 [1] -3 -2 -1 0 1 2 3 4 5 6 7 [1] -7 -6 -5 -4 -3 -2 -1 0Example 2:
Output: [1] 2 4 6 8 10 [1] 0 1 2 3 4 5 [1] 5 15 25 35 45Article Tags :
Examples of how to create a list of numbers in python using list comprehensions or built-in functions list(): Create a list of integersTo create a list of integers between 0 and N, a solution is to use the range(N) function: >>> l = [i for i in range(10)] >>> l [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> 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: >>> l = [i for i in range(25,35)] >>> l [25, 26, 27, 28, 29, 30, 31, 32, 33, 34] >>> l = list(range(25,35)) >>> l [25, 26, 27, 28, 29, 30, 31, 32, 33, 34]Create a list of floats using arangeTo create a list of floats between (N,M) with a given step, a solution is to use the numpy function called arange: >>> import numpy as np >>> l = [i for i in np.arange(2,8,0.5)] >>> l [2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5] >>> l = list(np.arange(2,8,0.5)) >>> l [2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5]Create a list of floats using linspaceTo 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 random integersThe 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 >>> l = random.sample(range(1,100), 10) >>> l [89, 56, 87, 51, 46, 25, 52, 44, 10, 32]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 ListFollowing 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 <- list("Red", "Green", c(21,32,11), TRUE, 51.23, 119.1) print(list_data)When we execute the above code, it produces the following result − [[1]] [1] "Red" [[2]] [1] "Green" [[3]] [1] 21 32 11 [[4]] [1] TRUE [[5]] [1] 51.23 [[6]] [1] 119.1Naming List ElementsThe list elements can be given names and they can be accessed using these names. # Create a list containing a vector, a matrix and a list. list_data <- list(c("Jan","Feb","Mar"), matrix(c(3,9,5,1,-2,8), nrow = 2), list("green",12.3)) # Give names to the elements in the list. names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list") # Show the list. print(list_data)When we execute the above code, it produces the following result − $`1st_Quarter` [1] "Jan" "Feb" "Mar" $A_Matrix [,1] [,2] [,3] [1,] 3 5 -2 [2,] 9 1 8 $A_Inner_list $A_Inner_list[[1]] [1] "green" $A_Inner_list[[2]] [1] 12.3Accessing List ElementsElements of the list can be accessed by the index of the element in the list. In case of named lists it can also be accessed using the names. We continue to use the list in the above example − # Create a list containing a vector, a matrix and a list. list_data <- list(c("Jan","Feb","Mar"), matrix(c(3,9,5,1,-2,8), nrow = 2), list("green",12.3)) # Give names to the elements in the list. names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list") # Access the first element of the list. print(list_data[1]) # Access the thrid element. As it is also a list, all its elements will be printed. print(list_data[3]) # Access the list element using the name of the element. print(list_data$A_Matrix)When we execute the above code, it produces the following result − $`1st_Quarter` [1] "Jan" "Feb" "Mar" $A_Inner_list $A_Inner_list[[1]] [1] "green" $A_Inner_list[[2]] [1] 12.3 [,1] [,2] [,3] [1,] 3 5 -2 [2,] 9 1 8Manipulating List ElementsWe can add, delete and update list elements as shown below. We can add and delete elements only at the end of a list. But we can update any element. # Create a list containing a vector, a matrix and a list. list_data <- list(c("Jan","Feb","Mar"), matrix(c(3,9,5,1,-2,8), nrow = 2), list("green",12.3)) # Give names to the elements in the list. names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list") # Add element at the end of the list. list_data[4] <- "New element" print(list_data[4]) # Remove the last element. list_data[4] <- NULL # Print the 4th Element. print(list_data[4]) # Update the 3rd Element. list_data[3] <- "updated element" print(list_data[3])When we execute the above code, it produces the following result − [[1]] [1] "New element" $Merging ListsYou can merge many lists into one list by placing all the lists inside one list() function. # Create two lists. list1 <- list(1,2,3) list2 <- list("Sun","Mon","Tue") # Merge the two lists. merged.list <- c(list1,list2) # Print the merged list. print(merged.list)When we execute the above code, it produces the following result − [[1]] [1] 1 [[2]] [1] 2 [[3]] [1] 3 [[4]] [1] "Sun" [[5]] [1] "Mon" [[6]] [1] "Tue"Converting List to VectorA list can be converted to a vector so that the elements of the vector can be used for further manipulation. All the arithmetic operations on vectors can be applied after the list is converted into vectors. To do this conversion, we use the unlist() function. It takes the list as input and produces a vector. # Create lists. list1 <- list(1:5) print(list1) list2 <-list(10:14) print(list2) # Convert the lists to vectors. v1 <- unlist(list1) v2 <- unlist(list2) print(v1) print(v2) # Now add the vectors result <- v1+v2 print(result)When we execute the above code, it produces the following result − [[1]] [1] 1 2 3 4 5 [[1]] [1] 10 11 12 13 14 [1] 1 2 3 4 5 [1] 10 11 12 13 14 [1] 11 13 15 17 19 |