简体   繁体   中英

Use lists/dataframes as items in for-loops in R

I am quite sure this is basic stuff, but I just can't find the answer by googling. So my problem:

I want to use a for-loop on a list of lists or data frames. But when you use list[i], you get all the values in the data frame instead of the data frame it self. Can anyone point out to me how to code this properly?

Example of the code:

a<-data.frame(seq(1:3),seq(3:1))
b<-data.frame(seq(1:3),seq(3:1))
l<-c(a,b)

Then l[1] returns:

> l[1]
$seq.1.3..
[1] 1 2 3

And I want it to just return: a

You can use the list function:

a<-data.frame(1:3,1:3)
b<-data.frame(3:1,3:1)

l<-list(a,b)

And access it's value with double brackets [[ :

l[[1]]
l[[2]]

Ps: seq(1:3) and seq(3:1) outputs the same value, so I used 1:3 and 3:1 . :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM