[英]R dplyr resolve variable in conditional filter
I am trying to filter based on a variable value, and have tried multiple combinations of filter_, dots and quotes to no avail. 我正在尝试基于变量值进行过滤,并且尝试了filter_,点和引号的多种组合无济于事。
As an example, I have a 例如,我有一个
runlist = c(1, 2, 3, 4, 5)
and a dataframe boo 和一个数据框boo
run <- rep(seq(5), 3)
edge1 <- sample(20, 15)
edge2 <- sample(20, 15)
weights <- sample(50, 15)
boo <- as.data.frame(cbind(run, edge1, edge2, weights))
and I want to filter a dataframe named boo which may look something like iteratively as 我想过滤一个名为boo的数据框,它看起来像迭代
for (i in runlist) {
bop <- boo %>% filter( run == i )
str(boo)
}
I suspect I'll be hearing about not using for loops and R, rather use group_by(run)
, but I'm sending this data to igraph
and need to further subset the dataset to just edges and weights, thus losing the grouping variable, as in 我怀疑我会听说不使用for循环和R,而是使用
group_by(run)
,但是我将数据发送到igraph
并且需要将数据集进一步igraph
为边缘和权重,从而丢失了分组变量,如
bop <- boo %>% filter( run == i ) %>% select( edge1, edge2, weights )
I will create a network graph and find density and centrality values for each run. 我将创建一个网络图,并找到每次运行的密度和中心度值。
bing <- graph.data.frame(bop)
How do I get the i
in the conditional filter to resolve as the correct index? 我如何在条件过滤器中将
i
解析为正确的索引?
My answer is not about "resolving a variable in a conditional filter", but there's a much easier way to do what you want to do. 我的答案不是关于“在条件过滤器中解析变量”,而是有一种更轻松的方法来执行您想做的事情。
The big idea is to split the data frame based on the variable run
, and map a function onto each of those pieces. 最重要的想法是根据变量
run
拆分数据帧,然后将函数映射到每个片段上。 This function takes a piece of the data frame and spits out an igraph. 该函数获取一个数据帧并吐出一个igraph。
The following code accomplishes the above, storing a list of graphs in the column graph
. 下面的代码实现上述,存储在列图列表
graph
。 (It's a list-column, see more at the R for data science book ) (这是一个列表列,有关详细信息,请参见R for data science book )
boo %>%
group_by(run) %>%
nest() %>%
mutate(graph = map(data, function(x) graph.data.frame(x %>% select(edge1, edge2, weights)))) %>%
mutate(density = map(graph, function(x) graph.density(x))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.