本文介绍了访问R中Filter(is.atomic,eq)所示的原子向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 Filter(is.atomic,something) 原子矢量。 1。 Weather -example 此处 > Filter(is.atomic,study) $ region [1]HamburgBremen 2。镶嵌图 - 树状图 - 示例此处 > Map(function(x)Filter(is.atomic,x),ls()) $ g [1]g $ lookup [ 1]lookup $ req.data [1]req.data $ tmp [1]tmp $ tmp1 [1]tmp1 职位可以是任意的,我可能对他们的数据结构有微弱的线索,所以不能使用 var $ some $ ... $ vector 。我觉得需要?职位。使用你的想象力,这些例子并不是独一无二的。我怎样才能访问他们的原子矢量? 解决方案为了压扁列表以便访问原子向量,可以使用以下函数:$ b $ (x){y while(is.list(x))b flatten.list< ){ id< - sapply(x,is.atomic)y< -c(y,x [id])x< - unlist(x [!id] = FALSE)} y } 的元素。用法,使用Vincent的答案列表x: $ p $ x list(1:3, (列表(9:11,12:15),16:20),21:24) 7:8,列表 然后: > flatten.list(x) [[1]] [1] 7 8 [[2]] [1] 1 2 3 [[3]] [1] 4 5 6 [[4]] [1] 21 22 23 24 ... 要递归地对列表中的所有原子元素执行操作,请使用 rapply()(这是文森特基本上编写的内容)。 rapply(x,sum) [1] 6 15 15 30 54 90 90 > rapply(x,sum,how ='list') [[1]] [[1]] [[1]] [1] 6 [[1]] [[2]] [1] 15 [[2]] [1] 15 ... 另见?rapply PS:您的代码 Map(函数(x)过滤器(is.atomic,x),ls())没有意义。 ls()返回一个字符向量,因此该字符向量的每个元素都将作为列表的一部分返回。这不会告诉你什么。 接下来, Filter()不会做什么你相信它的确如此。以文森特的答案为例, x 很容易。 Filter()仅返回第二个元素。这是唯一的原子元素。 过滤器(is.atomic,x)等于100%: ind< - sapply(x,is.atomic)x [ind] Filter(is.atomic, something)returns atomic vectors. 1. Weather -example here> Filter(is.atomic, study)$region[1] "Hamburg" "Bremen" 2. mosaic-plot-as-tree-plot -example here> Map(function(x) Filter(is.atomic, x), ls())$g[1] "g"$lookup[1] "lookup"$req.data[1] "req.data"$tmp[1] "tmp"$tmp1[1] "tmp1"Look their positions can be arbitrary, I may have faintest clue of their data-structure so cannot use var$some$...$vector. I feel the need of ?Position. Use your imagination, the examples are not exclusive. How can I access their atomic vectors? 解决方案 To flatten a list so you can access the atomic vectors, you can use following function:flatten.list <- function(x){ y <- list() while(is.list(x)){ id <- sapply(x,is.atomic) y <- c(y,x[id]) x <- unlist(x[!id],recursive=FALSE) } y}This function maintains names of the elements. Usage, using the list x from Vincent's answer :x <- list( list(1:3, 4:6), 7:8, list( list( list(9:11, 12:15), 16:20 ), 21:24 ))then: > flatten.list(x)[[1]][1] 7 8[[2]][1] 1 2 3[[3]][1] 4 5 6[[4]][1] 21 22 23 24...To recursively do an action on all atomic elements in a list, use rapply() (which is what Vincent handcoded basically).> rapply(x,sum)[1] 6 15 15 30 54 90 90> rapply(x,sum,how='list')[[1]][[1]][[1]][1] 6[[1]][[2]][1] 15[[2]][1] 15...See also ?rapplyPS : Your code Map(function(x) Filter(is.atomic, x), ls()) doesn't make sense. ls() returns a character vector, so every element of that character vector will be returned as part of the list. This doesn't tell you anything at all.Next to that, Filter() doesn't do what you believe it does. Taking the example list x, from the answer of Vincent, accessing only the atomic parts of it is pretty easy. Filter() only returns the second element. That's the only atomic element. Filter(is.atomic, x) is 100% equivalent to:ind <- sapply(x, is.atomic)x[ind] 这篇关于访问R中Filter(is.atomic,eq)所示的原子向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-29 10:03