variable-names

In R: pass column name as argument and use it in function with dplyr::mutate() and lazyeval::interp()

混江龙づ霸主 提交于 2019-11-29 12:09:13
This question links to this SO answer except that here I want to use the variable specified as function arg in a mutate_() . It works if I don't make any "calculations" in the 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") This give the expected: v1 v2 v3 1 1 3 1 2 2 4 2 But if I do anything like this, it seems that I have not specified "v3" correctly: func2 <- function(df, varname){ res <- df %>% mutate_(v3=sum(varname)) return(res) } func2(data, "v1") Does not work; how come it is not

How do global and local variables behave in this case?

♀尐吖头ヾ 提交于 2019-11-28 13:13:28
This is saved in my file function_local.py : x = 50 def func(x): print('x is', x) x = 2 print('Changed local x to', x) func(x) print('x is still', x) Output: $ python function_local.py x is 50 Changed local x to 2 x is still 50 Question: When we print the first line inside the Function, why does it print out 50, not 2? Even if it is said, in the function, that x = 2 ? In case you assign to a variable name (that wasn't declared global or nonlocal ) in a function or use the variable name in the argument list of the function the variable name will become part of the function. In that case you

In R: pass column name as argument and use it in function with dplyr::mutate() and lazyeval::interp()

我们两清 提交于 2019-11-28 05:32:52
问题 This question links to this SO answer except that here I want to use the variable specified as function arg in a mutate_() . It works if I don't make any "calculations" in the 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") This give the expected: v1 v2 v3 1 1 3 1 2 2 4 2 But if I do anything like this, it seems that I have not specified "v3" correctly: func2 <- function(df, varname){ res <-

How do global and local variables behave in this case?

半腔热情 提交于 2019-11-27 06:32:59
问题 This is saved in my file function_local.py : x = 50 def func(x): print('x is', x) x = 2 print('Changed local x to', x) func(x) print('x is still', x) Output: $ python function_local.py x is 50 Changed local x to 2 x is still 50 Question: When we print the first line inside the Function, why does it print out 50, not 2? Even if it is said, in the function, that x = 2 ? 回答1: In case you assign to a variable name (that wasn't declared global or nonlocal ) in a function or use the variable name

Programmatic way to get variable name in C?

梦想的初衷 提交于 2019-11-27 03:27:43
I am developing a tool to dump data from variables. I need to dump the variable name, and also the values. My solution: Store variable name as a string, and print the "variable name", followed by its value. Is there any programmatic way to know the variable name? You could try something like this: #define DUMP(varname) fprintf(stderr, "%s = %x", #varname, varname); I used to use this header I wrote, when I was new to C, it might contain some useful ideas. For example this would allow you to print a C value and provide the format specifier in one (as well as some additional information):

How can I load an object into a variable name that I specify from an R data file?

强颜欢笑 提交于 2019-11-26 23:57:45
When you save a variable in an R data file using save , it is saved under whatever name it had in the session that saved it. When I later go to load it from another session, it is loaded with the same name, which the loading script cannot possibly know. This name could overwrite an existing variable of the same name in the loading session. Is there a way to safely load an object from a data file into a specified variable name without risk of clobbering existing variables? Example: Saving session: x = 5 save(x, file="x.Rda") Loading session: x = 7 load("x.Rda") print(x) # This will print 5.

$ in variable name?

☆樱花仙子☆ 提交于 2019-11-26 15:27:36
I stumbled on some C++ code like this: int $T$S; First I thought that it was some sort of PHP code or something wrongly pasted in there but it compiles and runs nicely (on MSVC 2008). What kind of characters are valid for variables in C++ and are there any other weird characters you can use? The only legal characters according to the standard are alphanumerics and the underscore. The standard does require that just about anything Unicode considers alphabetic is acceptable (but only as single code-point characters). In practice, implementations offer extensions (i.e. some do accept a $) and

Programmatic way to get variable name in C?

岁酱吖の 提交于 2019-11-26 10:29:23
问题 I am developing a tool to dump data from variables. I need to dump the variable name, and also the values. My solution: Store variable name as a string, and print the \"variable name\", followed by its value. Is there any programmatic way to know the variable name? 回答1: You could try something like this: #define DUMP(varname) fprintf(stderr, "%s = %x", #varname, varname); I used to use this header I wrote, when I was new to C, it might contain some useful ideas. For example this would allow

How can I load an object into a variable name that I specify from an R data file?

瘦欲@ 提交于 2019-11-26 08:48:41
问题 When you save a variable in an R data file using save , it is saved under whatever name it had in the session that saved it. When I later go to load it from another session, it is loaded with the same name, which the loading script cannot possibly know. This name could overwrite an existing variable of the same name in the loading session. Is there a way to safely load an object from a data file into a specified variable name without risk of clobbering existing variables? Example: Saving

Create variables with names from strings

核能气质少年 提交于 2019-11-26 05:48:07
问题 Let\'s assume that I want to create 10 variables which would look like this: x1 = 1; x2 = 2; x3 = 3; x4 = 4; . . xi = i; This is a simplified version of what I\'m intending to do. Basically I just want so save code lines by creating these variables in an automated way. Is there the possibility to construct a variable name in Matlab? The pattern in my example would be [\"x\", num2str(i)] . But I cant find a way to create a variable with that name. 回答1: You can do it with eval but you really