[R] Towards genetic programming...
Hellou R-users, I've been interested in genetic programming and succeeded in doing a simple R-application which creates similarity measures for musical set theory. Before that program I made an even simpler program which generates mathematical functions and plots them. If you are interested in, see http://rgeneticprogramming.blogspot.com/ Atte Tenkanen University of Turku __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Break during the recursion?
Hi, Is it possible to break using if-condition during the recursive function? Here is a function which almost works. It is for inorder-tree-walk. iotw-function(v,i,Stack,Indexes) # input: a vector and the first index (1), Stack=c(), Indexes=c(). { print(Indexes) # if (sum(i)==0) break # Doesn't work... if (is.na(v[i])==FALSE is.null(unlist(v[i]))==FALSE) {Stack=c(i,Stack); i=2*i; iotw(v,i,Stack,Indexes)} Indexes=c(Indexes,Stack[1]) Stack=pop.stack(Stack)$vector Indexes=c(Indexes,Stack[1]) i=2*Stack[1]+1 Stack=pop.stack(Stack)$vector iotw(v,i,Stack,Indexes) } v=c(`-`,`+`,1,`^`,`^`,NA,NA,X,3,X,2) Stack=c() Indexes=c() iotw(v,1,Stack,Indexes) NULL NULL NULL NULL NULL [1] 8 4 [1] 8 4 [1] 8 4 9 2 [1] 8 4 9 2 [1] 8 4 9 2 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 Error in if (is.na(v[i]) == FALSE is.null(unlist(v[i])) == FALSE) { : argument is of length zero Regards, Atte Tenkanen University of Turku, Finland __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Break during the recursion?
Oh. I forgot one extra-function: pop.stack-function(v){ if(length(v)==0){x=NA} if(length(v)==1){x=v[1]; v=c()} if(length(v)1){x=v[1]; v=v[2:length(v)]} return(list(vector=v,x=x)) } Atte Hi, Is it possible to break using if-condition during the recursive function? Here is a function which almost works. It is for inorder-tree-walk. iotw-function(v,i,Stack,Indexes) # input: a vector and the first index (1), Stack=c(), Indexes=c(). { print(Indexes) # if (sum(i)==0) break # Doesn't work... if (is.na(v[i])==FALSE is.null(unlist(v[i]))==FALSE) {Stack=c(i,Stack); i=2*i; iotw(v,i,Stack,Indexes)} Indexes=c(Indexes,Stack[1]) Stack=pop.stack(Stack)$vector Indexes=c(Indexes,Stack[1]) i=2*Stack[1]+1 Stack=pop.stack(Stack)$vector iotw(v,i,Stack,Indexes) } v=c(`-`,`+`,1,`^`,`^`,NA,NA,X,3,X,2) Stack=c() Indexes=c() iotw(v,1,Stack,Indexes) NULL NULL NULL NULL NULL [1] 8 4 [1] 8 4 [1] 8 4 9 2 [1] 8 4 9 2 [1] 8 4 9 2 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 Error in if (is.na(v[i]) == FALSE is.null(unlist(v[i])) == FALSE) { : argument is of length zero Regards, Atte Tenkanen University of Turku, Finland __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Break during the recursion?
On 15/07/2007 10:06 AM, Atte Tenkanen wrote: Hi, Is it possible to break using if-condition during the recursive function? You can do if (condition) return(value) Here is a function which almost works. It is for inorder-tree- walk. iotw-function(v,i,Stack,Indexes) # input: a vector and the first index (1), Stack=c(), Indexes=c(). { print(Indexes) # if (sum(i)==0) break # Doesn't work... if (sum(i)==0) return(NULL) should work. Duncan Murdoch Hmm - - - I'd like to save the Indexes-vector (in the example c(8,4,9,2,10,5,11,1,3)) and stop, when it is ready. The results are enclosed to the end. Atte if (is.na(v[i])==FALSE is.null(unlist(v[i]))==FALSE) {Stack=c(i,Stack); i=2*i; iotw(v,i,Stack,Indexes)} Indexes=c(Indexes,Stack[1]) Stack=pop.stack(Stack)$vector Indexes=c(Indexes,Stack[1]) i=2*Stack[1]+1 Stack=pop.stack(Stack)$vector iotw(v,i,Stack,Indexes) } v=c(`-`,`+`,1,`^`,`^`,NA,NA,X,3,X,2) Stack=c() Indexes=c() iotw(v,1,Stack,Indexes) NULL NULL NULL NULL NULL [1] 8 4 [1] 8 4 [1] 8 4 9 2 [1] 8 4 9 2 [1] 8 4 9 2 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 Error in if (is.na(v[i]) == FALSE is.null(unlist(v[i])) == FALSE) { : argument is of length zero Regards, Atte Tenkanen University of Turku, Finland iotw(v,1,Stack,Indexes) NULL NULL NULL NULL NULL [1] 8 4 [1] 8 4 [1] 8 4 9 2 [1] 8 4 9 2 [1] 8 4 9 2 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 5 1 [1] 8 4 9 2 5 1 [1] 8 4 9 2 5 1 3 [1] 8 4 9 2 5 1 3 [1] 8 4 9 2 [1] 8 4 9 2 [1] 8 4 9 2 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 5 1 [1] 8 4 9 2 5 1 [1] 8 4 9 2 5 1 3 [1] 8 4 9 2 5 1 3 [1] 8 4 [1] 8 4 [1] 8 4 9 2 [1] 8 4 9 2 [1] 8 4 9 2 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 5 1 [1] 8 4 9 2 5 1 [1] 8 4 9 2 5 1 3 [1] 8 4 9 2 5 1 3 [1] 8 4 9 2 [1] 8 4 9 2 [1] 8 4 9 2 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 5 1 [1] 8 4 9 2 5 1 [1] 8 4 9 2 5 1 3 [1] 8 4 9 2 5 1 3 [1] 4 2 [1] 4 2 [1] 4 2 [1] 4 2 10 5 [1] 4 2 10 5 [1] 4 2 10 5 11 1 [1] 4 2 10 5 11 1 [1] 4 2 10 5 11 1 3 [1] 4 2 10 5 11 1 3 [1] 4 2 10 5 11 1 [1] 4 2 10 5 11 1 [1] 4 2 10 5 11 1 3 [1] 4 2 10 5 11 1 3 [1] 4 2 10 5 [1] 4 2 10 5 [1] 4 2 10 5 11 1 [1] 4 2 10 5 11 1 [1] 4 2 10 5 11 1 3 [1] 4 2 10 5 11 1 3 [1] 4 2 10 5 11 1 [1] 4 2 10 5 11 1 [1] 4 2 10 5 11 1 3 [1] 4 2 10 5 11 1 3 [1] 4 2 5 1 [1] 4 2 5 1 [1] 4 2 5 1 3 [1] 4 2 5 1 3 [1] 2 1 [1] 2 1 [1] 2 1 3 [1] 2 1 3 [1] 1 [1] 1 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R
Re: [R] Break during the recursion?
On 15/07/2007 10:33 AM, Atte Tenkanen wrote: On 15/07/2007 10:06 AM, Atte Tenkanen wrote: Hi, Is it possible to break using if-condition during the recursive function? You can do if (condition) return(value) Here is a function which almost works. It is for inorder-tree- walk. iotw-function(v,i,Stack,Indexes) # input: a vector and the first index (1), Stack=c(), Indexes=c(). { print(Indexes) # if (sum(i)==0) break # Doesn't work... if (sum(i)==0) return(NULL) should work. Duncan Murdoch Hmm - - - I'd like to save the Indexes-vector (in the example c(8,4,9,2,10,5,11,1,3)) and stop, when it is ready. This seems more like a problem with the design of your function than a question about R. I can't really help you with that, because your description of the problem doesn't make sense to me. What does it mean to do an inorder tree walk on something that isn't a tree? Duncan Murdoch The symbols in vector v have been originally derived from tree. See http://users.utu.fi/attenka/Tree.jpg But perhaps there's another way to do this, for instance by using loops and if-conditions? Atte The results are enclosed to the end. Atte if (is.na(v[i])==FALSE is.null(unlist(v[i]))==FALSE) {Stack=c(i,Stack); i=2*i; iotw(v,i,Stack,Indexes)} Indexes=c(Indexes,Stack[1]) Stack=pop.stack(Stack)$vector Indexes=c(Indexes,Stack[1]) i=2*Stack[1]+1 Stack=pop.stack(Stack)$vector iotw(v,i,Stack,Indexes) } v=c(`-`,`+`,1,`^`,`^`,NA,NA,X,3,X,2) Stack=c() Indexes=c() iotw(v,1,Stack,Indexes) NULL NULL NULL NULL NULL [1] 8 4 [1] 8 4 [1] 8 4 9 2 [1] 8 4 9 2 [1] 8 4 9 2 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 Error in if (is.na(v[i]) == FALSE is.null(unlist(v[i])) == FALSE) { : argument is of length zero Regards, Atte Tenkanen University of Turku, Finland iotw(v,1,Stack,Indexes) NULL NULL NULL NULL NULL [1] 8 4 [1] 8 4 [1] 8 4 9 2 [1] 8 4 9 2 [1] 8 4 9 2 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 5 1 [1] 8 4 9 2 5 1 [1] 8 4 9 2 5 1 3 [1] 8 4 9 2 5 1 3 [1] 8 4 9 2 [1] 8 4 9 2 [1] 8 4 9 2 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 5 1 [1] 8 4 9 2 5 1 [1] 8 4 9 2 5 1 3 [1] 8 4 9 2 5 1 3 [1] 8 4 [1] 8 4 [1] 8 4 9 2 [1] 8 4 9 2 [1] 8 4 9 2 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 5 1 [1] 8 4 9 2 5 1 [1] 8 4 9 2 5 1 3 [1] 8 4 9 2 5 1 3 [1] 8 4 9 2 [1] 8 4 9 2 [1] 8 4 9 2 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 10 5 11 1 3 [1] 8 4 9 2 5 1 [1] 8 4 9 2 5 1 [1] 8
Re: [R] Break during the recursion?
Here is now more elegant function for inorder tree walk, but I still can't save the indexes!? This version now prints them ok, but if I use return, I get only the first v[i]. leftchild-function(i){return(2*i)} rightchild-function(i){return(2*i+1)} iotw-function(v,i) { if (is.na(v[i])==FALSE is.null(unlist(v[i]))==FALSE) { iotw(v,leftchild(i)) print(v[i]) # return doesn't work here iotw(v,rightchild(i)) } } iotw(1:11,1) [1] 8 [1] 4 [1] 9 [1] 2 [1] 10 [1] 5 [1] 11 [1] 1 [1] 6 [1] 3 [1] 7 Yours faithfully ;-) Atte - Original Message - From: hadley wickham [EMAIL PROTECTED] Date: Sunday, July 15, 2007 6:58 pm Subject: Re: [R] Break during the recursion? On 7/15/07, Duncan Murdoch [EMAIL PROTECTED] wrote: On 15/07/2007 11:36 AM, Atte Tenkanen wrote: On 15/07/2007 10:33 AM, Atte Tenkanen wrote: On 15/07/2007 10:06 AM, Atte Tenkanen wrote: Hi, Is it possible to break using if-condition during the recursive function? You can do if (condition) return(value) Here is a function which almost works. It is for inorder- tree- walk. iotw-function(v,i,Stack,Indexes) # input: a vector and the first index (1), Stack=c(), Indexes=c(). { print(Indexes) # if (sum(i)==0) break # Doesn't work... if (sum(i)==0) return(NULL) should work. Duncan Murdoch Hmm - - - I'd like to save the Indexes-vector (in the example c(8,4,9,2,10,5,11,1,3)) and stop, when it is ready. This seems more like a problem with the design of your function than a question about R. I can't really help you with that, because your description of the problem doesn't make sense to me. What does it mean to do an inorder tree walk on something that isn't a tree? Duncan Murdoch The symbols in vector v have been originally derived from tree. See http://users.utu.fi/attenka/Tree.jpg But perhaps there's another way to do this, for instance by using loops and if-conditions? Or perhaps by doing the tree walk on the tree, before you collapse it into a vector. If it's a binary tree with n levels, I think you should be able to generate the positions more directly, depending on how the tree has been flattened. Binary heaps work this way, so that might be a good place to start. See http://en.wikipedia.org/wiki/Binary_heap, particularly heap implementation. Hadley __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Break during the recursion?
TNX Hadley! Atte - Original Message - From: hadley wickham [EMAIL PROTECTED] Date: Sunday, July 15, 2007 11:04 pm Subject: Re: [R] Break during the recursion? On 7/15/07, Atte Tenkanen [EMAIL PROTECTED] wrote: Here is now more elegant function for inorder tree walk, but I still can't save the indexes!? This version now prints them ok, but if I use return, I get only the first v[i]. leftchild-function(i){return(2*i)} rightchild-function(i){return(2*i+1)} iotw-function(v,i) { if (is.na(v[i])==FALSE is.null(unlist(v[i]))==FALSE) { iotw(v,leftchild(i)) print(v[i]) # return doesn't work here iotw(v,rightchild(i)) } } Shouldn't you return: c(iotw(v, leftchild(i)), v[i], iotw(v, rightchild(i))) (and rewrite the conditition to return null if the node doesn't exist, I think it reads clearer that way) Hadley __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] is.null doesn't work
Seems to work, if I unlist the argument at first ;-) Atte Hi, What's wrong here?: v=c(`-`,`+`,1,`^`,`^`,NA,NA,X,9,X,2) i2=16 v[i2] [[1]] NULL is.null(v[i2]) [1] FALSE Is it a bug or have I misunderstood something? Atte Tenkanen University of Turku, Finland __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] is.null doesn't work
Hi, What's wrong here?: v=c(`-`,`+`,1,`^`,`^`,NA,NA,X,9,X,2) i2=16 v[i2] [[1]] NULL is.null(v[i2]) [1] FALSE Is it a bug or have I misunderstood something? Atte Tenkanen University of Turku, Finland __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] from character string to function body?
Dear R users, I wonder if it is possible to form a function from a character string. Here is an example: x=3 `-`(`+`(`^`(x,3),`^`(x,2)),1) # Here is my function evaluated. [1] 35 V=list(`-`,(,`+`,(,`^`,(,x,,,3,),,,`^`,(,x,,,2,),),,,1,)) # Here I construct the string, it could be vector as well? S=noquote(paste(V,collapse=)) S [1] `-`(`+`(`^`(x,3),`^`(x,2)),1) # Here is the same as a character string. Now I'd like to create a function using this string, something like this, but of course, this doesn't work: S=as.expression(S) F1-function(x){S} Is there some way to do this? Cheers, Atte Tenkanen University of Turku, Finland __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] from character string to function body?
Thanks Sundar! I wonder that - when evaluating F1 - the right mathematical formula is now also printed! F1 function (x) x^3 + x^2 - 1 Atte Atte Tenkanen said the following on 7/7/2007 8:41 AM: Dear R users, I wonder if it is possible to form a function from a character string. Here is an example: x=3 `-`(`+`(`^`(x,3),`^`(x,2)),1) # Here is my function evaluated. [1] 35 V=list(`- `,(,`+`,(,`^`,(,x,,,3,),,,`^`,(,x,,,2,),),,,1,)) # Here I construct the string, it could be vector as well? S=noquote(paste(V,collapse=)) S [1] `-`(`+`(`^`(x,3),`^`(x,2)),1) # Here is the same as a character string. Now I'd like to create a function using this string, something like this, but of course, this doesn't work: S=as.expression(S) F1-function(x){S} Is there some way to do this? Cheers, Atte Tenkanen University of Turku, Finland __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code. How about: V - list(`- `,(,`+`,(,`^`,(,x,,,3,),,,`^`,(,x,,,2,),),,,1,)) # Here I construct the string, it could be vector as well? S - paste(V, collapse = ) F1 - function(x) {} body(F1) - parse(text = S) F1(3) # [1] 35 F1(2) # [1] 11 HTH, --sundar __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] New function combinations using tree structures?
Hi everybody, I'm still interesting the possibility to use R for genetic programming (see https://stat.ethz.ch/pipermail/r-help/2007-April/128782.html) and I'd like to know, how to express for instance this kind of functions (x^2+3x+1 etc, see the picture) using some kind of tree structures and what are needed to make similar recombination operations with them as seen in the picture: http://users.utu.fi/attenka/Kuva1.png I know the basic principles of mutation and mating etc, as far as genetic algorithms are concerned, but now I'd like to know the practical issues considering R and mating of these functions using trees. All suggestions are warmly appreciated. With best wishes, Atte __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Transfer entropy not implemented in R?
Hi, I couldn't find the 'transfer entropy' implemented in any R packages. I can't follow the math needed here (www.macalester.edu/~kaplan/knoxville/PRL00461.pdf). If it is not implemented, could somebody do it? Or write a garage level code for testing purposes? Atte Tenkanen __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] A function for raising a matrix to a power?
, The Center on Aging and Health Division of Geriatric Medicine and Gerontology Johns Hopkins University Ph: (410) 502-2619 Fax: (410) 614-9625 Email: [EMAIL PROTECTED] Webpage: http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html -- -- -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Paul Gilbert Sent: Monday, May 07, 2007 5:16 PM To: Atte Tenkanen Cc: r-help@stat.math.ethz.ch Subject: Re: [R] A function for raising a matrix to a power? You might try this, from 9/22/2006 with subject line Exponentiate a matrix: I am getting a bit rusty on some of these things, but I seem to recall that there is a numerical advantage (speed and/or accuracy?) to diagonalizing: expM - function(X,e) { v - La.svd(X); v$u %*% diag(v$d^e) %*% v$vt } P - matrix(c(.3,.7, .7, .3), ncol=2) P %*% P %*% P [,1] [,2] [1,] 0.468 0.532 [2,] 0.532 0.468 expM(P,3) [,1] [,2] [1,] 0.468 0.532 [2,] 0.532 0.468 I think this also works for non-integer, negative, large, and complex exponents: expM(P, 1.5) [,1] [,2] [1,] 0.3735089 0.6264911 [2,] 0.6264911 0.3735089 expM(P, 1000) [,1] [,2] [1,] 0.5 0.5 [2,] 0.5 0.5 expM(P, -3) [,1][,2] [1,] -7.3125 8.3125 [2,] 8.3125 -7.3125 expM(P, 3+.5i) [,1] [,2] [1,] 0.4713+0.0141531i 0.5287-0.0141531i [2,] 0.5287-0.0141531i 0.4713+0.0141531i Paul Gilbert Doran, Harold wrote: Suppose I have a square matrix P P - matrix(c(.3,.7, .7, .3), ncol=2) I know that P * P Returns the element by element product, whereas P%*%P Returns the matrix product. Now, P2 also returns the element by element product. But, is there a slick way to write P %*% P %*% P Obviously, P3 does not return the result I expect. Thanks, Harold Atte Tenkanen wrote: Hi, Is there a function for raising a matrix to a power? For example if you like to compute A%*%A%*%A, is there an abbreviation similar to A^3? Atte Tenkanen A=rbind(c(1,1),c(-1,-2)) A [,1] [,2] [1,]11 [2,] -1 -2 A^3 [,1] [,2] [1,]11 [2,] -1 -8 But: A%*%A%*%A [,1] [,2] [1,]12 [2,] -2 -5 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. La version française suit le texte anglais. -- -- This email may contain privileged and/or confidential inform...{{dropped}} __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code. La version française suit le texte anglais. This email may contain privileged and/or confidential info...{{dropped}} __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] A function for raising a matrix to a power?
Hi, Is there a function for raising a matrix to a power? For example if you like to compute A%*%A%*%A, is there an abbreviation similar to A^3? Atte Tenkanen A=rbind(c(1,1),c(-1,-2)) A [,1] [,2] [1,]11 [2,] -1 -2 A^3 [,1] [,2] [1,]11 [2,] -1 -8 But: A%*%A%*%A [,1] [,2] [1,]12 [2,] -2 -5 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] About autocorrelation
Hello R-users, I'd like to know if there is an autocorrelation function for cases when the time differences between the observations vary? I want to give both the time points (x) and values (y) for the function and get as an output an estimation of autocorrelation. Atte Tenkanen University of Turku, Finland __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] R-OSX error: 'memory not mapped'
On Wed, 18 Apr 2007, Atte Tenkanen wrote: I often get a following error with R *** caught segfault *** address 0x78807e00, cause 'memory not mapped' Possible actions: 1: abort (with core dump) 2: normal R exit 3: exit R without saving workspace 4: exit R saving workspace Selection: The system is OSX 4.9 and R-version 2.4.1. Is there something to d0? Does this involve your own compiled code? No. I have installed R 2.4.1 .dmg-package. If so, run R under a debugger (e.g. R -d gdb) when you will get more information. (You may also be able to get a core dump from option 1 and look at that in a debugger, but what happens is OS-dependent, including system settings in the OS.) If not, it is likely to be a MacOS-specific problem, so please send a reproducible example to the R-sig-mac list. Now working with linux-R but next time when using the OSX-version I'll do. Sometimes the console is full of red error texts. Perhaps I can copy and send them? Atte T. -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Fractals with R
Now problems with plot-command... I try to plot Julia set and the algorithm works, if the points are drawn during the loop. But if I want to save the values first to the matrix and then afterwards plot them at once, the picture is distorted. What's wrong with the plot-command? I think the PointsMatrix is ok? -Atte C=-0.7-0.4i # Complex parameter, connected to coordinate of the Mandelbrot set in a complex plane. Limits=c(-2,2) z=0+0i MaxIter=60 cl=colours() Step=seq(Limits[1],Limits[2],by=0.01) PointsMatrix=array(0,dim=c(length(Step)*length(Step),3)) a1=0 for(x in Step) { for(y in Step) { z1=x+y*1i n=0 z=z1 while(nMaxIter abs(z)2) { z=z^2+C n=n+1 } if(abs(z)2) colour=1 else colour=n*10 #points(z1, pch=., col=cl[colour]) # This works! # But this doesn't! a1=a1+1 PointsMatrix[a1,]=c(x,y,colour) } } #??? plot(PointsMatrix[,1], PointsMatrix[,2], xlim=Limits, ylim=Limits, col=cl[PointsMatrix[,3]], pch=.) ## Atte Tenkanen wrote: Hi, That is of counter for web page. Do you get some pop-up windows? Atte Hi Atte, __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Fractals with R
Here is a workable version for the Julia set. I put it also to http://fractalswithr.blogspot.com/ Atte Now problems with plot-command... I try to plot Julia set and the algorithm works, if the points are drawn during the loop. But if I want to save the values first to the matrix and then afterwards plot them at once, the picture is distorted. What's wrong with the plot-command? I think the PointsMatrix is ok? -Atte C=-0.7-0.4i # Complex parameter, connected to coordinate of the Mandelbrot set in a complex plane. Limits=c(-2,2) z=0+0i MaxIter=60 cl=colours() Step=seq(Limits[1],Limits[2],by=0.01) PointsMatrix=array(0,dim=c(length(Step)*length(Step),3)) a1=0 for(x in Step) { for(y in Step) { z1=x+y*1i n=0 z=z1 while(nMaxIter abs(z)2) { z=z^2+C n=n+1 } if(abs(z)2) colour=1 else colour=n*10 #points(z1, pch=., col=cl[colour]) # This works! # But this doesn't! a1=a1+1 PointsMatrix[a1,]=c(x,y,colour) } } #??? plot(PointsMatrix[,1], PointsMatrix[,2], xlim=Limits, ylim=Limits, col=cl[PointsMatrix[,3]], pch=.) #--- -# Atte Tenkanen wrote: Hi, That is of counter for web page. Do you get some pop-up windows? Atte Hi Atte, __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] R-OSX error: 'memory not mapped'
Hi, I often get a following error with R *** caught segfault *** address 0x78807e00, cause 'memory not mapped' Possible actions: 1: abort (with core dump) 2: normal R exit 3: exit R without saving workspace 4: exit R saving workspace Selection: The system is OSX 4.9 and R-version 2.4.1. Is there something to d0? Atte Tenkanen __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Fractals with R
Hi, That is of counter for web page. Do you get some pop-up windows? Atte kone wrote: Hi everybody, I put some R-code to a web page for drawing fractals. See http://fractalswithr.blogspot.com/ If you have some R-code for fractal images, I'm willing to include them to the page. That's really nice, but what's with the annoying popups, courtesy of Webstats4U? Gad -- Gad Abraham Department of Mathematics and Statistics The University of Melbourne Parkville 3010, Victoria, Australia email: [EMAIL PROTECTED] web: http://www.ms.unimelb.edu.au/~gabraham __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Fractals with R
Hi Jeff, Thank you for the nice page. I also have done some R-videos combining jpeg's in video editor (or with mjpegtools) but this kind of command in R would be useful. Unfortunately I couldn't install NRart-package but got a lot of fun when playing with your Start Making Art!-page. I haven't met pop-ups in my page, but perhaps the counter-server detects my id and leave me alone? Atte kone wrote: Hi everybody, I put some R-code to a web page for drawing fractals. See http://fractalswithr.blogspot.com/ If you have some R-code for fractal images, I'm willing to include them to the page. Has somebody tried L-systems or Markov algorithm (http:// en.wikipedia.org/wiki/Markov_algorithm) with R? Here's an interactive example of how to draw pretty pictures with polynomials and other R functions: http://biostat3.mc.vanderbilt.edu/polyart/ ...without the pop-up windows ;) Cheers, Jeff -- http://biostat.mc.vanderbilt.edu/JeffreyHorner __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Scatterplot3d-proposal
Hi, Does somebody know if there is some trick to betoken the starting points (on the floor) of the vertical lines in scatterplot3d-plots. I'd prefer something like that one in the example here http://users.utu.fi/attenka/proposal3d.jpg If this is not possible, could this option be added? Sometimes it is not easy to distinguish objects behind the others and for instance in this example picture the case nr. 21 is important. Atte Tenkanen University of Turku, Finland __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Scatterplot3d-proposal
Duncan Murdoch wrote: On 1/23/2007 6:38 AM, Atte Tenkanen wrote: Hi, Does somebody know if there is some trick to betoken the starting points (on the floor) of the vertical lines in scatterplot3d-plots. I'd prefer something like that one in the example here http://users.utu.fi/attenka/proposal3d.jpg If this is not possible, could this option be added? Sometimes it is not easy to distinguish objects behind the others and for instance in this example picture the case nr. 21 is important. scatterplot3d() returns a list of functions which could help with this, in particular the points3d function. Right, or rather use the returned function xyz.convert, as in: s3d - scatterplot3d(1:10, 1:10, 1:10, type=h) s3d$xyz.convert(5, 5, 0) Now you got coordinates for the point on the plane with z=0 projected from (5,5,5). It should be easy now to add circles, ellipses or whatever you like. Uwe Ligges Thanks! This seems to work. http://users.utu.fi/attenka/ellipses_in3Db.png I only had to change that (5,5,0) to (5,5,0.4). I'm not sure about the reason, is it because of the minimum of x- or y-coordinate in my plot or what? Atte __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] MDS in 3D
Oh, it was nearer than I have thought! Thanks. Atte I have tried to develop multidimensional scaling for 3D space using PCA without success, yet;-) Is there some application ready in R? Yes. stats has cmdscale. MASS has sammon and isoMDS. All three have a 'k' argument to determine the output dimension. -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] MDS in 3D
Hi, I have tried to develop multidimensional scaling for 3D space using PCA without success, yet;-) Is there some application ready in R? Cheers, Atte __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] MDS in 3D
Hi, I have tried to develop multidimensional scaling for 3D space using PCA without success, yet;-) Is there some application ready in R? Cheers, Atte I found xgobi, but when I try to run example I get some command not found -errors. Atte data(morsecodes) ## from the XGobi/XGvis data, see ?morsecodes mc.row - paste(morsecodes.row[,1],morsecodes.row[,2]) xgvis(dmat = morsecodes.dist, + pos = morsecodes.pos, + rowlab = mc.row, + colors = morsecodes.colors, + glyphs = morsecodes.glyphs, + lines = morsecodes.lines, + linecolors = morsecodes.linecolors) xgvis /tmp/RtmpDaR3cT/xgvis-6058ed8 ## 2) Show lines by hitting l with the mouse over the plot. ## 3) Examine morsecode labels by hitting i and mousing around on the plot. ## 3b) Press r (on the plot) to switch 3D rotation in xgobi. ## 4) Run MDS in 3D by clicking Run MDS (in xgvis). ## 5) Speed up the optimization by increasing the Stepsize with the slider. ## The Stress function value may go as low as 0.1925 (MM). ## 6) When the optimization calms down, click Run MDS to toggle MDS off. ## 7) Rotate the MDS configuration in 3D {by r with mouse over plot}. ## 8) Increase the rotation speed with the slider in the top left and ## control the rotation direction by dragging the mouse on the plot. ## 9) You can check out the initial configuration by ## In order to have no color warning : Mcolors - unique(morsecodes.colors) /bin/sh: line 1: xgvis: command not found (Mcolors - paste(*brushColor, 0:(length(Mcolors)-1),: , Mcolors, sep=)) [1] *brushColor0: SkyBlue *brushColor1: Green [3] *brushColor2: Yellow *brushColor3: HotPink [5] *brushColor4: Red xgobi(morsecodes.pos, collab = morsecodes.col, rowlab = mc.row, + colors = morsecodes.colors, + glyphs = morsecodes.glyphs, + lines = morsecodes.lines, + linecolors = morsecodes.linecolors, + resources= c(*showLines: True, Mcolors)) xgobi -title 'morsecodes.pos' -std mmx /tmp/RtmpDaR3cT/xgobi-mrscd56e509fe /bin/sh: line 1: xgobi: command not found ## This XGobi window will be linked with ## the XGvis window for glyph-color brushing and labeling. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] MDS in 3D
...and my system is OSX 10.4. Atte Hi, I have tried to develop multidimensional scaling for 3D space using PCA without success, yet;-) Is there some application ready in R? Cheers, Atte I found xgobi, but when I try to run example I get some command not found -errors. Atte data(morsecodes) ## from the XGobi/XGvis data, see ?morsecodes mc.row - paste(morsecodes.row[,1],morsecodes.row[,2]) xgvis(dmat = morsecodes.dist, + pos = morsecodes.pos, + rowlab = mc.row, + colors = morsecodes.colors, + glyphs = morsecodes.glyphs, + lines = morsecodes.lines, + linecolors = morsecodes.linecolors) xgvis /tmp/RtmpDaR3cT/xgvis-6058ed8 ## 2) Show lines by hitting l with the mouse over the plot. ## 3) Examine morsecode labels by hitting i and mousing around on the plot. ## 3b) Press r (on the plot) to switch 3D rotation in xgobi. ## 4) Run MDS in 3D by clicking Run MDS (in xgvis). ## 5) Speed up the optimization by increasing the Stepsize with the slider. ## The Stress function value may go as low as 0.1925 (MM). ## 6) When the optimization calms down, click Run MDS to toggle MDS off. ## 7) Rotate the MDS configuration in 3D {by r with mouse over plot}. ## 8) Increase the rotation speed with the slider in the top left and ## control the rotation direction by dragging the mouse on the plot. ## 9) You can check out the initial configuration by ## In order to have no color warning : Mcolors - unique(morsecodes.colors) /bin/sh: line 1: xgvis: command not found (Mcolors - paste(*brushColor, 0:(length(Mcolors)-1),: , Mcolors, sep=)) [1] *brushColor0: SkyBlue *brushColor1: Green [3] *brushColor2: Yellow *brushColor3: HotPink [5] *brushColor4: Red xgobi(morsecodes.pos, collab = morsecodes.col, rowlab = mc.row, + colors = morsecodes.colors, + glyphs = morsecodes.glyphs, + lines = morsecodes.lines, + linecolors = morsecodes.linecolors, + resources= c(*showLines: True, Mcolors)) xgobi -title 'morsecodes.pos' -std mmx /tmp/RtmpDaR3cT/xgobi- mrscd56e509fe /bin/sh: line 1: xgobi: command not found ## This XGobi window will be linked with ## the XGvis window for glyph-color brushing and labeling. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Hershey fonts for musical notation?
Hi, I'd like to know if it is possible to use Hershey vector fonts to create very primitive musical notation. If I can hang some whole notes on these lines X11() plot(0,0, xlim=c(0,10), ylim=c(0,10)) # Staves: for (i in c(seq(from=2,to=2.8,by=0.2),seq(from=4,to=4.8,by=0.2))) { abline(h=i) } it is enough. Best wishes, Atte Tenkanen University of Turku, Finland ___ P.S. By the way, right now the demo(Hershey) seems not to work in OSX version R 2.4.1. ... I get a message i - i + 1 Error in deparse(ei, control = c(showAttributes, useSource)) : invalid multibyte string __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Hershey fonts for musical notation?
Hello Christophe, Thanks a lot! This is what I need. My purpose is to generate chords and I need interactive responses straight in R. I can output midi event lists as csv-files and convert them with a nice midicsv-program in linux- or OSX-console. Then it is easy to convert midi files to notes with Finale or other music notation program. Atte Hi, Atte De : [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] De la part de Atte Tenkanen Envoyé : mercredi 3 janvier 2007 09:17 Hi, I'd like to know if it is possible to use Hershey vector fonts to create very primitive musical notation. [...] There is an example of a music score produced with R and the Hershey fonts in the book 'R Graphics', by Paul Murrell (Chapman Hall/CRC, 2005), page 15. The R Code is on the web page for the book: http://www.stat.auckland.ac.nz/~paul/RGraphics/examples-stevemiller.R BTW, I do a lot of things with R but for music scores I use the ABC language(http://www.walshaw.plus.com/abc/). You can output PostScript from it with Jef Moine's abcm2ps (http://moinejf.free.fr/). Happy new year! Christophe -- Christophe Declercq, MD Observatoire régional de la santé Nord-Pas-de-Calais 235, avenue de la recherche BP 86 F-59373 LOOS CEDEX Phone +33 3 20 15 49 24 Fax + 33 3 20 15 10 46 E-mail [EMAIL PROTECTED] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] The most common row in a matrix?
Hi Gabor, Your version works with the small sample matrix but I didn't get it to work with a larger 675x8 -matrix. I don't know why. I don't know the command aggregate if there is something. I got this one from Peter Alspach which.max(table(paste(data.frame(t(MATRIX)), sep=','))) and it seems to work as well as one posted by Dimitrios Rizopoulos. Atte Try this: a - matrix(1:3, 4, 5) a.ag - aggregate(1:nrow(a), as.data.frame(a), length) a.ag[which.max(a.ag$x), 1:ncol(a)] On 11/19/06, kone [EMAIL PROTECTED] wrote: Hi, How do you get the most common row from a matrix? If I have a matrix like this array(1:3,dim=c(4,5)) [,1] [,2] [,3] [,4] [,5] [1,]12312 [2,]23123 [3,]31231 [4,]12312 in which rows 1 and 4 are similar, I want to find that vector c (1,2,3,1,2). Atte Tenkanen University of Turku, Finland __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Row comparisons to a new matrix?
---BeginMessage--- Thanks Gabor, Your version is handy to use, because you can change the function as you like. However it isn't any faster and if you know some way to make the result matrix more quickly, I'm interested to learn it. My test material (musical improvisations) consists of samples with 2x2 or even bigger result matrices. Atte - Original Message - From: Gabor Grothendieck [EMAIL PROTECTED] Date: Saturday, October 7, 2006 0:28 am Subject: Re: [R] Row comparisons to a new matrix? There is a generalized inner product here: http://tolstoy.newcastle.edu.au/R/help/05/04/3709.html On 10/6/06, Atte Tenkanen [EMAIL PROTECTED] wrote: Hi, Can somebody tell me, which is the fastest way to make comparisons between all rows in a matrix (here A) and put the results to the new symmetric matrix? I have here used cosine distance as an example, but the comparison function can be any other, euclidean dist etc. A=rbind(c(2,3),c(4,5),c(-1,2),c(5,6)) M=matrix(nrow=length(A[,1]),ncol=length(A[,1])) for(i in 1:length(A[,1])) { for(j in 1:length(A[,1])) { M[i,j]=cosine(A[i,],A[j,]) } } Atte Tenkanen University of Turku, Finland __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code. ---End Message--- __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Row comparisons to a new matrix?
Thanks Gabor, Your version is handy to use, because you can change the function as you like. However it isn't any faster and if you know some way to make the result matrix more quickly, I'm interested to learn it. My test material (musical improvisations) consists of samples with 2x2 or even bigger result matrices. Atte - Original Message - From: Gabor Grothendieck [EMAIL PROTECTED] Date: Saturday, October 7, 2006 0:28 am Subject: Re: [R] Row comparisons to a new matrix? There is a generalized inner product here: http://tolstoy.newcastle.edu.au/R/help/05/04/3709.html On 10/6/06, Atte Tenkanen [EMAIL PROTECTED] wrote: Hi, Can somebody tell me, which is the fastest way to make comparisons between all rows in a matrix (here A) and put the results to the new symmetric matrix? I have here used cosine distance as an example, but the comparison function can be any other, euclidean dist etc. A=rbind(c(2,3),c(4,5),c(-1,2),c(5,6)) M=matrix(nrow=length(A[,1]),ncol=length(A[,1])) for(i in 1:length(A[,1])) { for(j in 1:length(A[,1])) { M[i,j]=cosine(A[i,],A[j,]) } } Atte Tenkanen University of Turku, Finland __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Row comparisons to a new matrix?
Hi, Can somebody tell me, which is the fastest way to make comparisons between all rows in a matrix (here A) and put the results to the new symmetric matrix? I have here used cosine distance as an example, but the comparison function can be any other, euclidean dist etc. A=rbind(c(2,3),c(4,5),c(-1,2),c(5,6)) M=matrix(nrow=length(A[,1]),ncol=length(A[,1])) for(i in 1:length(A[,1])) { for(j in 1:length(A[,1])) { M[i,j]=cosine(A[i,],A[j,]) } } Atte Tenkanen University of Turku, Finland __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Row comparisons to a new matrix?
I have to focus my question a little. I'd like to get rid of those for-loops and use, if possible, some faster method for creating the symmetric result matrix. Atte ?dist Bert Gunter Nonclinical Statistics 7-7374 -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Atte Tenkanen Sent: Friday, October 06, 2006 1:54 PM To: r-help@stat.math.ethz.ch Subject: [R] Row comparisons to a new matrix? Hi, Can somebody tell me, which is the fastest way to make comparisons betweenall rows in a matrix (here A) and put the results to the new symmetricmatrix? I have here used cosine distance as an example, but the comparison function can be any other, euclidean dist etc. A=rbind(c(2,3),c(4,5),c(-1,2),c(5,6)) M=matrix(nrow=length(A[,1]),ncol=length(A[,1])) for(i in 1:length(A[,1])) { for(j in 1:length(A[,1])) { M[i,j]=cosine(A[i,],A[j,]) } } Atte Tenkanen University of Turku, Finland __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting- guide.htmland provide commented, minimal, self-contained, reproducible code. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Geometric pattern matching with R?
Hi, Is there some package or function for 2- or 3 -dimensional geometric pattern matching with R? It should be measure similarities between patterns in a fuzzy way, so not exact similarities are demanded. Atte Tenkanen University of Turku, Finland __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Modifying the embed-results
Again my example was't very clear: there were not enough same numbers in the VECTOR. What I need is something like this: VECTOR-c(0,3,6,3,11,2,11,4,3,4,7,7,6,4,8) MATRIX-c() for(i in 1:length(VECTOR)){ v-(unique(VECTOR[i:length(VECTOR)])[1:5]) MATRIX-rbind(MATRIX,v) } MATRIX-na.omit(MATRIX) MATRIX data.frame(MATRIX, row.names=NULL) X1 X2 X3 X4 X5 1 0 3 6 11 2 2 3 6 11 2 4 3 6 3 11 2 4 4 3 11 2 4 7 5 11 2 4 3 7 6 2 11 4 3 7 7 11 4 3 7 6 8 4 3 7 6 8 9 3 4 7 6 8 So, there are no duplicates in rows. VECTOR is always scanned forward as long as the number of items (here 5) becomes full. Atte Try: embed(VECTOR, 5)[,5:1] On 8/25/06, Atte Tenkanen [EMAIL PROTECTED] wrote: Hi, Here is a vector and the result from the embed-command: VECTOR=c(0,3,6,3,11,2,4,3,7,6,4,5,10,2,3,5,8) embed(VECTOR, dimension=5) [,1] [,2] [,3] [,4] [,5] [1,] 113630 [2,]2 11363 [3,]42 1136 [4,]342 113 [5,]7342 11 [6,]67342 [7,]46734 [8,]54673 [9,] 105467 [10,]2 10546 [11,]32 1054 [12,]532 105 [13,]8532 10 Is there a way to little modify the algorithm so that the result looks like this: [1] 0 3 6 11 2 - beginning from the first number of the VECTOR [1] 3 6 11 2 4 - beginning from the second number of the VECTOR etc [1] 6 3 11 2 4 [1] 3 11 2 4 7 [1] 11 2 4 3 7 [1] 2 4 3 7 6 [1] 4 3 7 6 5 [1] 3 7 6 4 5 [1] 7 6 4 5 10 [1] 6 4 5 10 2 [1] 4 5 10 2 3 [1] 5 10 2 3 8 [1] 10 2 3 5 8 Every row consists of next five unique(!) member of the VECTOR. I made this example result with a time consuming algorithm which uses for-loops and whiles. How to do this better?? Thanks in advance! Atte Tenkanen University of Turku __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Modifying the embed-results
Thanks! I see, that do.call-function is used often in R-algorithms... Looking over some extra do.call-examples seems useful. This tail-function is also new for me. Is there some reason to use seq(along = VECTOR) instead of 1:length(VECTOR)? Atte You can replace the for with lapply like this: VECTOR - c(0, 3, 6, 3, 11, 2, 11, 4, 3, 4, 7, 7, 6, 4, 8) f - function(i) unique(tail(VECTOR, length(VECTOR)-i+1))[1:5] out - do.call(rbind, lapply(seq(along = VECTOR), f)) na.omit(rbind(rep(NA, 5), out)) Note that a matrix with zero rows is returned in the case that VECTOR has zero length and in the case that VECTOR has fewer than 5 unique elements. On 8/26/06, Atte Tenkanen [EMAIL PROTECTED] wrote: Again my example was't very clear: there were not enough same numbers in the VECTOR. What I need is something like this: VECTOR-c(0,3,6,3,11,2,11,4,3,4,7,7,6,4,8) MATRIX-c() for(i in 1:length(VECTOR)){ v-(unique(VECTOR[i:length(VECTOR)])[1:5]) MATRIX-rbind(MATRIX,v) } MATRIX-na.omit(MATRIX) MATRIX data.frame(MATRIX, row.names=NULL) X1 X2 X3 X4 X5 1 0 3 6 11 2 2 3 6 11 2 4 3 6 3 11 2 4 4 3 11 2 4 7 5 11 2 4 3 7 6 2 11 4 3 7 7 11 4 3 7 6 8 4 3 7 6 8 9 3 4 7 6 8 So, there are no duplicates in rows. VECTOR is always scanned forward as long as the number of items (here 5) becomes full. Atte Try: embed(VECTOR, 5)[,5:1] On 8/25/06, Atte Tenkanen [EMAIL PROTECTED] wrote: Hi, Here is a vector and the result from the embed-command: VECTOR=c(0,3,6,3,11,2,4,3,7,6,4,5,10,2,3,5,8) embed(VECTOR, dimension=5) [,1] [,2] [,3] [,4] [,5] [1,] 113630 [2,]2 11363 [3,]42 1136 [4,]342 113 [5,]7342 11 [6,]67342 [7,]46734 [8,]54673 [9,] 105467 [10,]2 10546 [11,]32 1054 [12,]532 105 [13,]8532 10 Is there a way to little modify the algorithm so that the result looks like this: [1] 0 3 6 11 2 - beginning from the first number of the VECTOR [1] 3 6 11 2 4 - beginning from the second number of the VECTOR etc [1] 6 3 11 2 4 [1] 3 11 2 4 7 [1] 11 2 4 3 7 [1] 2 4 3 7 6 [1] 4 3 7 6 5 [1] 3 7 6 4 5 [1] 7 6 4 5 10 [1] 6 4 5 10 2 [1] 4 5 10 2 3 [1] 5 10 2 3 8 [1] 10 2 3 5 8 Every row consists of next five unique(!) member of the VECTOR. I made this example result with a time consuming algorithm which uses for-loops and whiles. How to do this better?? Thanks in advance! Atte Tenkanen University of Turku __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R- project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Modifying the embed-results
Hi, Here is a vector and the result from the embed-command: VECTOR=c(0,3,6,3,11,2,4,3,7,6,4,5,10,2,3,5,8) embed(VECTOR, dimension=5) [,1] [,2] [,3] [,4] [,5] [1,] 113630 [2,]2 11363 [3,]42 1136 [4,]342 113 [5,]7342 11 [6,]67342 [7,]46734 [8,]54673 [9,] 105467 [10,]2 10546 [11,]32 1054 [12,]532 105 [13,]8532 10 Is there a way to little modify the algorithm so that the result looks like this: [1] 0 3 6 11 2 - beginning from the first number of the VECTOR [1] 3 6 11 2 4 - beginning from the second number of the VECTOR etc [1] 6 3 11 2 4 [1] 3 11 2 4 7 [1] 11 2 4 3 7 [1] 2 4 3 7 6 [1] 4 3 7 6 5 [1] 3 7 6 4 5 [1] 7 6 4 5 10 [1] 6 4 5 10 2 [1] 4 5 10 2 3 [1] 5 10 2 3 8 [1] 10 2 3 5 8 Every row consists of next five unique(!) member of the VECTOR. I made this example result with a time consuming algorithm which uses for-loops and whiles. How to do this better?? Thanks in advance! Atte Tenkanen University of Turku __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Successive subsets from a vector?
Thanks! I have used tons of for- and while-loops (I'm ashamed to reveal these scripts, but I'm primarily a musician;-) http://users.utu.fi/attenka/SetTheoryScripts.r), taken some or more cup of cocoa and mostly been happy ;-) Now I got so many new ways to do these things, that it takes a while to ruminate all the ideas here. Atte embed(VECTOR, 5)[, 5:1] gives the subsets, so something like apply(embed(VECTOR, 5)[, 5:1], 1, paste, collapse=) does the job. The following is a bit more efficient ind - 1:(length(VECTOR)-4) do.call(paste, c(lapply(0:4, function(j) VECTOR[ind+j]), sep=)) but by looking at how embed() works it could be made as efficient. Larger example: VECTOR - sample(1:10, 1e5, replace=TRUE) system.time(apply(embed(VECTOR, 5)[, 5:1], 1, paste, collapse=)) [1] 5.73 0.05 5.81 NA NA system.time({ind - 1:(length(VECTOR)-4) + do.call(paste, c(lapply(0:4, function(j) VECTOR[ind+j]), sep=)) + }) [1] 1.00 0.01 1.01 NA NA The loop method took 195 secs. Just assigning to an answer of the correct length reduced this to 5 secs. e.g. use ADDRESSES - character(length(VECTOR)-4) Moral: don't grow vectors repeatedly. On Tue, 22 Aug 2006, kone wrote: I'd like to pick every imbricated five character long subsets from a vector. I guess there is some efficient way to do this without loops... Here is a for-loop-version and a model for output: VECTOR=c(1,4,2,6,5,0,11,10,4,3,6,8,6); ADDRESSES=c(); You do not need the semicolons, and they just confuse readers. for(i in 1:(length(VECTOR)-4)){ ADDRESSES[i]=paste(VECTOR[i:(i+4)],collapse=) } ADDRESSES [1] 14265 42650 265011 6501110 5011104 0111043 1110436 104368 [9] 43686 Atte Tenkanen University of Turku, Finland [[alternative text/enriched version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code. -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] A matrix problem
Hi, I have a matrix with two columns. The first column means indexes, the second one contents of those indexes. If I have a MATRIX like this, MATRIX [,1] [,2] [1,]13 [2,]51 [3,]21 [4,]15 I'd like to get as a result vector the sums of these indexes, something like this: c(8,1,0,0,1) How to do this? I did solved it this way, but is there some more elegant way: RESULTVECTOR=c(); RESULTMATRIX=c(); INDEXES=as.integer(names(table(TRANSP_TABLE[,1]))); for(i in INDEXES) { RESULTVECTOR=c(i,sum(MATRIX[,2][MATRIX[,1]==i])) RESULTMATRIX=rbind(RESULTMATRIX,RESULTVECTOR) } row.names(RESULTMATRIX)-INDEXES; RESULTMATRIX=RESULTMATRIX[,2]; RESULTMATRIX 1 2 5 8 1 1 Atte Tenkanen University of Turku, Finland __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] A matrix problem
Thanks for both, tapply seems to be a fast and delicate solution. I had appr. 150 000 rows and it took few seconds to get the result. Till now I have been too tied by for-loops. I will make acquaintance with An Introduction to R. Atte - Original Message - From: Richard M. Heiberger [EMAIL PROTECTED] Date: Saturday, August 19, 2006 11:10 pm Subject: Re: [R] A matrix problem x - cbind(index=c(1,5,2,1), contents=c(3,1,1,5)) x index contents [1,] 13 [2,] 51 [3,] 21 [4,] 15 ## use tapply to get the values you want z0 - tapply(x[,contents], x[,index], sum) ## read ?tapply z0 1 2 5 8 1 1 ## more work is needed to get them into the structure you want r - range(x[,index]) r [1] 1 5 nn - seq(r[1], r[2]) nn [1] 1 2 3 4 5 z - nn*0 z [1] 0 0 0 0 0 names(z) - nn z 1 2 3 4 5 0 0 0 0 0 z[names(z0)] - z0 ## read about subscripting ?[ z 1 2 3 4 5 8 1 0 0 1 ## R is a matrix and vector language. Loops are rarely needed. ## Read An Introduction to R. ## It is clickable from the Help menu in the Windows RGui Console. ## It is available in R-2.3.1/doc/manual/R-intro.pdf on all platforms. This is essentially the same as jim holtman's answer. I did some extra work to get nice names on the result vector. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] How to remove similar successive objects from a vector?
Is there some (much) more efficient way to do this? VECTOR=c(3,2,4,5,5,3,3,5,1,6,6); NEWVECTOR=VECTOR[1]; for(i in 1:(length(VECTOR)-1)) { if((identical(VECTOR[i], VECTOR[i+1]))==FALSE){ NEWVECTOR=c(NEWVECTOR,VECTOR[i+1])} } VECTOR [1] 3 2 4 5 5 3 3 5 1 6 6 NEWVECTOR [1] 3 2 4 5 3 5 1 6 ___ Atte Tenkanen University of Turku, Finland __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to change the margin widths in png-plots?
I found one solution. It may be unofficial but it works. I put the par(mar=c(5.1, 7.1, 4.1, 2.1)) command between png() and plot() -commands. Atte Hello, I have tried to change the margin widths so that mtext (here sd of consecutive pc intervals, look at the picture) and plot(...,xlab=bar) fits to the picture. Here is an example: http://users.utu.fi/attenka/margins.png This doesn't help: par(mar=c(5.1, 7.1, 4.1, 2.1)) And here are the commands: png(filename=/Users/kone/Vaitostutkimus/Pictures/ BachBWV60_sd_cons_pc_int.png, width = 2800, height = 1200,pointsize = 12, bg = white); plot(Compo_SD_succ_int_array_vector,ylim=c(0.40,1.9),col=white,xlab=b ar,ylab=, cex.lab=3, cex.axis=2); mtext(sd of consecutive pc intervals, side=2, line=0, padj=-1.8,at=1.2, cex=3) lines(Compo_SD_succ_int_array_vector,col=1,lty=1,lwd=2); text(2,0.93,labels=*,cex=3) # an asterisk... dev.off(); What do I do next? Atte Tenkanen, Turku Finland __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] Border line width?
Is there some way to change the line widths of plot borders? I couldn't find any parameters for that purpose. Atte Tenkanen University of Turku, Finland __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Border line width?
TNX Brian! Atte - Original Message - From: Prof Brian Ripley [EMAIL PROTECTED] Date: Monday, June 19, 2006 10:33 am Subject: Re: [R] Border line width? On Mon, 19 Jun 2006, Atte Tenkanen wrote: Is there some way to change the line widths of plot borders? I couldn't find any parameters for that purpose. Do you mean the (optional) box? Use bty=n and then box(lwd=). plot(1:10, bty=n) box(lwd=3) works for me. -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] How to change the margin widths in png-plots?
Hello, I have tried to change the margin widths so that mtext (here sd of consecutive pc intervals, look at the picture) and plot(...,xlab=bar) fits to the picture. Here is an example: http://users.utu.fi/attenka/margins.png This doesn't help: par(mar=c(5.1, 7.1, 4.1, 2.1)) And here are the commands: png(filename=/Users/kone/Vaitostutkimus/Pictures/ BachBWV60_sd_cons_pc_int.png, width = 2800, height = 1200,pointsize = 12, bg = white); plot(Compo_SD_succ_int_array_vector,ylim=c(0.40,1.9),col=white,xlab=b ar,ylab=, cex.lab=3, cex.axis=2); mtext(sd of consecutive pc intervals, side=2, line=0, padj=-1.8,at=1.2, cex=3) lines(Compo_SD_succ_int_array_vector,col=1,lty=1,lwd=2); text(2,0.93,labels=*,cex=3) # an asterisk... dev.off(); What do I do next? Atte Tenkanen, Turku Finland __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] How to replace some output candidates in a function?
Hello, I have a function called prime form (here below). It works mostly ok, but some of the output vectors it gives are not those I want. For example, if the result (Pf) in the end is c(0,2,3,7,8) I'd like to replace it with the vector c(0,1,5,7,8) and there are appr. 20 other cases. How to make those corrections in the end of the algorithm before return(Pf)? I know this works... if (identical(Pf,c(0,2,3,7,8))){Pf=c(0,1,5,7,8)} if (identical(Pf,c(0,2,5,6,9))){Pf=c(0,1,4,7,9)} . . . ... but is there some more elegant way? ##*## ## Function primeform (pform) ## ##*## # INPUT:string of pitch class numbers. # USAGE: pform(c(1,4,8,9,0,4)). pform=function(spcn) { if(length(spcn)==1) # if the length of the pcs is 1, # then the prime form is c(0) { Pf=c(0) } else { spcno=sort(unique(spcn));# sort lop duplicates succ_i_arr=c() # create successive interval array succ_i_arr[length(spcno)]=(spcno[1]+12)-spcno[length(spcno)] for(i in 2:length(spcno)) { succ_i_arr[i-1]=spcno[i]-spcno[i-1] } m=matrix(nrow=length(succ_i_arr),ncol=length(succ_i_arr)) for(i in 1:length(succ_i_arr)) # matrix of possible alternatives { m[,i]=succ_i_arr succ_i_arr=c(succ_i_arr[length(succ_i_arr)],succ_i_arr[2:length(succ_i_arr)-1]) } b=0 for(i in 1:length(m[1,])) { b[i]=sum(m[,i]^c(1:length(m[1,]))) } m=as.vector(m[,rev(order(b))[1]]) Pf=c() # the winner set is transposed to prime form Pf[1]=0 for(i in 1:(length(succ_i_arr)-1)) { Pf[1+i]=Pf[i]+m[i] } } return(Pf) } #**# Atte Tenkanen University of Turku, Finland __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] How to divide too long labels?
Is there any possibility to divide too long text in a plot to two or more lines, when using labels-parameter in the text()-command? Here is an example picture: http://users.utu.fi/attenka/253.jpeg My example script is something like this: text(1,0.7,labels=Chordnames[fnid(pcs%%12)]) # according to Larry Solomon's table http://solomonsmusic.net/pcsets.htm Chordnames is a long vector with long character strings. Atte Tenkanen University of Turku Finland __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] How to divide too long labels?
Thank you for answering, both Roger and Henrik There are only 5 cases which need divisions, so I inserted \n to them and now everything works. Atte On 3/18/06, Atte Tenkanen [EMAIL PROTECTED] wrote: Is there any possibility to divide too long text in a plot to two or more lines, when using labels-parameter in the text()-command? Here is an example picture: http://users.utu.fi/attenka/253.jpeg My example script is something like this: text(1,0.7,labels=Chordnames[fnid(pcs%%12)]) # according to Larry Solomon's table http://solomonsmusic.net/pcsets.htm Chordnames is a long vector with long character strings. plot(1, xlab=First row\nSecond row) text(1,1, labels=First row\nSecond row) To insert a '\n' at sensible positions such as between two words, look at regexpr() and substring() and paste(). If you labels have a known pattern you might be able to use gsub(). /Henrik Atte Tenkanen University of Turku Finland __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html -- Henrik Bengtsson Mobile: +46 708 909208 (+1h UTC) __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] How to read more than 1 table?
Question 1) I want to read many csv-tables and run the same commands for all of them. Is there some simpler solution than this below to solve this problem? for (g in 1:6) { if (g==1){k=kt1_0057} if (g==2){k=kt1_0101} if (g==3){k=kt1_0613} if (g==4){k=staten} if (g==5){k=tenpenny} if (g==6){k=fiddrunk} TABLE=read.table(paste(/home/user/,k,.csv,sep=),sep = ,, na.strings=.,header=F,fill=TRUE); print(TABLE) } Question 2) Is it possible to create new variables for example with the assistance of for-loop without initialising them beforehand? __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] How to generate pitch strings
If I calculate a transition probability matrix, first order markov 12x12 or second order 144x144 from musical pitch classes (0-11), is it possible to generate pitch class strings similar as those original strings using those probability matrix with R? If, how? Atte Tenkanen, Turku, Finland I found this kind of solution: j=1; a1=c(); Generated_Melody=c(1); #first note as a seed a1=sample(1:12, size=1, prob=PT_matrix[j,]) # a seed for the loop following for (i in 1:100){ # length of the generated melody will be 100 Generated_Melody=c(Generated_Melody,a1) a1=sample(1:12, size=1, prob=PT_matrix[a1,]) } # Generated_Melody=Generated_Melody-1; # to picht classes # Generated_Melody=Generated_Melody+59; # or to midi pitches Atte Tenkanen PS. Here is my page considering the course in which we use R for music analysis... http://musiikintutkimus.blogspot.com/ __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] How to generate pitch strings
If I calculate a transition probability matrix, first order markov 12x12 or second order 144x144 from musical pitch classes (0-11), is it possible to generate pitch class strings similar as those original strings using those probability matrix with R? If, how? Atte Tenkanen, Turku, Finland __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] How to change the names in tone pitch column
Hi, I have a column (V4) in a midi event list which includes tone pitch names, i.e. A4, E4, C#4, A3...: compo[1:10,] V1 V2 V3 V4 V5 V6 V7 1 1 1 0 A4 96 2 0 2 1 1 0 E4 96 2 0 3 1 1 0 C#4 96 2 0 4 1 1 0 A3 96 2 0 5 1 3 0 B4 96 1 0 6 1 3 0 E4 96 1 0 7 1 3 0 B3 96 1 0 8 1 3 0 G#3 96 1 0 9 1 4 0 C#5 96 1 0 10 1 4 0 D4 96 0 512 Now I'd like to change them to pitch classes (column pc here, which has the values between 0-11, using modulo 12) and absolute midi numbers (column pcAb, values 0-107), so as results A4 gives 9 and 57 (and for example C0 gives 0 and O, C2- 0 and 24 etc): compo[1:10,] V1 V2 V3 V4 V5 V6 V7 pc pcAb 1 1 1 0 A4 96 2 0 9 57 2 1 1 0 E4 96 2 0 4 52 3 1 1 0 C#4 96 2 0 1 49 4 1 1 0 A3 96 2 0 9 45 5 1 3 0 B4 96 1 0 11 59 6 1 3 0 E4 96 1 0 4 52 7 1 3 0 B3 96 1 0 11 47 8 1 3 0 G#3 96 1 0 8 44 9 1 4 0 C#5 96 1 0 1 61 10 1 4 0 D4 96 0 512 2 50 I have done it this way (see next under this comment), but there must be some shorter way, using for-loop and paste-command with sep= or some other way?: ##*# ## Create pitch class vector (add column pc to compo file) from column V4: ##*# pc=c(); pc[V4==C0|V4==C1|V4==C2|V4==C3|V4==C4|V4==C5|V4==C6|V4==C7|V4==C8]=0; pc[V4==C#0|V4==C#1|V4==C#2|V4==C#3|V4==C#4|V4==C#5|V4==C#6|V4==C#7|V4==C#8]=1; pc[V4==D0|V4==D1|V4==D2|V4==D3|V4==D4|V4==D5|V4==D6|V4==D7|V4==D8]=2; pc[V4==D#0|V4==D#1|V4==D#2|V4==D#3|V4==D#4|V4==D#5|V4==D#6|V4==D#7|V4==D#8]=3; pc[V4==E0|V4==E1|V4==E2|V4==E3|V4==E4|V4==E5|V4==E6|V4==E7|V4==E8]=4; pc[V4==F0|V4==F1|V4==F2|V4==F3|V4==F4|V4==F5|V4==F6|V4==F7|V4==F8]=5; pc[V4==F#0|V4==F#1|V4==F#2|V4==F#3|V4==F#4|V4==F#5|V4==F#6|V4==F#7|V4==F#8]=6; pc[V4==G0|V4==G1|V4==G2|V4==G3|V4==G4|V4==G5|V4==G6|V4==G7|V4==G8]=7; pc[V4==G#0|V4==G#1|V4==G#2|V4==G#3|V4==G#4|V4==G#5|V4==G#6|V4==G#7|V4==G#8]=8; pc[V4==A0|V4==A1|V4==A2|V4==A3|V4==A4|V4==A5|V4==A6|V4==A7|V4==A8]=9; pc[V4==A#0|V4==A#1|V4==A#2|V4==A#3|V4==A#4|V4==A#5|V4==A#6|V4==A#7|V4==A#8]=10; pc[V4==B0|V4==B1|V4==B2|V4==B3|V4==B4|V4==B5|V4==B6|V4==B7|V4==B8]=11; ## ... and absolute pitches (0-107): pcAb=c(); pcAb[V4==C0]=0;pcAb[V4==C#0]=1;pcAb[V4==D0]=2;pcAb[V4==D#0]=3;pcAb[V4==E0]=4;pcAb[V4==F0]=5;pcAb[V4==F#0]=6;pcAb[V4==G0]=7;pcAb[V4==G#0]=8;pcAb[V4==A0]=9;pcAb[V4==A#0]=10;pcAb[V4==B0]=11; pcAb[V4==C1]=12;pcAb[V4==C#1]=13;pcAb[V4==D1]=14;pcAb[V4==D#1]=15;pcAb[V4==E1]=16;pcAb[V4==F1]=17;pcAb[V4==F#1]=18;pcAb[V4==G1]=19;pcAb[V4==G#1]=20;pcAb[V4==A1]=21;pcAb[V4==A#1]=22;pcAb[V4==B1]=23; pcAb[V4==C2]=24;pcAb[V4==C#2]=25;pcAb[V4==D2]=26;pcAb[V4==D#2]=27;pcAb[V4==E2]=28;pcAb[V4==F2]=29;pcAb[V4==F#2]=30;pcAb[V4==G2]=31;pcAb[V4==G#2]=32;pcAb[V4==A2]=33;pcAb[V4==A#2]=34;pcAb[V4==B2]=35; pcAb[V4==C3]=36;pcAb[V4==C#3]=37;pcAb[V4==D3]=38;pcAb[V4==D#3]=39;pcAb[V4==E3]=40;pcAb[V4==F3]=41;pcAb[V4==F#3]=42;pcAb[V4==G3]=43;pcAb[V4==G#3]=44;pcAb[V4==A3]=45;pcAb[V4==A#3]=46;pcAb[V4==B3]=47; pcAb[V4==C4]=48;pcAb[V4==C#4]=49;pcAb[V4==D4]=50;pcAb[V4==D#4]=51;pcAb[V4==E4]=52;pcAb[V4==F4]=53;pcAb[V4==F#4]=54;pcAb[V4==G4]=55;pcAb[V4==G#4]=56;pcAb[V4==A4]=57;pcAb[V4==A#4]=58;pcAb[V4==B4]=59; pcAb[V4==C5]=60;pcAb[V4==C#5]=61;pcAb[V4==D5]=62;pcAb[V4==D#5]=63;pcAb[V4==E5]=64;pcAb[V4==F5]=65;pcAb[V4==F#5]=66;pcAb[V4==G5]=67;pcAb[V4==G#5]=68;pcAb[V4==A5]=69;pcAb[V4==A#5]=70;pcAb[V4==B5]=71; pcAb[V4==C6]=72;pcAb[V4==C#6]=73;pcAb[V4==D6]=74;pcAb[V4==D#6]=75;pcAb[V4==E6]=76;pcAb[V4==F6]=77;pcAb[V4==F#6]=78;pcAb[V4==G6]=79;pcAb[V4==G#6]=80;pcAb[V4==A6]=81;pcAb[V4==A#6]=82;pcAb[V4==B6]=83; pcAb[V4==C7]=84;pcAb[V4==C#7]=85;pcAb[V4==D7]=86;pcAb[V4==D#7]=87;pcAb[V4==E7]=88;pcAb[V4==F7]=89;pcAb[V4==F#7]=90;pcAb[V4==G7]=91;pcAb[V4==G#7]=92;pcAb[V4==A7]=93;pcAb[V4==A#7]=94;pcAb[V4==B7]=95; pcAb[V4==C8]=96;pcAb[V4==C#8]=97;pcAb[V4==D8]=98;pcAb[V4==D#8]=99;pcAb[V4==E8]=100;pcAb[V4==F8]=101;pcAb[V4==F#8]=102;pcAb[V4==G8]=103;pcAb[V4==G#8]=104;pcAb[V4==A8]=105;pcAb[V4==A#8]=106;pcAb[V4==B8]=107; #* ## Bind vectors pc and pcAb to original composition array: compo=cbind(compo,pc,pcAb); -Atte __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] How to change the names in tone pitch column
Thanks Phil! This is much more beautiful and elegant. And I learned 2 new R-functions, nchar and substr. -Atte Atte - Here's one way - there certainly are others: notes = 0:11 names(notes) = c(C,C#,D,D#,E,F,F#,G,G#,A,A#,B) pc = notes[substr(V4,1,nchar(V4) - 1)] names(pc) = NULL pcAb = pc + 12 * as.numeric(substr(V4,nchar(V4),nchar(V4))) names(pcAb) = NULL - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley [EMAIL PROTECTED] On Sat, 13 Aug 2005, Atte Tenkanen wrote: Hi, I have a column (V4) in a midi event list which includes tone pitch names, i.e. A4, E4, C#4, A3...: compo[1:10,] V1 V2 V3 V4 V5 V6 V7 1 1 1 0 A4 96 2 0 2 1 1 0 E4 96 2 0 3 1 1 0 C#4 96 2 0 4 1 1 0 A3 96 2 0 5 1 3 0 B4 96 1 0 6 1 3 0 E4 96 1 0 7 1 3 0 B3 96 1 0 8 1 3 0 G#3 96 1 0 9 1 4 0 C#5 96 1 0 10 1 4 0 D4 96 0 512 Now I'd like to change them to pitch classes (column pc here, which has the values between 0-11, using modulo 12) and absolute midi numbers (column pcAb, values 0-107), so as results A4 gives 9 and 57 (and for example C0 gives 0 and O, C2- 0 and 24 etc): compo[1:10,] V1 V2 V3 V4 V5 V6 V7 pc pcAb 1 1 1 0 A4 96 2 0 9 57 2 1 1 0 E4 96 2 0 4 52 3 1 1 0 C#4 96 2 0 1 49 4 1 1 0 A3 96 2 0 9 45 5 1 3 0 B4 96 1 0 11 59 6 1 3 0 E4 96 1 0 4 52 7 1 3 0 B3 96 1 0 11 47 8 1 3 0 G#3 96 1 0 8 44 9 1 4 0 C#5 96 1 0 1 61 10 1 4 0 D4 96 0 512 2 50 I have done it this way (see next under this comment), but there must be some shorter way, using for-loop and paste-command with sep= or some other way?: ##*# ## Create pitch class vector (add column pc to compo file) from column V4: ##*# pc=c(); pc[V4==C0|V4==C1|V4==C2|V4==C3|V4==C4|V4==C5|V4==C6|V4==C7|V4==C8]=0; pc[V4==C#0|V4==C#1|V4==C#2|V4==C#3|V4==C#4|V4==C#5|V4==C#6|V4==C#7|V4==C#8]=1; pc[V4==D0|V4==D1|V4==D2|V4==D3|V4==D4|V4==D5|V4==D6|V4==D7|V4==D8]=2; pc[V4==D#0|V4==D#1|V4==D#2|V4==D#3|V4==D#4|V4==D#5|V4==D#6|V4==D#7|V4==D#8]=3; pc[V4==E0|V4==E1|V4==E2|V4==E3|V4==E4|V4==E5|V4==E6|V4==E7|V4==E8]=4; pc[V4==F0|V4==F1|V4==F2|V4==F3|V4==F4|V4==F5|V4==F6|V4==F7|V4==F8]=5; pc[V4==F#0|V4==F#1|V4==F#2|V4==F#3|V4==F#4|V4==F#5|V4==F#6|V4==F#7|V4==F#8]=6; pc[V4==G0|V4==G1|V4==G2|V4==G3|V4==G4|V4==G5|V4==G6|V4==G7|V4==G8]=7; pc[V4==G#0|V4==G#1|V4==G#2|V4==G#3|V4==G#4|V4==G#5|V4==G#6|V4==G#7|V4==G#8]=8; pc[V4==A0|V4==A1|V4==A2|V4==A3|V4==A4|V4==A5|V4==A6|V4==A7|V4==A8]=9; pc[V4==A#0|V4==A#1|V4==A#2|V4==A#3|V4==A#4|V4==A#5|V4==A#6|V4==A#7|V4==A#8]=10; pc[V4==B0|V4==B1|V4==B2|V4==B3|V4==B4|V4==B5|V4==B6|V4==B7|V4==B8]=11; ## ... and absolute pitches (0-107): pcAb=c(); pcAb[V4==C0]=0;pcAb[V4==C#0]=1;pcAb[V4==D0]=2;pcAb[V4==D#0]=3;pcAb[V4==E0]=4;pcAb[V4==F0]=5;pcAb[V4==F#0]=6;pcAb[V4==G0]=7;pcAb[V4==G#0]=8;pcAb[V4==A0]=9;pcAb[V4==A#0]=10;pcAb[V4==B0]=11; pcAb[V4==C1]=12;pcAb[V4==C#1]=13;pcAb[V4==D1]=14;pcAb[V4==D#1]=15;pcAb[V4==E1]=16;pcAb[V4==F1]=17;pcAb[V4==F#1]=18;pcAb[V4==G1]=19;pcAb[V4==G#1]=20;pcAb[V4==A1]=21;pcAb[V4==A#1]=22;pcAb[V4==B1]=23; pcAb[V4==C2]=24;pcAb[V4==C#2]=25;pcAb[V4==D2]=26;pcAb[V4==D#2]=27;pcAb[V4==E2]=28;pcAb[V4==F2]=29;pcAb[V4==F#2]=30;pcAb[V4==G2]=31;pcAb[V4==G#2]=32;pcAb[V4==A2]=33;pcAb[V4==A#2]=34;pcAb[V4==B2]=35; pcAb[V4==C3]=36;pcAb[V4==C#3]=37;pcAb[V4==D3]=38;pcAb[V4==D#3]=39;pcAb[V4==E3]=40;pcAb[V4==F3]=41;pcAb[V4==F#3]=42;pcAb[V4==G3]=43;pcAb[V4==G#3]=44;pcAb[V4==A3]=45;pcAb[V4==A#3]=46;pcAb[V4==B3]=47; pcAb[V4==C4]=48;pcAb[V4==C#4]=49;pcAb[V4==D4]=50;pcAb[V4==D#4]=51;pcAb[V4==E4]=52;pcAb[V4==F4]=53;pcAb[V4==F#4]=54;pcAb[V4==G4]=55;pcAb[V4==G#4]=56;pcAb[V4==A4]=57;pcAb[V4==A#4]=58;pcAb[V4==B4]=59; pcAb[V4==C5]=60;pcAb[V4==C#5]=61;pcAb[V4==D5]=62;pcAb[V4==D#5]=63;pcAb[V4==E5]=64;pcAb[V4==F5]=65;pcAb[V4==F#5]=66;pcAb[V4==G5]=67;pcAb[V4==G#5]=68;pcAb[V4==A5]=69;pcAb[V4==A#5]=70;pcAb[V4==B5]=71; pcAb[V4==C6]=72;pcAb[V4==C#6]=73;pcAb[V4==D6]=74;pcAb[V4==D#6]=75;pcAb[V4==E6]=76;pcAb[V4==F6]=77;pcAb[V4==F#6]=78;pcAb[V4==G6]=79;pcAb[V4==G#6]=80;pcAb[V4==A6]=81;pcAb[V4==A#6]=82;pcAb[V4==B6]=83; pcAb[V4==C7]=84;pcAb[V4==C#7]=85;pcAb[V4==D7]=86;pcAb[V4==D#7]=87;pcAb[V4==E7]=88;pcAb[V4==F7]=89;pcAb[V4==F#7]=90;pcAb[V4==G7]=91;pcAb[V4==G#7]=92;pcAb[V4==A7]=93;pcAb[V4==A#7]=94;pcAb[V4==B7]=95; pcAb[V4==C8]=96;pcAb[V4==C#8]=97;pcAb[V4==D8]=98;pcAb[V4==D#8]=99;pcAb[V4==E8]=100;pcAb[V4==F8]=101;pcAb[V4==F#8]=102;pcAb[V4==G8]=103;pcAb[V4==G#8]=104;pcAb[V4==A8]=105;pcAb[V4==A#8]=106;pcAb[V4==B8]=107
[R] From soundcard to R?
Is it possible to input data from sound card of the computer to R? What do I need to know about my computer (it's linux pc)? Can I get some real time graphical information this way, spektrum for example? Atte __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html