30

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="\t",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?

starball
  • 20,030
  • 7
  • 43
  • 238
Mohr
  • 323
  • 1
  • 3
  • 9

1 Answers1

43

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 
Mark Heckmann
  • 10,943
  • 4
  • 56
  • 88
  • 14
    It might not be obvious for a developer that indexing starts from '1' and not '0' – KarlP Mar 25 '16 at 09:25
  • 4
    @KarlP On the contrary, it is not nature to count something from 0 :) – Verbal Dec 05 '16 at 10:31
  • 4
    Fair enough :-D Not obvious for developers using programming languages with a heritage from "system" languages (assembly, C, etc, up to Java, Swift), and also lisp-languages, BASIC, and just about every "generic" programming language in existence such as perl, php, etc etc. Specialist tools, such as Matlab, and other original non-techy languages made for a purpose such as COBOL (!) but also FORTRAN, statistical languages, etc, typically index from 1. As does "design by committee" languages from IBM, etc. – KarlP Jan 04 '17 at 12:18
  • 2
    @Karlp had said "for a developer" - so indexing from 1 is like driving your car from the back seat. – WestCoastProjects Feb 03 '18 at 22:31
  • 1
    @KarlP Thank you Karl for telling me that indexes start at 1, I would have never figured this out otherwise... who would do such a terrible thing. – Adam Mar 17 '22 at 11:06