编程中怎样将列表中数字排序_R编程中的列表

编程中怎样将列表中数字排序

Lists in R can group together different kinds of variables into a single compound structure. For this reason, lists prove very useful in handling data. A list can contain a numeric, integer string, factor or even another list as its component.

R中的列表可以将不同种类的变量组合到一个复合结构中。 因此,列表在处理数据方面非常有用。 一个列表可以包含数字,整数字符串,因子甚至其他列表作为其组成部分。

在R中创建列表 (Creating Lists in R)

Lists in R can be thought of as similar to vectors in R. However, vectors are more or less atomic since their components cannot be broken down further.

可以认为R中的列表类似于R中的向量 。 但是,矢量或多或少是原子的,因为它们的成分无法进一步分解。

Lists are often known as recursive vectors, as they are comprised of complex structures like vectors and other lists that may have more components.

列表通常称为递归向量 ,因为它们由复杂的结构组成,例如向量和可能具有更多成分的其他列表。

Let us create a list first.

让我们首先创建一个列表。


alist <-list(name="Ram",age=25,salary=20000)

Each component of the list can be accessed by its name, preceded by a dollar sign.

列表的每个组成部分都可以通过其名称(以美元符号开头)进行访问。


> alist$name
[1] "Ram"
> alist$age
[1] 25
> alist$salary
[1] 20000

R中的索引列表 (Indexing Lists in R)

However, it is not always necessary to name the components of a list. We can also refer to the elements using basic indexing. Let us create the same list without the component names.

但是,不一定总是需要命名列表的组成部分。 我们还可以使用基本索引来引用元素。 让我们创建没有组件名称的相同列表。


> alist <-list("Ram",25,20000)

Now we can access each of these elements within the list. Notice how the indexing starts from 1 compared to the other programming languages where the first element is usually 0.

现在,我们可以访问列表中的每个元素。 请注意,与第一个元素通常为0的其他编程语言相比,索引是如何从1开始的。


> alist[[1]]
[1] "Ram"
> alist[[2]]
[1] 25
> alist[[3]]
[1] 20000

Indexing using names of the components within the square braces is also allowed.

也允许使用方括号内的组件名称进行索引。


> alist <-list(name="Ram",age=25,salary=20000)
> alist[['name']]
[1] "Ram"

Let us look at more complex lists and how we index them. Consider we are building a game where a character gets a choice at each position. All the information about the gameplay is encapsulated in a list as follows.

让我们看一下更复杂的列表以及如何对其进行索引。 考虑我们正在构建一个游戏,角色在每个位置都有选择权。 有关游戏玩法的所有信息都封装在一个列表中,如下所示。


> mylist <-list(choices=c("Right","Left","Jump","Duck"),pos=matrix(c(22.4,32.5,20,28),ncol=2,nrow=2),charname="Mario")

The list here contains a vector for the choices available, the matrix for the character’s position on screen and the name of the character as a string. Let us see how we index these.

此处的列表包含可供选择的向量,字符在屏幕上的位置的矩阵以及字符名称(字符串)。 让我们看看如何索引这些。


#Access the elements of the vector within the list
> mylist$choices[2]
[1] "Left"

#Get the matrix from the list
> mylist$pos
     [,1] [,2]
[1,] 22.4   20
[2,] 32.5   28

#Access elements of matrix using the indexes
> mylist$pos[1,2]
[1] 20

分配名称 (Assigning Names)

You can also reassign the names for the components of a list after you created it as follows. This method works whether you have already assigned the names or not.

创建列表后,还可以按以下方式重新分配列表组件的名称。 无论您是否已分配名称,此方法都有效。


> names(mylist)=c("Actions","Positions","Name")
> mylist
$Actions
[1] "Right" "Left"  "Jump"  "Duck" 

$Positions
     [,1] [,2]
[1,] 22.4   20
[2,] 32.5   28

$Name
[1] "Mario"

修改清单 (Modifying lists)

You can always add new components to the list using the dollar sign as follows. We’ll demonstrate the addition of a new component named “lives” with an integer value of 3.

您始终可以使用美元符号将新组件添加到列表中,如下所示。 我们将演示添加一个名为“ lives”的新组件,该组件的整数值为3。


> mylist$lives <-3
> mylist
$Actions
[1] "Right" "Left"  "Jump"  "Duck" 

$Positions
     [,1] [,2]
[1,] 22.4   20
[2,] 32.5   28

$Name
[1] "Mario"

$lives
[1] 3

嵌套列表 (Nesting Lists)

A list can be added as a component to another list. This can be done to any depth level. The indexing in such cases follows the pattern of nesting. Let us look at an example.

可以将一个列表作为组件添加到另一个列表中。 可以在任何深度级别上完成。 在这种情况下,索引遵循嵌套模式。 让我们来看一个例子。


#Create a list to represent the player 
> player <- list(name="Adam",age=16,location= "USA")

#Add this as a component to the list we created earlier.
> mylist$player <- player

#Display the list.
> mylist
$Actions
[1] "Right" "Left"  "Jump"  "Duck" 

$Positions
     [,1] [,2]
[1,] 22.4   20
[2,] 32.5   28

$Name
[1] "Mario"

$lives
[1] 3

$player
$player$name
[1] "Adam"

$player$age
[1] 16

$player$location
[1] "USA"

When we need to refer to the location of the player within the mylist, we do it using mylist$player$location.

当我们需要引用玩家在mylist的位置时,可以使用mylist$player$location


> mylist$player$location
[1] "USA"

It is important to bear the nesting order in mind when trying to access the list elements in the above manner. Lists are usually employed to return function outputs. However, these can consume a lot of memory compared to vectors and matrices due to their variability. Therefore, it is necessary to tackle lists with care while programming.

在尝试以上述方式访问列表元素时,请记住嵌套顺序,这一点很重要。 通常使用列表来返回函数输出。 但是,由于矢量和矩阵的可变性,它们可能会消耗大量内存。 因此,在编程时必须小心处理列表。

翻译自: https://www.journaldev.com/35806/lists-in-r

编程中怎样将列表中数字排序

你可能感兴趣的:(编程中怎样将列表中数字排序_R编程中的列表)