R language learning from scratch (e) - "List (List)" data structures

This article first to know almost Column: https://zhuanlan.zhihu.com/p/60141740

Also simultaneously updated on my personal blog: https://www.cnblogs.com/nickwu/p/12567803.html


The list (list)

R language and Python list of lists in different languages. R is a list of language set of objects , called list Object and its components (Components) . Component may comprise vectors, matrices, arrays, data block or even another list, and requires each component in the list must have a name.

my_list <- list(stud_id=c(34453,123),
                 stud_name="John",
                 stud_marks=c(14.3,12,15,19))
my_list
#输出:
 $stud_id
 [1] 34453   123
 $stud_name
 [1] "John"
 $stud_marks
 [1] 14.3 12.0 15.0 19.0

5.1 index list

Use the list index form can be accessed, edited or deleted in the list of elements.

Access elements in the list can use the double square brackets to indicate the ingredient or ingredients with the name and location of access.

5.1.1 one component of the access list

list1 [1] # access list in the first component, the use of this method, the result is still returns a list (may also be used list1 [ 'stu_id'] a way to access the first component)

# mode(my_list[1])
# 返回 "list"

All elements of the value of a particular component 5.1.2 Access List

list1 [[1]] all the elements of value of the first component # access list, this returns a vector, not a list. (You can also access through list1 $ stu_id name for all elements "stu_id" ingredients.)

# mode(list[[1]])
# 返回"numeric"  

The value of a certain element of a component 5.1.3 Access List

List [. 1] The first element value of an access list # component

5.2 Edit List

5.2.1 Add or delete a list of ingredients

Principle and the index of the list is the same as an ingredient

my_list $ new_comp <- c (1,2,3,4 ) # If new_comp component does not exist, add the ingredients directly and assigned 
my_list [ 'new_comp'] <-list (c (1,2,3)) # if new_comp component does not exist, add the ingredients and direct assignment 
my_list $ stu_id = NULL # delete the name of the ingredient stu_id 
my_list [ 'stu_name'] = NULL # delete the name of the ingredient stu_name

5.2.2 modify the list of component elements of a value

Method with 5.2.1, if the component already exists, modify the value of the component elements, such as does not exist, create a new component

5.2.3 Edit list of a certain component element value

Principles and elements of a specific index values ​​of the same

my_list [[1]] [2] <- c (1,2,3) # modify the second component value of the first element in the list

5.3 List of common operations

  1. names( )Function lets you display and modify the list of names of all components

  2. length( )Function can display the number of list of ingredients

  3. unlist( )List function may unpack

#Example:
names(list1) <- c('a','b','c')
​
unlist(my.lst)
#输出:
stud_id1    stud_id2   stud_name stud_marks1 stud_marks2 stud_marks3 
     "34453"       "123"      "John"      "14.3"        "12"        "15" 
 stud_marks4 
        "19" 

Guess you like

Origin www.cnblogs.com/nickwu/p/12567803.html