Selecting multiple parts of a list

喜欢而已 提交于 2019-12-17 21:13:32

问题


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,...)

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.


回答1:


You can do

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

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



来源:https://stackoverflow.com/questions/14794602/selecting-multiple-parts-of-a-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!