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

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编程中的列表相关推荐

  1. python列表内数字排序_如何在Python中手动排序数字列表?

    规格:Ubuntu 13.04,Python 3.3.1 背景:Python的初学者,遇到了这个"手动排序"的问题. 我被要求做的是:"让用户输入3个数值并将它们存储在3 ...

  2. c语言中判断输入是否为数字_C语言编程判断回文数

    大家好,我是阿汤哥,前两天在公众号后台收到一个朋友问回文数,今天我们就来谈谈怎么用C语言编程判断一个数是不是回文数. 怎么判断回文数? 首先看定义,什么是回文数. 回文数 定义: 设n是一任意自然数. ...

  3. Python中如何获得数组或者列表按大小排序后元素的索引列表

    使用 np.argsort 函数 import numpy as np a = np.array([10,40,30,50,25]) b = np.argsort(a)# 从小到大排序的元素的索引 p ...

  4. c语言删除数组重复元素并升序,C语言编程题: 编写函数,从一个已经排序的数组中删去某数后,该数组仍然有序....

    满意答案 nanhrui57q 2014.01.10 采纳率:57%    等级:12 已帮助:8634人 #include #include int compear(void *, void *); ...

  5. 编程中什么叫做元素什么叫帧_R编程中的数据帧

    编程中什么叫做元素什么叫帧 Let's continue in our R programming tutorial series, and understand data frames in R. ...

  6. lua 从一串数字中取出偶数位的数字_为什么JavaScript中 0.1 0.2 不等于0.3?

    在 js 中进行数学的运算时,会出现0.1+0.2=0.300000000000000004的结果,一开始认为是浮点数的二进制存储导致的精度问题,但这似乎不能很好的解释为什么在同样的存储方式下0.3+ ...

  7. Java中运用数组的四种排序方法_JAVA中运用数组的四种排序方法

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 快速排序 public class TestMain { public static void main(String[] args) { Integer ...

  8. 要求返回三位数中的水仙花数 ,数字范围在100-999中。(水仙花数:百位数的3次方+十位数的3次方+个位数的三次方=数字本身)

    参考答案: for num1 in range(100,1000):     num2=str(num1)     if num1 == int(num2[0])**3+int(num2[1])**3 ...

  9. python列表内数字怎么求和_python怎么把列表的值相加

    2016-12-14 回答 第一步: 我分析如果从一堆数里随机抽取几个,并且按原来的顺序输出的话,那么至少要随机两次才能完成,第一次随机的值是确定随机几个数,第二次随机是确定随机的值. 第二步: 随机 ...

最新文章

  1. ubuntu自定义命令
  2. Linux下matlab断点调试
  3. Mysql迁移到Postgresql
  4. map语法获取index_MySQL SQL语法优化——使用Explain查看执行计划
  5. idea 切换java11_Java 11就在这里,您准备好进行切换了吗?
  6. flex socket java_使用Java编写Socket服务器,并且与Flex通信(二)
  7. 使用Adobe Acrobat为PDF文件添加签名(图片+签名)
  8. ie下的firebug
  9. 附上堆和栈的区别 (转贴)
  10. 计算机标准差平方差怎么按,数学标准差公式
  11. word按标题自动生成序号
  12. 鼠标键盘的使用:用左ALT+左SHIFT+NUM LOCK即可启动或关闭键盘的鼠标键
  13. 康托尔集合论-罗素悖论-公理化集合论-不完全性定理
  14. 【VUE】在vue中使用google地图
  15. 【Linux】C++后台开发面试
  16. 更易用的OceanBase|生态工具征文大赛正式开启!
  17. 【Python】:数据可视化之相关系数热力图绘制(二)(seaborn版本)
  18. 推荐几个好用的网盘搜索引擎
  19. PhotoSwipe.js 相册展示插件学习
  20. Android如何支持多种屏幕

热门文章

  1. [DB]MariaDB 与 MySql 数据库
  2. Eclipse中文乱码解决汇总(应该比较全):
  3. 全心加入web前端开发,向上吧!
  4. Paypal Rest Api自定义物流地址(跳过填写物流地址)
  5. 现实JS模板,可设置默认值
  6. ajax图片上传,基于firefox
  7. Nautilus获得了标签化支持
  8. ASP.NET中常用的优化性能的方法(转贴,Icyer收集整理)
  9. [转载] Python 内置函数 dir()
  10. [转载] python数字类型(一)