这个问题链接到这个SO答案,除了在这里我想使用在a中指定为函数arg的变量mutate_().如果我不做任何"计算",它的工作原理是mutate_():
data <-
data.frame(v1=c(1,2),
v2=c(3,4))
func1 <- function(df, varname){
res <-
df %>%
mutate_(v3=varname)
return(res)
}
func1(data, "v1")
Run Code Online (Sandbox Code Playgroud)
这给了预期:
v1 v2 v3
1 1 3 1
2 2 4 2
Run Code Online (Sandbox Code Playgroud)
但如果我做这样的事情,似乎我没有正确指定"v3":
func2 <- function(df, varname){
res <-
df %>%
mutate_(v3=sum(varname))
return(res)
}
func2(data, "v1")
Run Code Online (Sandbox Code Playgroud)
不起作用; 为什么它不等同于函数之外?:
data %>%
mutate(v3=sum(v1))
Run Code Online (Sandbox Code Playgroud)
得到:
v1 v2 v3
1 1 3 3
2 2 4 3
Run Code Online (Sandbox Code Playgroud)
更新(在@docendo discimus的解决方案之后):关于使用lazyeval::interp()工作的解决方案.但是,如果有一个更复杂的功能,我似乎得到了很多打字.例如.我想要一个函数,可以在计数数据框中为所有NP组合返回得分和fisher的2x2 pvalue,c.
require(plyr)
require(dplyr)
require(lazyeval)
set.seed(8)
df <-
data.frame(
N …Run Code Online (Sandbox Code Playgroud) 我的应用程序中有一个带有许多属性的Kotlin类,我想构建的是一种将变量名存储在字典中的方法。字典看起来像这样:
HashMap<String, Pair<Any, Any>>()
Run Code Online (Sandbox Code Playgroud)
这样做的目的是存储对特定属性所做的更改,我将变量的名称存储为键,在对中存储旧值和新值。为了通知更改,我使用观察者模式。因此,每当从属性调用设置器时,都会通知更改并将其存储到字典中。
以下代码导致以下问题:
var person = Person("Harry", 44)
person.age = 45
Run Code Online (Sandbox Code Playgroud)
HashMap("age", (44, 45))
现在,我只是将变量名硬编码为字符串,所以我的问题是:
如何在Kotlin中动态获取变量名?
我在Java中看到了相同的问题:Java思考:如何获取变量的名称?
另外,关于同一主题的其他一些问题声称这是不可能的:获取变量的name属性
我可以理解,不可能获得变量的名称,因为简单的编译器没有该信息,但是我仍然急于查看其他人是否对此问题有任何解决方案。
标题是相当不言自明的,但我将重申:对于描述对象位置和大小的矩形,什么是一个好的变量名称.
编辑 - 对象是类似Pong的游戏的桨,并且已经有一个名为"bounds"的矩形来限制桨可以移动到的位置.
我最终选择了'padsles'的位置和尺寸,'movableArea'限制了桨可以移动的位置.
如果已有答案,请原谅我,但我无法从档案中找到答案.
我通过R中的for循环生成了一个非常相似的函数列表:
adoptint.fun=list()
for(i in 1:40) {
#function name for each column
func.name <- paste('adoptint',i,sep='')
#function
func = paste('function(yearenter, adoptyear, yearleave) {ifelse(is.na(yearenter) | yearenter >', i+1905, ' | is.na(adoptyear) | yearleave > ', i+1905, ', NA, ifelse(yearenter <= ', i+1905, ' & adoptyear <= ', i+1905, ', 1, 0))}', sep='')
adoptint.fun[[func.name]] = eval(parse(text=func))
}
Run Code Online (Sandbox Code Playgroud)
我现在有兴趣应用此函数为尚未在数据框中创建的变量生成值.我想使用循环或类似方法来做这个,因为过程是相同的,尽管特定值在40次迭代中发生变化.代码看起来像:
#generate variables that will be inserted into dataframe, dfanal.reshape
var_names <- paste("dfanal.reshape$adopt", 1:40, sep="")
#run function i to obtain values for variable i, which should …Run Code Online (Sandbox Code Playgroud) 可能重复:
任何编程语言中的变量名是否占用内存空间
我刚刚阅读有关内存分配的内容,不禁要问这个问题:
做到这两点
int x = 4;
Run Code Online (Sandbox Code Playgroud)
和
int this_is_really_really_long_name_for_an_integer_variable = 4;
Run Code Online (Sandbox Code Playgroud)
占用相同数量的内存(变量占用的总内存.不仅仅是)sizeof(int)
我知道这个问题与"编程语言和编译器构造"有关.但是,我没有研究它:(
我目前正在尝试在D中编写一个程序,当调用并传递一个对象时,它会将对象序列化为XML文档.我想让它像传递对象一样简单,但我不能完全确定它可以完成.例:
class A
{
//Constructors and fluff
....
int firstInt;
int secondInt;
}
.....
A myObj = new A();
XMLSerialize(myObj);
Run Code Online (Sandbox Code Playgroud)
输出将是
<A.A>
<firstInt></firstInt>
<secondInt></secondInt>
</A.A>
Run Code Online (Sandbox Code Playgroud)
那么,我是否有可能在对象内部获取变量的名称,或者是否必须手动完成?
使用^w::MsgBox, 100%返回值:
该参数缺少变量名。
但没有标志的同一条线也%有效。如何修复它?
我正在为脚本的调试模式编写一些打印输出.是否有一种紧凑的方法在符合条件的列表中打印这些变量的名称?
specification_aw3 = 43534
specification_hg7 = 75445
specification_rt5 = 0
specification_nj8 = 5778
specification_lo4 = 34
specification_ee2 = 8785
specification_ma2 = 67
specification_pw1 = 1234
specification_mu6 = 0
specification_xu8 = 12465
specifications = [
specification_aw3,
specification_hg7,
specification_rt5,
specification_nj8,
specification_lo4,
specification_ee2,
specification_ma2,
specification_pw1,
specification_mu6,
specification_xu8
]
if any(specification == 0 for specification in specifications):
# magic code to print variables' names
# e.g. "variables equal to 0: \"specification_rt5\", \"specification_mu6\"
Run Code Online (Sandbox Code Playgroud)
正如9000所暗示的那样,不言而喻,定义字典对于我在此定义的最小工作示例来说是一种理性的方法.请假设这对于现有代码项目来说不是一个可行的选项,而且我正在寻找一个快速,紧凑(似乎难看)的代码,仅用于调试.
编辑:类似于我想要的东西的插图
所以这是我正在寻找的开始:
print("specifications equal to zero:")
callers_local_objects = inspect.currentframe().f_back.f_locals.items() …Run Code Online (Sandbox Code Playgroud) 假设:
aa = c('A','B','C')
bb = c('D','E','F')
list1 = list(aa,bb)
vect1 = c(aa,bb)
Run Code Online (Sandbox Code Playgroud)
有没有办法从 list1 或 vect1 中将变量名称提取为字符串('aa'、'bb')?这些信息是否存储在列表和向量中?如果不是,那么合适的格式是什么?
提前致谢!
我对使用感叹号在此链接中命名变量感到困惑.
首先,它的工作正常 JuliaPro Command Prompt
然后,我关闭JuliaPro Command Prompt并再次打开它,试图测试不同的变量命名:
我无法理解如何使用感叹号.