variable-assignment

generate variable names (something like get())

血红的双手。 提交于 2019-12-01 23:03:28
How can i substitute this code with a loop? m1 <- ggplot(foo) m2 <- ggplot(foo) ... m9 <- ggplot(foo) I guess i need something like get() to replace magic(), But get did not work here. for (i in 1:9){ magic(i) <- ggplot(foo) } Use a list: out <- list() for (i in 1:9){ out[[i]] <- ggplot(foo) } This will work: p <- qplot(data = mtcars, wt, mpg) for(i in 1:9) assign(paste0("m",i),p) ls() [1] "i" "m1" "m2" "m3" "m4" "m5" "m6" "m7" "m8" "m9" "p" Something like this should work: for (i in 1:9) { name <- paste("m", i, sep="") assign(name, ggplot(foo)) } 来源: https://stackoverflow.com/questions

Why am I getting this output from this simple splice command?

假装没事ソ 提交于 2019-12-01 22:38:51
I have a small issue with understanding why I'm getting this output. var arr = ["a", "b", "c", "d", "e", "f"]; arr.splice(2,0,"1"); console.log(arr); var arr2 = ["a", "b", "c", "d", "e", "f"]; arr2 = arr2.splice(2,0,"2"); console.log(arr2); ouput is: [ 'a', 'b', '1', 'c', 'd', 'e', 'f' ] [] Why is the second line of output not: [ 'a', 'b', '2', 'c', 'd', 'e', 'f' ] Is it an issue of assignment or what? Reading about splice method. It returns An array containing the removed elements. If only one element is removed, an array of one element is returned. So by doing arr2 = arr2.splice(2,0,"2");

How do I make a class assignable to primitives? Or, how do I make a scalar class?

≡放荡痞女 提交于 2019-12-01 22:30:43
I was wondering if it's possible to make my class Time { public: Time(); explicit Time( const double& d); Time& operator=( const Time& time); Time& operator=( const double& d); }; assignable to the primitive double? I'm using Time as an IV a lot and need to do a lot of scalar operations on it, so it needs to "mingle" with DV's which are usually ordinary doubles. Adding a second assignment operator did the trick the other way around. A lot of operations still aren't possible with just this though. I've been writing operators outside of the Time class to allow for addition, substraction,

How can I assign a value to the diagonals of a 4-D matrix using linear indexing in MATLAB?

纵然是瞬间 提交于 2019-12-01 22:02:59
问题 I have a 4-D matrix A of size NxNxPxQ . How can I easily change the diagonal values to 1 for each NxN 2-D submatrix in a vectorized way? 回答1: Incorporating gnovice's suggestion, an easy way to index the elements is: [N,~,P,Q]=size(A);%# get dimensions of your matrix diagIndex=repmat(logical(eye(N)),[1 1 P Q]);%# get logical indices of the diagonals A(diagIndex)=1;%# now index your matrix and set the diagonals to 1. 回答2: You can actually do this very simply by directly computing the linear

Cannot invoke “+=” with an argument list of type (Int, @value Int)

坚强是说给别人听的谎言 提交于 2019-12-01 22:01:38
I have a class Transaction which has a var amount of type Int . I want to access it from another class, where I have an array of Transactions and sum all of their amounts. So I have this piece of code func computeTotal()-> Int{ let total = 0 for transaction in transactions{ //get the amounts of each and sum all of them up total += transaction.amount } return total } But it gives me an error Cannot invoke "+=" with an argument list of type (Int, @value Int) What can cause that? I know that in Swift both operands must be the same type, but they are both of type Int in my code. let creates an

How does this chain assignment work?

家住魔仙堡 提交于 2019-12-01 21:32:12
Consider the following code; it is a bad programming practice. I am wondering why the resulting list A is [1, 1, 3] rather than [1, 2, 1] . From the view of Java, the result should be [1, 2, 1] . Can anyone explain why this result is what it is? A = [1, 2, 3] t = 2 t = A[t] = A.count(3) After evaluation, A is [1, 1, 3] and t is 1 . My Python version is 3.3. Henrik On line 3 you have a chained assignment t = A[t] = A.count(3) t = A.count(3) is evaluated first – t set to the return value of A.count(3) which in this case is 1. Then the member of A at index t (=1) is set to the return value of A

How to assign result of integrate in R to a numeric variable?

最后都变了- 提交于 2019-12-01 21:25:10
问题 R example: k=6 f<-function(s){s^(k-1)*exp(-s)} integrate(f,0,Inf) The output of integrate is the string: 120 with absolute error < 7.3e-05 I want to assign the first value in the string (120, the integral) to a variable. How to do that? 回答1: The result of integrate is a list : > temp <- integrate(f,0,Inf) > temp 120 with absolute error < 7.3e-05 > str(temp) List of 5 $ value : num 120 $ abs.error : num 7.34e-05 $ subdivisions: int 5 $ message : chr "OK" $ call : language integrate(f = f,

double '=' in initialization

泪湿孤枕 提交于 2019-12-01 20:17:51
I came across this line when I was doing some laborations: int home_address = current_pos_ = hash(key, size_); And I was wondering what it actually does? (not the hashfunction or anything, just the "int variable = variable = value;" thing) That expression is read as: int home_address = (current_pos_ = hash(key,size_)); It assigns the result of hash(key,size_) into current_pos_ and it then assigns the value of current_pos_ into home_address . The assignment operator evaluates to the final value of its left argument, so this code assigns hash(key, size_) to current_pos_ , and initialises home

Assignment issue OCJP; why can't I pass an int to a short?

拟墨画扇 提交于 2019-12-01 20:14:40
问题 I have two pieces of code. One works, another doesn't, but both seem to do identical things. This works: short s=7; but the below code doesn't. Instead, it gives error: can't assign int to short I know an integer number literal by default is int , but if it can be assigned directly above, then why not when passing to a method? class Demo1{ public static void main(String[] args){ new Demo1().go(7); } void go(short s){System.out.println("short");} } 回答1: The rules are different for assignment

How to assign result of integrate in R to a numeric variable?

≯℡__Kan透↙ 提交于 2019-12-01 19:50:22
R example: k=6 f<-function(s){s^(k-1)*exp(-s)} integrate(f,0,Inf) The output of integrate is the string: 120 with absolute error < 7.3e-05 I want to assign the first value in the string (120, the integral) to a variable. How to do that? The result of integrate is a list : > temp <- integrate(f,0,Inf) > temp 120 with absolute error < 7.3e-05 > str(temp) List of 5 $ value : num 120 $ abs.error : num 7.34e-05 $ subdivisions: int 5 $ message : chr "OK" $ call : language integrate(f = f, lower = 0, upper = Inf) - attr(*, "class")= chr "integrate" You can access elements by name: > temp$value [1]