这让我非常糟糕.你可以缩写列表名称吗?我之前从未注意到它,而且我完全搞砸了一天.有人可以解释这里发生了什么,为什么它可能比它可怕更有用?为什么它与底部的不一致?如果我可以关掉它?
> wtf <- list(whatisthe=1, pointofthis=2)
> wtf$whatisthe
[1] 1
> wtf$what
[1] 1
> wtf <- list(whatisthe=1, whatisthepointofthis=2)
> wtf$whatisthepointofthis
[1] 2
> wtf$whatisthep
[1] 2
> wtf$whatisthe
[1] 1
> wtf$what
NULL
Run Code Online (Sandbox Code Playgroud) 我有一个操作,我想为数据帧的每一行运行,更改一列.我是一个apply/ddply/sqldf人,但是当它们有意义时我会使用循环,我认为这是其中之一.这种情况很棘手,因为要更改的列取决于按行更改的信息; 根据一个单元格中的信息,我应该只更改该行中的十个其他单元格中的一个.对于75列和20000行,操作需要10分钟,当我的脚本中的每个其他操作需要0-5秒,最多10秒.我已经将问题解决了下面非常简单的测试用例.
n <- 20000
t.df <- data.frame(matrix(1:5000, ncol=10, nrow=n) )
system.time(
for (i in 1:nrow(t.df)) {
t.df[i,(t.df[i,1]%%10 + 1)] <- 99
}
)
Run Code Online (Sandbox Code Playgroud)
这需要70秒,十列,当ncol = 50时需要360.太疯狂了.循环是错误的方法吗?有没有更好,更有效的方法来做到这一点?
我已经尝试将嵌套术语(t.df [i,1] %% 10 + 1)初始化为for循环外的列表.它节省了大约30秒(10分钟内),但使上面的示例代码更加复杂.所以它有所帮助,但它不是解决方案.
在准备这个测试用例时,我目前最好的想法来了.对我来说,只有10列是相关的(75-11列是无关紧要的).由于运行时间在很大程度上取决于列数,因此我可以在排除不相关列的数据框上运行上述操作.那会让我失望一分钟.但是"使用嵌套索引进行循环"甚至是考虑我的问题的最佳方式吗?
我试图在sql中禁用(auto-/omni-/whichever-)完成.这对我来说是一个问题,因为我用于<C-c>转义,当文件以.sql结尾时,它似乎以令人沮丧的2秒暂停开始一些搜索.特别是,k在暂停期间键入会导致插入不需要的sql关键字的冲突.
.vimrc有
filetype plugin off
set omnifunc=
Run Code Online (Sandbox Code Playgroud)
并:filetype返回filetype detection:ON plugin:OFF indent:ON
但在插入模式下<C-c>k仍然打印
-- Omni completion (^O^N^P)
match 1 of 80` while autocompleting
Run Code Online (Sandbox Code Playgroud)
并:verbose imap <C-c>k返回
i <C-C>k *@<C-\><C-O>:call sqlcomplete#Map("sqlKeyword\\w*")<CR><C-X><C-O>
Last set from ~/projects.vim
Run Code Online (Sandbox Code Playgroud)
并且verbose set omnifunc可以被覆盖,即使我:set omnifunc=(当我将其设置为空)直接:
omnifunc=sqlcomplete#Complete
Last set from /opt/local/share/vim/vim74/autoload/sqlcomplete.vim
Run Code Online (Sandbox Code Playgroud)
更多尽职调查:
我正在处理一些大型数据集,并且正在尽我所能保持R的内存限制.关于覆盖R对象的一个问题.我有一个大data.table(或任何R对象),它必须被复制tmp多次.问题是:如果我tmp在覆盖它之前删除它会有什么不同吗?在代码中:
for (1:lots_of_times) {
v_l_d_t_tmp <- copy(very_large_data_table) # Necessary copy of 7GB data
# table on 16GB machine. I can
# afford 2 but not 3 copies.
### do stuff to v_l_d_t_tmp and output
rm (v_l_d_t_tmp) # The question is whether this rm keeps max memory
# usage lower, or if it is equivalent to what an
# overwrite will automatically do on the next iteration.
}
Run Code Online (Sandbox Code Playgroud)
假设副本是必要的(如果我到达我需要very_large_data_table在每个循环中从磁盘读取的点,我会这样做,但问题是:如果我v_l_d_t_tmp在加载到它之前明确删除它会对最大内存使用产生任何影响吗?再次?).
或者,为了教导男人钓鱼,我可以输入什么(在R中,让我们不要进入ps …
最后一个例子用来工作,现在却没有.函数内部没有字符串插值?也许它与变量范围有关?有什么建议?
library(gsubfn)
#R.Version() # I'm using 2.15.0, just upgraded from 2.13.something.
### dumb example of a function
g <- function() {for (a in 1:2) { print(paste('is a', a)) }}
g()
### same thing, outside a function, using string interpolation
for (a in 1:2) { fn$print('is a $a') }
rm(a)
### and now string interpolation inside a function
h <- function() {for (a in 1:2) { fn$print('is a $a') }}
h()
Run Code Online (Sandbox Code Playgroud)
最后一个例子告诉我
Error in eval(expr, envir, enclos) : object 'a' not …Run Code Online (Sandbox Code Playgroud) 我正在尝试列出数据库中的所有表.\ dt没有这样做,可能是因为名称冲突.我尝试了很多命令,但是当不同模式中的两个表共享一个名称时,只有一个表被\ dt列出:
CREATE DATABASE tester;
\c tester
CREATE SCHEMA hid1;
CREATE SCHEMA hid2;
CREATE TABLE a (a int);
CREATE TABLE b (a int);
CREATE TABLE hid1.a (a int);
CREATE TABLE hid1.b (a int);
CREATE TABLE hid1.c (a int);
CREATE TABLE hid2.a (a int);
CREATE TABLE hid2.d (a int);
\dt
SET search_path TO public,hid1,hid2;
\dt
SET search_path TO hid1,public,hid2;
\dt
SET search_path TO hid2,hid1,public;
\dt
Run Code Online (Sandbox Code Playgroud)
即
tester=# \dt
List of relations
Schema | Name | Type | Owner
--------+------+-------+-------
hid1 …Run Code Online (Sandbox Code Playgroud) r ×4
performance ×2
autocomplete ×1
bigdata ×1
debugging ×1
for-loop ×1
gsub ×1
list ×1
memory ×1
names ×1
nested ×1
postgresql ×1
vim ×1