有没有办法将R中的工作环境或工作空间设置为已定义的环境?我想在不经常引用环境的环境中调用变量.我知道attach函数可以用这种方式访问变量,但是创建的任何变量都不会放回到附加的环境中.目标是让所有功能都在其他环境中进行.
例如:
original.env <- .GlobalEnv
other.env <- new.env()
other.env$A <- 12; other.env$B <- 1.5
set.env(other.env)
C <- A + B
set.env(original.env)
other.env$C
[1] 13.5
Run Code Online (Sandbox Code Playgroud)
这是set.env我无法弄清楚它是否存在的步骤,或者还有其他一些技巧可以做到这一点.
这种方法的目标是允许相同的代码在几个非嵌套环境中的相同结构的不同数据集上使用,而不必经常调用具有Environment$前缀的其他环境,这在许多情况下变得非常冗长.
如果结果也可以分配回设置环境(如在全局环境中,所有内容都隐含.GlobalEnv$在任何变量前面),那么它将更容易访问并从函数内部返回多个值.
任何帮助表示赞赏.谢谢.
在 R 中,可以通过以下方式获取组的乘积:
iris %>% group_by(Species) %>% summarize(new_col = prod(Petal.Length))
Run Code Online (Sandbox Code Playgroud)
如何在 postgresql 或 dbplyr/dplyr 中实现相同的概念?
我正在使用字符串R.我有下一个字符串:
x<-c("Alfa Cactus 500 Bachelor degree Star")
Run Code Online (Sandbox Code Playgroud)
例如,在第一个单词之后的这个字符串中我有两个空格.在第二个单词之后我有三个空格.当有两个或更多空格时,我试图仅替换空格.在这种情况下,我想添加*而不是白色空格.我正在寻找一个像这样的新字符串:
Alfa*Cactus*500*Bachelor degree*Star
Run Code Online (Sandbox Code Playgroud)
在Bachelor degree我只有一个空格的情况下.然后,不能进行替换.当我尝试这个:
gsub(" +","*",x)
Run Code Online (Sandbox Code Playgroud)
我懂了:
Alfa*Cactus*500*Bachelor*degree*Star
Run Code Online (Sandbox Code Playgroud)
但是,不应该尊重只有一个空白区域不能被替换的事实.我不知道如何适应gsub这种变化.
我希望你能帮助我.非常感谢.
我在Linux Mint(平台i686-pc-linux-gnu)上安装了R 3.4.1,但以前的R版本以及Windows都发生了同样的问题。当我尝试安装igraph使用install.packages(),这个错误发生的情况:
foreign-graphml.c: In function ‘igraph_write_graph_graphml’:
foreign-graphml.c:1408:46: error: expected ‘)’ before ‘GRAPHML_NAMESPACE_URI’
ret=fprintf(outstream, "<graphml xmlns=\"" GRAPHML_NAMESPACE_URI "\"\n");
^
foreign-graphml.c:1412:59: error: expected ‘)’ before ‘GRAPHML_NAMESPACE_URI’
ret=fprintf(outstream, " xsi:schemaLocation=\"" GRAPHML_NAMESPACE_URI "\n");
^
foreign-graphml.c:1414:38: error: expected ‘)’ before ‘GRAPHML_NAMESPACE_URI’
ret=fprintf(outstream, " " GRAPHML_NAMESPACE_URI "/1.0/graphml.xsd\">\n");
^
/usr/lib/R/etc/Makeconf:159: recipe for target 'foreign-graphml.o' failed
make: *** [foreign-graphml.o] Error 1
ERROR: compilation failed for package ‘igraph’
* removing ‘/home/ninja/R/i686-pc-linux-gnu-library/3.4/igraph’
Warning in install.packages :
installation of package ‘igraph’ had non-zero exit status
Run Code Online (Sandbox Code Playgroud)
我也尝试使用类似的方法从github安装: …