我想计算"和"除第一个id列之外的每个列之间的组合。我通过两个for循环实现了它。但是,如果对于大的列和行大小,以下方法变得非常慢。这样的
是否有效library(dplyr)
Input <- data_frame(id=1:4, c1=c(T,T,F,F), c2=c(T,F,F,F),c3=c(F,T,F,F))
id c1 c2 c3
1 1 TRUE TRUE FALSE
2 2 TRUE FALSE TRUE
3 3 FALSE FALSE FALSE
4 4 FALSE FALSE FALSE
期望的输出是:
> Output
id c1_c2 c1_c3 c2_c3
1 1 TRUE FALSE FALSE
2 2 FALSE TRUE FALSE
3 3 FALSE FALSE FALSE
4 4 FALSE FALSE FALSE
for-loop方法:
Output <- data_frame(id=Input$id)
colSize <- ncol(Input)
colnms <- colnames(Input)
for(i in 2:(colSize-1)){
for (j in (i+1):colSize){
name_i <- paste(colnms[i],colnms[j],sep="_")
logic_and <- (Input[colnms[i]]&Input[colnms[j]])
Output$name <- logic_and
names(Output)[ncol(Output)] <- name_i
}
}
答案 0 :(得分:2)
一个选项是来自bool resolve_links_rec(pugi::xml_node node)
{
if (node.type() == pugi::node_pi && strcmp(node.name(), "link") == 0)
{
try
{
pugi::xml_node parent = node.parent();
pugi::xpath_node_set ns = parent.select_nodes(node.value());
for (size_t i = 0; i < ns.size(); ++i)
parent.insert_copy_before(ns[i].node(), node);
return true;
}
catch (pugi::xpath_exception& e)
{
std::cerr << "Error processing link " << node.path() << ": " << e.what() << std::endl;
}
}
else
{
for (pugi::xml_node child = node.first_child(); child; )
{
pugi::xml_node next = child.next_sibling();
if (resolve_links_rec(child))
node.remove_child(child);
child = next;
}
}
return false;
}
combn
base R