Re: [R] R equivalent of Python str()?
8-02-2012, 09:45 (+0100); Martin Maechler escriu: R Michael Weylandt michael.weyla...@gmail.com michael.weyla...@gmail.com on Tue, 7 Feb 2012 20:23:57 -0500 writes: Possibly as.character() is what the OP was seeking Michael or format() which is closer for numeric data Thanks for the suggestions, but none of these appears to do what I need. Take a table such as a - as.table(c(A=450,B=12)) a A B 450 12 The string that print() prints is A B \n450 12 \n. Once you have the string you can print it with cat() and get the same result as with print(). cat( A B \n450 12 \n) A B 450 12 The function that I was looking for was one that given the table a in the example would return the string described above. Apparently, capture.output() can be used for that, although it returns the string split into lines they can be joined together easily. So, case closed :) -- Bye, Ernest __ R-help@r-project.org 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 equivalent of Python str()?
8-02-2012, 04:22 (+); William Dunlap escriu: Use capture.output(print(yourData)) to capture would be printed by print as a vector of a strings (one per line of printout). Paste together if desired. This will do it!! Thanks. -- Cheers, Ernest __ R-help@r-project.org 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] Memory allocation problem (again!)
8-02-2012, 22:22 (+0545); Christofer Bogaso escriu: And the Session info is here: sessionInfo() R version 2.14.0 (2011-10-31) Platform: i386-pc-mingw32/i386 (32-bit) Not an expert, but I think that 32-bit applications can only address up to 2GB on Windows. -- Bye, Ernest __ R-help@r-project.org 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] using mclapply (multi core apply) to do matrix multiplication
7-02-2012, 00:29 (-0800); Alaios escriu: Dear all, I am trying to multiply three different matrices and each matrice is of size 16384,16384 the normal %*% multiplciation operator has not finished one day now. As I am running a system with many cores (and it seems that R is using only one of those) I would like to write fast a brief function that converts the typical for loops of a matrix multiplication to a set of lapply sets (mclapply uses the lapply syntax but it applies the work to many cores). If my thinking is correct , in the sense that this will speed up things a lot, I want you to help me covert the first matrix in rows the second in columns and convert those in a format that lapply would like to work with. If I understand correctly, R uses a specialized library called BLAS to do matrix multiplications. I doubt re-implementing the matrix multiplication code at R-level would be any faster. What you can try is replace BLAS with a multicore version of BLAS although it's not easy if you have to compile it yourself. Also, you may try to re-think the problem you're trying to solve. Maybe there's a different approach that is less computation-intensive. -- Cheers, Ernest __ R-help@r-project.org 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] using mclapply (multi core apply) to do matrix multiplication
7-02-2012, 02:31 (-0800); Alaios escriu: I would like to thank you Ernest for your answer. I guess that this is gonna be faster as right now R only sees one core. In my work there is a system with 64 cores and you can see only one working. If I understand it right a [m,n][n,k] matrix multiplication can be split into rows (from first matrice) and columns (from the second matrice) and then combine all the local results of each cpu together. Would that be too weird for mclapply to handle? I never used mclapply, but anyway here's a matrix multiplication function that uses lapply. Because the two lapply's are nested I don't think you can parallelize the two... I would only make the second one work with multiple cores mmult - function(a, b) { a - as.matrix(a) b - as.matrix(b) if (ncol(a) != nrow(b)) stop('non-conforming matrices') out - lapply(1:ncol(b), function(j) lapply(1:nrow(a), function(i) sum(a[i,] * b[,j]))) array(unlist(out), c(nrow(a), ncol(b))) } Also, I'm pretty sure that there are better algorithms. If you do this it would be interesting if you measured the execution time of the different alternatives and post the results :) -- Cheers, Ernest __ R-help@r-project.org 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] using mclapply (multi core apply) to do matrix multiplication
7-02-2012, 03:32 (-0800); Alaios escriu: I wouldl ike to thank you for your response. The hardest part in the installation is to find a BLAS library to install. If I understand it right once I install BLAS then I only need to change a flag in the ./configure of R installation.. Our system is running opensuse and has intel cores. according to the link here http://cran.r-project.org/doc/manuals/R-admin.html#BLAS I ahve to find a proper BLAS library to insta.. In the explanation for the different alternatives seem that most of those are not implemented any more and other require special configuration :( This article includes an overview of different BLAS libraries along with benchmarks: http://cran.r-project.org/web/packages/gcbd/vignettes/gcbd.pdf It looks like using single-threaded ATLAS is already an improvement over LAPACK in most cases. I use Debian and it's straightforward to replace one with the other: you only have to install the libatlas3gf-base package and remove liblapack3gf and libblas3gf. Unfortunately, Debian does not include a multi-threaded version of ATLAS although they provide instructions on how to recompile the package yourself with multi-threading enabled. I don't know about SUSE, sorry. -- Bye, Ernest __ R-help@r-project.org 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 equivalent of Python str()?
Hi, I was wondering if there's a function in R that is meant to return a string representation of an object. Basically, it's like print() but it doesn't print anything, it only returns a string. I know there's a str() function but it's not quite the same. I mean a function that returns the same string that print() would display. -- Bye, Ernest __ R-help@r-project.org 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 enterprise for linux
6-02-2012, 06:29 (-0800); Alaios escriu: Actually, I would like to speed up matrix multiplication which is really too slow. MatrixA %*% MatrixB (each one is a [128,128] table) is running four hours now... and just imagine that I want to calculate many of those. It doesn't seem normal to me... in my computer such a multiplication takes a fraction of a second: system.time(array(rnorm(128*128), c(128,128)) %*% array(rnorm(128*128), c(128,128))) user system elapsed 0.008 0.000 0.006 Am I missing something? Cheers, Ernest __ R-help@r-project.org 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] Reordering levels of a factor when the factor is part of a data frame
6-02-2012, 11:03 (-0800); Judith Flores escriu: The name of the column will be saved in an object called: variab the data frame is called df. If I try to the do following: df[variab]-factor(df[variab], levels=c(A2B,B31,C33)) it won't work because df[variab] is a data frame. I think you need to use df[[variab]] instead. Cheers, Ernest __ R-help@r-project.org 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] replicate rows
3-02-2012, 11:26 (+); Schumacher, G. escriu: I have a matrix of 17 rows and 20 columns. I want to replicate this matrix 20 times, but I only want to replicate the rows. How do I do that? If x is your matrix, this x[rep(1:17, 20),] will give you a matrix with 340 rows and 20 columns which I think is what you want. Cheers, Ernest __ R-help@r-project.org 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] sapply help
3-02-2012, 08:37 (-0800); Filoche escriu: Hi every one. I'm learning how to use sapply (and other function of this family). Here's what I'm trying to do. I have a vector of lets say 5 elements. I also have a matrix of nX5. I would like to know how many element by column are inferior to each element of my vector. On this example: v = c(1:5) M = matrix(3,2,5) I would like to have a vector at the end which give me 0 0 0 2 2 This does that: sapply(1:5, function(i) sum(M[,i] v[i])) [1] 0 0 0 2 2 Basically, it's like a loop where at each iteration the function is called with one element of the vector 1:5 as argument, so what this really does is sum(M[,1] v[1])) sum(M[,2] v[2])) ... and then the results are put all together in a vector. -- Cheers, Ernest __ R-help@r-project.org 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] dimensions dropped on assignment
Hi there, This is a problem I've run into and do not know how to avoid. It happens when I make an assignment using the dimension names as the subscript of the array. The end result is a dimenensionless array (i.e. a vector) which I don't want. See: out - array(0, 5, list(1:5)) dim(out) [1] 5 out[names(out)] - 1 dim(out) NULL I tried to include a 'drop' argument into the index but it doesn't seem to work: out[names(out), drop=FALSE] - 1 Error in out[names(out), drop = FALSE] - 1 : incorrect number of subscripts on matrix A solution would be to change the vector into an array myself, but I would like to hear opinions before I do that, because I would rather just make the assignment and keep the array object unchanged if such thing is possible. Also, using a subscript of integers instead of labels is not an option in this case. Any help appreciated. -- Cheers, Ernest __ R-help@r-project.org 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] dimensions dropped on assignment
Hi, Thanks for your comments, the issue remains unsolved though. I insist the dropping of dimensions only appears to occur in assignments out - array(0, 5, list(1:5)) out 1 2 3 4 5 0 0 0 0 0 dim(out) [1] 5 dim(out[as.character(2:3)]) [1] 2 There is no dropping here, but then out[as.character(2:3)] - NA out 1 2 3 4 5 0 NA NA 0 0 dim(out) NULL Also, this only happens when labels are used as indices, NOT with integer indices: out - array(0, 5, list(1:5)) out[2:3] - NA out 1 2 3 4 5 0 NA NA 0 0 dim(out) [1] 5 I find this very strange. It's not that commas are missing, or the drop parameter is needed (doesn't work, as shown in my previous e-mail). 31-01-2012, 13:23 (-0500); Richard M. Heiberger escriu: tmp - matrix(1:12, 3, 4, dimnames=list(letters[1:3], LETTERS[1:4])) tmp A B C D a 1 4 7 10 b 2 5 8 11 c 3 6 9 12 tmp[a,, drop=FALSE] A B C D a 1 4 7 10 tmp[,A,drop=FALSE] A a 1 b 2 c 3 you need the correct number of commas. For your example dim(out[1, drop=FALSE]) [1] 1 out - array(0, 5, list(1:5)) dim(out) [1] 5 dim(out[1]) NULL dim(out[1, drop=FALSE]) [1] 1 This construct usually isn't needed for assignment out[2] - 12 out 1 2 3 4 5 0 12 0 0 0 dim(out) [1] 5 dim(out[2]) NULL dim(out[2, drop=FALSE]) [1] 1 2012/1/31 Ernest Adrogué nfdi...@gmail.com Hi there, This is a problem I've run into and do not know how to avoid. It happens when I make an assignment using the dimension names as the subscript of the array. The end result is a dimenensionless array (i.e. a vector) which I don't want. See: out - array(0, 5, list(1:5)) dim(out) [1] 5 out[names(out)] - 1 dim(out) NULL I tried to include a 'drop' argument into the index but it doesn't seem to work: out[names(out), drop=FALSE] - 1 Error in out[names(out), drop = FALSE] - 1 : incorrect number of subscripts on matrix A solution would be to change the vector into an array myself, but I would like to hear opinions before I do that, because I would rather just make the assignment and keep the array object unchanged if such thing is possible. Also, using a subscript of integers instead of labels is not an option in this case. Any help appreciated. -- Cheers, Ernest __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.htmlhttp://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org 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 stack a list of arrays
Hello, I have this list of 2-d arrays: $`0` kd [1,] 0.2011962 4.019537 [2,] 0.2020706 5.722719 [3,] 0.2029451 7.959612 $`1` kd [1,] 0.3148325 2.606903 [2,] 0.3160287 3.806665 [3,] 0.3172249 5.419222 $`2` kd [1,] 0.2332536 4.949390 [2,] 0.2342188 7.115258 [3,] 0.2351840 9.955909 which I need to transform into a data frame like this one: kd group [1,] 0.2011962 4.019537 0 [2,] 0.2020706 5.722719 0 [3,] 0.2029451 7.959612 0 [1,] 0.3148325 2.606903 1 [2,] 0.3160287 3.806665 1 [3,] 0.3172249 5.419222 1 [1,] 0.2332536 4.949390 2 [2,] 0.2342188 7.115258 2 [3,] 0.2351840 9.955909 2 Is there any R function that I can use? I know stack() but it only works with vectors. Thank you! Ernest __ R-help@r-project.org 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] building a subscript programatically
Hi, On ocasion, you need to subscript an array that has an arbitrary (ie. not known in advance) number of dimensions. How do you deal with these situations? It appears that it is not possible use a list as an index, for instance this fails: x - array(NA, c(2,2,2)) x[list(TRUE,TRUE,2)] Error in x[list(TRUE, TRUE, 2)] : invalid subscript type 'list' The only way I know is using do.call() but it's rather ugly. There must be a better way!! do.call('[', c(list(x), TRUE, TRUE, 2)) [,1] [,2] [1,] NA NA [2,] NA NA Any idea? Regards, Ernest __ R-help@r-project.org 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] building a subscript programatically
1/11/11 @ 20:22 (-0400), Comcast escriu: Leaving the indices empty should give you what I'm guessing you want/expect. x[,,2]#. TRUE would also work, just not in a list. Exactly, but this only works if x has three dimensions. What I want is x[,,2] if x has three dimensions, x[,,,2] if it has four, and so forth. I cannot hard code [,,2] because I do not know how many dimensions x will have, instead the subscript has to be built on the fly. Ernest __ R-help@r-project.org 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] building a subscript programatically
2/11/11 @ 13:10 (+1300), Rolf Turner escriu: On 02/11/11 11:14, Ernest Adrogué wrote: Hi, On ocasion, you need to subscript an array that has an arbitrary (ie. not known in advance) number of dimensions. How do you deal with these situations? It appears that it is not possible use a list as an index, for instance this fails: x- array(NA, c(2,2,2)) x[list(TRUE,TRUE,2)] Error in x[list(TRUE, TRUE, 2)] : invalid subscript type 'list' The only way I know is using do.call() but it's rather ugly. There must be a better way!! do.call('[', c(list(x), TRUE, TRUE, 2)) [,1] [,2] [1,] NA NA [2,] NA NA Any idea? It's possible that matrix subscripting might help you. E.g.: a - array(1:60,dim=c(3,4,5)) m - matrix(c(1,1,1,2,2,2,3,4,5,1,2,5),byrow=TRUE,ncol=3) a[m] [1] 1 17 60 52 You can build m to have the same number of columns as your array has dimensions. It's not clear to me what result you want in your example. Sorry for not stating my problem in a more clear way. What I want is, given an array of n dimensions, overwrite it by iteratating over its outermost dimension... OK, in the previous example, I would like to do x - array(NA, c(2,2,2)) for (i in 1:2) { x[,,i] - 0 } As you can see, the index I used in the loop only works in the case of three-dimensional arrays, if x was two dimensional I would have had to write for (i in 1:2) { x[,i] - 0 } So, when the dimensions of x are not known in advance, how would you write such a loop? Your solution of using a matrix might work (I haven't been able to check it yet). Cheers, Ernest __ R-help@r-project.org 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] problem deparsing argument
Hi, I don't know much about R's deparsing magic, I simply use the deparse(substitute(arg)) trick to get the names of the variables passed as arguments to the function in order to set labels, etc. The problem is that this doesn't work with nested functions. For example, foo - function(x) print(deparse(substitute(x))) a=1 foo(a) [1] a This is OK. However, bar - function(x) foo(x) bar(a) [1] x this is not what I want. I wanted bar(a) to print a not x. Is it possible to do this? Thanks in advance. -- Ernest __ R-help@r-project.org 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] accumulative grouping of time series
HI there, Consider a data set like this: x - data.frame(a=1:10, b=11:20, t=c(1,1,1,2,2,2,3,3,3,3)) x a b t 1 1 11 1 2 2 12 1 3 3 13 1 4 4 14 2 5 5 15 2 6 6 16 2 7 7 17 3 8 8 18 3 9 9 19 3 10 10 20 3 Here x$t is a vector of integers that represent a moment in time. I would like to calculate a function of a b at each moment (t0), but using the rows corresponding not only to moment t0 but also all moments t t0. For example, if the function was f(a,b) = sum(a - b), the result would be tf 1 -30 # (1-11) + (2-12) + (3-13) 2 -60 3 -100 As far as I know there is no built-in function in R to group rows like this. The naive approach of using a loop is doable but extremely slow even for small data sets. result - NULL for (i in unique(x$t)) { part - x[x$t = i,] result - rbind(result, sum(part$a + part$b)) } So, any suggestions? Note: in this example, it is possible to calculate f() for each subset using by() and then accumulate the results, but with other functions this won't work. Cheers, Ernest __ R-help@r-project.org 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] get caller's name
Hi, Suppose a function that checks an object: stop.if.dims - function(x) { if (! is.null(dim(x))) { stop(cannot handle dimensional data) } } This would be used by other functions that can only work with dimensionless objects. The problem is the error message would need to include the name of the function that called stop.if.dims, so that the user knows which function got an argument that was incorrect. How do I do this? Or maybe there is another way... -- Ernest __ R-help@r-project.org 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] get caller's name
3/02/11 @ 11:25 (-0500), David Winsemius escriu: On Feb 3, 2011, at 10:27 AM, Ernest Adrogué wrote: Hi, Suppose a function that checks an object: stop.if.dims - function(x) { if (! is.null(dim(x))) { stop(cannot handle dimensional data) } } mtx - matrix(c(1,0,0,1), 2) stop.if.dims - function(x) { objname - deparse(substitute(x)) + if (! is.null(dim(x))) { +stop(paste(objname,cannot handle dimensional data) ) + } + } stop.if.dims(mtx) Error in stop.if.dims(mtx) : mtx cannot handle dimensional data Here, mtx is the name of the argument that stop.if.dims gets. What I want is name of the function that calls stop.if.dims. That being said, I was also interested in getting the names of arguments, so thanks :) Cheers. -- Ernest __ R-help@r-project.org 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] get caller's name
3/02/11 @ 14:30 (-0200), Henrique Dallazuanna escriu: Try this: g - function(.x) tryCatch(stop.if.dims(.x), error=function(e)sprintf(Error in %s: %s, deparse(sys.call(1)), e$message)) g(rbind(2, 3)) This is it. Thanks! Cheers. -- Ernest __ R-help@r-project.org 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] sapply puzzlement
Hi, I have this data.frame with two variables in it, z V1 V2 1 10 8 2 NA 18 3 9 7 4 3 NA 5 NA 10 6 11 12 7 13 9 8 12 11 and a vector of means, means - apply(z, 2, function (col) mean(na.omit(col))) means V1V2 9.67 10.714286 My intention was substracting means from z, so instictively I tried z-means V1 V2 1 0.333 -1.667 2 NA 7.2857143 3 -0.667 -2.667 4 -7.7142857 NA 5 NA 0.333 6 0.2857143 1.2857143 7 3.333 -0.667 8 1.2857143 0.2857143 But this is completely wrong. sapply() gives the same result: sapply(z, function(row) row - means) V1 V2 [1,] 0.333 -1.667 [2,] NA 7.2857143 [3,] -0.667 -2.667 [4,] -7.7142857 NA [5,] NA 0.333 [6,] 0.2857143 1.2857143 [7,] 3.333 -0.667 [8,] 1.2857143 0.2857143 So, what is going on here? The following appears to work z-matrix(means,ncol=2)[rep(1, dim(z)[1]),] V1 V2 1 0.333 -2.7142857 2 NA 7.2857143 3 -0.667 -3.7142857 4 -6.667 NA 5 NA -0.7142857 6 1.333 1.2857143 7 3.333 -1.7142857 8 2.333 0.2857143 but I think it's rather cumbersome, surely there must be a cleaner way to do it. -- Ernest __ R-help@r-project.org 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] unexpected results involving the skellam distribution
Hi all, I am new to R. I made this script that plots the deviation of a skellam-distributed random variable with respect to the skellam distribution. I would expect to get random errors, but the plot sistematically shows a non-random pattern (first a peak and then a low). I don't know how to explain this. Can somebody enlighten me?? require(skellam) n - 5000 lam1 - 5.45 lam2 - 2.78 p1 - rpois(n, lam1) p2 - rpois(n, lam2) rho - cor(p1,p2) z - hist(p1-p2, plot=FALSE) mu1 - lam1 - (rho*sqrt(lam1*lam2)) mu2 - lam2 - (rho*sqrt(lam1*lam2)) x - z$breaks[1:length(z$breaks)-1] y - dskellam(x, mu1, mu2) plot(x,z$density-y) -- Cheers. Ernest __ R-help@r-project.org 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.