获取列表行中的第一个条目? [英] get first entries in rows of list?

查看:132
本文介绍了获取列表行中的第一个条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有3个列"的列表:

I have a list, with 3 "columns":

test = list(1:100, 1:100,1:100)

我如何访问例如每列的前10个条目?

How can I access the e.g. first 10 entries of each column?

test[c(1:10),]

不起作用.我知道它适用于data.frames:

doesn't work. I know it works for data.frames:

as.data.frame(test)[1:10,1:3]

如何使用列表解决此问题?

How do I solve this with lists?

要获得更一般的答案:

如何获得第1列和第3列中的条目15到20?这是我为data.frames做的事情:

How do I get the entries 15 to 20 in column 1 and 3? Here is what I do for data.frames:

as.data.frame(test)[c(15:20),c(1,3)]

似乎索引在data.frames和list之间有很大的不同.

It seems like the indexing differs a lot between data.frames and lists.

推荐答案

您可以使用函数lapply()head()来做到这一点.

You can do it with functions lapply() and head().

lapply(test,head,n=10)
[[1]]
 [1]  1  2  3  4  5  6  7  8  9 10

[[2]]
 [1]  1  2  3  4  5  6  7  8  9 10

[[3]]
 [1]  1  2  3  4  5  6  7  8  9 10

如果列表元素中的值少于n=,则显示所有值.

If there will be less than n= values in list element then this show all values.

test = list(1:100, 1:100,1:5)
lapply(test,head,n=10)
[[1]]
 [1]  1  2  3  4  5  6  7  8  9 10

[[2]]
 [1]  1  2  3  4  5  6  7  8  9 10

[[3]]
[1] 1 2 3 4 5

这篇关于获取列表行中的第一个条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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