如何提取列表中的每个奇数元素 [英] How to extract every odd numbered element in list

查看:27
本文介绍了如何提取列表中的每个奇数元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提取列表中每个奇数(或偶数)元素.

I want to extract every odd (or even) numbered element in list.

#example data
ls <- list(c(1,2,3,4,5,6,7), c(8,9,10,11,12,13,14,15), c(16,17,18,19,20,21))

ls
[[1]]
[1] 1 2 3 4 5 6 7

[[2]]
[1]  8  9 10 11 12 13 14 15

[[3]]
[1] 16 17 18 19 20 21

在此示例中,我希望能够仅提取列表中的第一个和第三个元素.我怎样才能做到这一点?

In this example I want to be able to extract only the first and third element in the list. How can I do this?

谢谢

推荐答案

这应该可以解决您的问题:

this should solve your problem:

my_list[seq(1, length(my_list), 2)]

我建议您不要将内置的R函数用作对象的名称( ls ).还查找一些基本的R列表操作,索引等

i would advise you against using inbuilt R functions as names for your objects (ls). also look up some elementary R list manipulations, indexing, etc

这篇关于如何提取列表中的每个奇数元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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