选择列表的多个部分 [英] Selecting multiple parts of a list

查看:79
本文介绍了选择列表的多个部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含100个条目的数据框,我想获得条目子集的字段值。具体来说,我希望每隔10个条目(即指数1-10,21-30,41-50,61-70,......)

I have a data frame with 100 entries, and I want to get a fields value for a subset of the entries. Specifically, I want every other 10 entries (i.e. indices 1-10,21-30,41-50,61-70,...)

唯一的方法我已经能够做到这一点是通过:c(数据$字段[1:10],数据$字段[21:30],...)

The only way I've been able to do this is via: c(data$field[1:10],data$field[21:30],...)

但是这似乎是一个可怕的解决方案,特别是如果数据框的大小发生变化。

But this seems like a horrible solution, especially if the size of the data frame changes.

推荐答案

你可以做到

data$field[rep(c(TRUE, FALSE), each = 10)]

其中 rep 创建一个10的向量 TRUE 然后是10 FALSE ,并在用于索引时根据需要进行回收。

whererep creates a vector of ten TRUE followed by ten FALSE and is recycled as needed when used for indexing.

这篇关于选择列表的多个部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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