组合n列表R的非NA值

编程入门 行业动态 更新时间:2024-10-14 18:16:38
本文介绍了组合n列表R的非NA值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 goodgg [[1]] IGRAPH UN-- 3 3 -- + attr: name (v/c), color (v/c), value (e/n), sourceID (e/n), targetID (e/n) + edges (vertex names): [1] 89315--89316 89315--89928 89316--89928 [[2]] IGRAPH UN-- 3 2 -- + attr: name (v/c), color (v/c), value (e/n), sourceID (e/n), targetID (e/n) + edges (vertex names): [1] 106277--106278 106278--106279

我可以使用 [union] [1] 将这些组合成一个对象:

I can combine these into a single object using [union][1]:

combine = graph.union(goodgg[[1]], goodgg[[2]], byname=T) combine IGRAPH UN-- 6 5 -- + attr: color_1 (v/c), color_2 (v/c), name (v/c) + edges (vertex names):

从此,我可以提取特定属性,例如一个颜色,它与原始对象的顺序排列(1 - 2):

From this, I can extract particular attributes e.g. a color, which lines up with the order of the original objects (1 - 2):

as.list(get.vertex.attribute(combine)) $color_1 [1] "red" "red" "orange" NA NA NA $color_2 [1] NA NA NA "red" "red" "red" $name [1] "89315" "89316" "89928" "106277" "106278" "106279"

如何提取非 NA $ color_1 和 $ color_2 中的值,并在我拥有时将它们合并到一个新列表中任意数量的 color_n 条目? (例如,我有n个条目)?

要获得:

How can I extract the non NA values in $color_1 and $color_2 and merge them into a new list when I have an arbitrary number of color_n entries? (E.g. I have n entries)?

To get:

[1] "red" "red" "orange" "red" "red" "red"

我尝试了什么(这不适用于 n 颜色_ 变量:

在这个简单的例子中我可以做这个答案在这里做的事情:

What I tried (which does not work for n color_ variables:

In this simple case I can do what this answer did here:

V(combine)$color <- ifelse(is.na(get.vertex.attribute(combine)$color_1), get.vertex.attribute(combine)$color_2,get.vertex.attribute(combine)$color_1) get.vertex.attribute(combine)$color [1] "red" "red" "orange" "red" "red" "red"

然而,实际上我的列表可以有 n 元素。我如何调整这个来计算 n 元素?

However, in reality my list could have n elements. How can I adjust this to account for n elements?

我考虑使用多个嵌套的IFELSE语句,例如此处和这里 a la:

I considered using multiple nested IFELSE statements such as here and here a la:

V(combine)$color <- ifelse(is.na(get.vertex.attribute(combine)$color_1), ifelse(is.na(get.vertex.attribute(combine)$color_2), ifelse(get.vertex.attribute(combine)$color_3)......))

这不适用于未知的 n 属性并没有解决使用未知数量 n 的属性的问题。

This does not work for unknown n attributes and does not solve the issue of having an unknown number n of attributes to work with.

非常感谢您的帮助。

推荐答案

您可以使用减少累积在向量上应用函数:

You can use Reduce to "cumulatively" apply a function over a vector:

set.seed(125) color_choices <- c("red", "orange", NA) color_samples <- replicate( 4, sample(color_choices, 5, replace = TRUE), simplify = FALSE ) color_samples # [[1]] # [1] NA "red" "red" "orange" NA # # [[2]] # [1] NA "orange" "red" "orange" "orange" # # [[3]] # [1] "red" NA "orange" "red" "orange" # # [[4]] # [1] "orange" "orange" NA NA NA Reduce( f = function(a, b) ifelse(is.na(a), b, a), x = color_samples ) # [1] [1] "red" "red" "red" "orange" "orange"

在这种情况下,减少将函数应用于第一个和第二个元素,然后应用于该结果和第三个元素,然后应用于该结果和第四个元素。如果列表更长,它将继续这样。

In this case, Reduce applied the function to the first and second elements, then to that result and the third element, then to that result and the fourth element. If the list were longer, it would've kept going on that way.

根据您的具体情况编辑:保存属性列表,找到名称如 color_n ,然后使用 Reduce 解决方案。

Edit for your specific situation: save the list of attributes, find which have names like color_n, and then use the Reduce solution on those.

combine_attributes <- as.list(get.vertex.attribute(combine))

因为我没有您的数据,我们只需说 combine_attributes 看起来像上面创建的 color_samples 使用额外元素:

Because I don't have your data, let's just say combine_attributes looks like the color_samples created above with an extra element:

combine_attributes # $color_1 # [1] NA "red" "red" "orange" NA # # $color_2 # [1] NA "orange" "red" "orange" "orange" # # $color_3 # [1] "red" NA "orange" "red" "orange" # # $color_4 # [1] "orange" "orange" NA NA NA # # $name # [1] "89315" "89316" "89928" "106277" "106278" color_attributes <- grep( "^color_\\d+$", names(combine_attributes), value = TRUE ) color_attributes # [1] "color_1" "color_2" "color_3" "color_4" Reduce( f = function(a, b) ifelse(is.na(a), b, a), x = combine_attributes[color_attributes] ) # [1] "red" "red" "red" "orange" "orange"

更多推荐

组合n列表R的非NA值

本文发布于:2023-11-30 13:57:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650240.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   列表   NA

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!