如何在R中索引列表对象的元素 [英] How to index an element of a list object in R

查看:17
本文介绍了如何在R中索引列表对象的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行以下操作以导入一些 txt 表并将它们保留为列表:

# set working directory - 存放所有选择表的文件夹hypo_selections<-list.files() # 根据每个物种改变对象名称hypo_list<-lapply(hypo_selections,read.table,sep="	",header=T) # 根据每个物种改变对象名称

我想访问一个特定的元素,比如 hypo_list[1].由于每个元素代表一个表格,我应该如何访问特定的单元格(行和列)?

我想做类似的事情:

a<-hypo_list[1][1,2]

但我收到以下错误消息:

a[1, 2] 中的错误:维数不正确

有什么巧妙的方法吗?

提前致谢!

解决方案

索引列表是使用双括号完成的,即 hypo_list[[1]] (例如,看看这里:http://www.r-tutor.com/r-introduction/list).顺便说一句:read.table 不返回表而是返回数据帧(请参阅 ?read.table 中的值部分).因此,您将拥有一个数据框列表,而不是一个表对象列表.不过,表和数据框的主要机制是相同的.

注意:在 R 中,第一个条目的索引是 1(不像其他语言中的 0).

数据框

l <- list(anscombe, iris) # 将dfs放入列表l[[1]] # 返回 anscombe 数据帧anscombe[1:2, 2] # 访问数据集的前两行第二列[1] 10 8l[[1]][1:2, 2] # 相同但首先从列表中选择数据框[1] 10 8

表格对象

tbl1 <- table(sample(1:5, 50, rep=T))tbl2 <-表(样本(1:5,50,代表= T))l <- list(tbl1, tbl2) # 将表放入列表tbl1[1:2] # 访问表 1 的前两个元素

现在有了清单

l[[1]] # 访问列表中的第一个表1 2 3 4 59 11 12 9 9l[[1]][1:2] # 访问第一个表中的前两个元素1 29 11

I'm doing the following in order to import some txt tables and keep them as list:

# set working directory - the folder where all selection tables are stored
hypo_selections<-list.files() # change object name according to each species
hypo_list<-lapply(hypo_selections,read.table,sep="	",header=T) # change object name according to each species

I want to access one specific element, let's say hypo_list[1]. Since each element represents a table, how should I procced to access particular cells (rows and columns)?

I would like to do something like it:

a<-hypo_list[1]

a[1,2]

But I get the following error message:

Error in a[1, 2] : incorrect number of dimensions

Is there a clever way to do it?

Thanks in advance!

解决方案

Indexing a list is done using double bracket, i.e. hypo_list[[1]] (e.g. have a look here: http://www.r-tutor.com/r-introduction/list). BTW: read.table does not return a table but a dataframe (see value section in ?read.table). So you will have a list of dataframes, rather than a list of table objects. The principal mechanism is identical for tables and dataframes though.

Note: In R, the index for the first entry is a 1 (not 0 like in some other languages).

Dataframes

l <- list(anscombe, iris)   # put dfs in list
l[[1]]             # returns anscombe dataframe

anscombe[1:2, 2]   # access first two rows and second column of dataset
[1] 10  8

l[[1]][1:2, 2]     # the same but selecting the dataframe from the list first
[1] 10  8

Table objects

tbl1 <- table(sample(1:5, 50, rep=T))
tbl2 <- table(sample(1:5, 50, rep=T))
l <- list(tbl1, tbl2)  # put tables in a list

tbl1[1:2]              # access first two elements of table 1 

Now with the list

l[[1]]                 # access first table from the list

1  2  3  4  5 
9 11 12  9  9 

l[[1]][1:2]            # access first two elements in first table

1  2 
9 11 

这篇关于如何在R中索引列表对象的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆