Re: [R] quickest way convert 1-col df to vector?
D'oh -- thanks! I'm always forgetting the double-bracket extractor... -Original Message- From: Erik Iverson [mailto:er...@ccbr.umn.edu] Sent: Monday, March 08, 2010 10:50 AM To: Steve Jaffe Cc: r-help@r-project.org Subject: Re: [R] quickest way convert 1-col df to vector? sjaffe wrote: anything shorter than as.vector(as.matrix( df ) )? df[[1]] __ 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] tapply for function taking of 1 argument?
Yes, this is clearly the key to working with subsets. Thanks -Original Message- From: Petr PIKAL [mailto:petr.pi...@precheza.cz] Sent: Wednesday, February 03, 2010 4:16 AM To: Steve Jaffe Cc: r-help@r-project.org Subject: Re: [R] tapply for function taking of 1 argument? Hi r-help-boun...@r-project.org napsal dne 02.02.2010 22:16:06: 'fraid not :-(( tapply( data, groups, weighted.mean, weights) tapply(seq(along=lll), rrr, function(i, x, w) weighted.mean(x[i], w[i]), x=lll, w=ttt) If you want to subset more than one thing, subset the index vector. The above help I obtained from Prof.Ripley several years ago so (untested) tapply( seq(along=data), groups, function (i, x, w) weighted.mean(x[i], w[i]), x=data, w=weights) I believe it shall still work. Regards Petr won't work because the *entire* weights vector is passed as the 2nd arg to weighted.means. But weighted.mean needs 'weights' to be split in the same way as 'data' -- the first and 2nd args need to correspond. Jorge Ivan Velez wrote: Hi sjaffem, You were almost there: tapply( yourdata, groups, weighted.mean, weights) See ?tapply for more information. HTH, Jorge -- View this message in context: http://n4.nabble.com/tapply-for-function-taking- of-1-argument-tp1460392p1460419.html Sent from the R help mailing list archive at Nabble.com. __ 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-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] tapply for function taking of 1 argument?
Excellent! I knew there would be a clever answer using 'do.call' :-) -Original Message- From: Charles C. Berry [mailto:cbe...@tajo.ucsd.edu] Sent: Tuesday, February 02, 2010 4:25 PM To: Steve Jaffe Cc: r-help@r-project.org Subject: Re: [R] tapply for function taking of 1 argument? On Tue, 2 Feb 2010, sjaffe wrote: 'fraid not :-(( tapply( data, groups, weighted.mean, weights) won't work because the *entire* weights vector is passed as the 2nd arg to weighted.means. But weighted.mean needs 'weights' to be split in the same way as 'data' -- the first and 2nd args need to correspond. try sapply( split( data.frame(x,w), grp) , do.call, what=weighted.mean ) HTH, Chuck Jorge Ivan Velez wrote: Hi sjaffem, You were almost there: tapply( yourdata, groups, weighted.mean, weights) See ?tapply for more information. HTH, Jorge -- View this message in context: http://n4.nabble.com/tapply-for-function-taking-of-1-argument-tp1460392p1460419.html Sent from the R help mailing list archive at Nabble.com. __ 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. Charles C. Berry(858) 534-2098 Dept of Family/Preventive Medicine E mailto:cbe...@tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901 __ 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] dyn.load search path?
Sorry if this is somewhere in the fine manuals but I've been unable to locate it. Does dyn.load use a search path or does it just look in the current directory for non-fully-qualified filenames? If there is a search path, what is it? Thanks for your help -- View this message in context: http://www.nabble.com/dyn.load-search-path--tp25492214p25492214.html Sent from the R help mailing list archive at Nabble.com. __ 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] Several simple but hard tasks to do with R
For history of both commands and output, consider running R inside emacs using the ESS package and simply saving the buffer to a file. If you save the session as an S transcript file (extension .St) it is also easy to reload and re-execute any part of it. Emacs or xemacs is available on most platforms including Windows. Rakknar wrote: 1. logs. help.search(history) and ?savehistory shows you that R does exactly what you want very easily (depending on the platform, which contrary to the posting guide's request, you did not tell us). I've already find out about the history tool but it was not useful because it only register commands, not output from the commands. The commands are already stored in scripts (I use Tinn-R, I don't know if you would recommend me other) what I want to do it's to store de commands AND the outputs from each. I use the Windows version by the way. -- View this message in context: http://www.nabble.com/Several-simple-but-hard-tasks-to-do-with-R-tp25052563p25064007.html Sent from the R help mailing list archive at Nabble.com. __ 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] Is there a construct for conditional comment?
Why not if ( 0 ) { commented with zero } else { commented with one } Greg Snow-2 wrote: I believe that #if lines for C++ programs is handled by the preprocessor, not the compiler. So if you want the same functionality for R programs, it would make sense to just preprocess the R file. In C++, I can use the following construct to choice either of the two blocks the comment but not both. Depending on whether the number after #if is zero or not, the commented block can be chose. I'm wondering if such thing is possible in R? #if 0 commented with 0 #else commented with 1 #endif Regards, Peng -- View this message in context: http://www.nabble.com/Is-there-a-construct-for-conditional-comment--tp25034224p25064798.html Sent from the R help mailing list archive at Nabble.com. __ 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] why is 0 not an integer?
Why when I assign 0 to an element of an integer vector does the type change to numeric? Here is a particularly perplexing example: v - 0:10 v [1] 0 1 2 3 4 5 6 7 8 9 10 class(v) [1] integer v[1] - 0 class(v) [1] numeric #!! -- View this message in context: http://www.nabble.com/why-is-0-not-an-integer--tp24835423p24835423.html Sent from the R help mailing list archive at Nabble.com. __ 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] array slice notation?
Suppose I have an n-diml array A and I want to extract the first row -- ie all elements A[1, ...] Interactively if I know 'n' I can write A[1,] with (n-1) commas. How do I do the same more generally, eg in a script? (I can think of doing this by converting A to a vector then extracting the approp elements then reshaping it to an array, but I wonder if there isn't a more straightforward approach) Thanks -- View this message in context: http://www.nabble.com/array-slice-notation--tp24814643p24814643.html Sent from the R help mailing list archive at Nabble.com. __ 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] array slice notation?
Yes, I was thinking more in terms of mental operations than physical. I think the following works, but it doesn't seem entirely transparent :-) Given array A, and a vector of row indices v (ie 1 = v = dim(A)[1]), the slice of rows v is A[ outer(v, dim(A)[1]*( 1:prod(dim(A)[-1])-1 ), '+') ] Steve Lianoglou-6 wrote: You actually don't have to convert A to a vector, you can use vector- style indexing into a matrix: -- View this message in context: http://www.nabble.com/array-slice-notation--tp24814643p24815116.html Sent from the R help mailing list archive at Nabble.com. __ 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] array slice notation?
Although you *do* have to re-assign the dimensions, otherwise the result is just a flat vector, ie slice - A[ outer(v, dim(A)[1]*( 1:prod(dim(A)[-1])-1 ), '+') ] dim(slice) - dim(A)[-1] Steve Jaffe wrote: A[ outer(v, dim(A)[1]*( 1:prod(dim(A)[-1])-1 ), '+') ] -- View this message in context: http://www.nabble.com/array-slice-notation--tp24814643p24815435.html Sent from the R help mailing list archive at Nabble.com. __ 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] array slice notation?
correction -- that would work for a single row, if you want the result to be an array with one fewer dimensions. But in general you get an array of the same dimension you started with (where the first dimension may be length 1). So: dim(slice) - c(length(v), dim(arr)[-1]) Although you *do* have to re-assign the dimensions, otherwise the result is just a flat vector, ie -- View this message in context: http://www.nabble.com/array-slice-notation--tp24814643p24816190.html Sent from the R help mailing list archive at Nabble.com. __ 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] array slice notation?
Very nice. Two questions: 1 Do you have any idea of the timing difference, if any, between this and the vector-subscripting method? 2 How do you generalize this to select multiple rows eg with indexes given by a vector 'v'? Søren Højsgaard wrote: You can do A - HairEyeColor do.call([, c(list(A),list(1,T,T))) Sex Eye Male Female Brown 32 36 Blue11 9 Hazel 10 5 Green3 2 Regards Søren -- View this message in context: http://www.nabble.com/array-slice-notation--tp24814643p24819883.html Sent from the R help mailing list archive at Nabble.com. __ 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] write matrix M including names(dimnames(M))
I can do this by writing (and reading) the file according to some format of my own devising, but I'm wondering if there is a built-in way to write and then restore a matrix with not only the dimnames (which write.table/read.table can preserve) but also the names(dimnames)? Example: M - matrix(1:4, 2, 2) dimnames(M) - list(xdim=c(a, b), ydim=c(u, v)) M ydim xdim u v a 1 3 b 2 4 -- View this message in context: http://www.nabble.com/write-matrix-M-including-names%28dimnames%28M%29%29-tp24750637p24750637.html Sent from the R help mailing list archive at Nabble.com. __ 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 aligning barplot and scatter plot (ala 'layout' example)
I'm trying just to do the x-barplot + scatterplot from the layout example. The barplot does not come out aligned horizontally with the scatter plot, although I am passing in the same xlim to both. When I run the example it comes out correctly. I haven't figured out what I'm doing differently, except that my layout is just two regions, one above the other, rather than 4 regions. Any ideas what might be wrong? Or suggestions for a simpler/more robust way to plot a histogram alongside a scatterplot, with x-axes aligned? Another question, is there a simple way to make the bars go down instead of up (so putting the histogram underneath the scatter plot they would essentially share a common x axis?) Thanks -- View this message in context: http://www.nabble.com/problem-aligning-barplot-and-scatter-plot-%28ala-%27layout%27-example%29-tp24432689p24432689.html Sent from the R help mailing list archive at Nabble.com. __ 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] print() to file?
I'd like to write some objects (eg arrays) to a log file. cat() flattens them out. I'd like them formatted as in 'print' but print only writes to stdout. Is there a simple way to achieve this result? Thanks -- View this message in context: http://www.nabble.com/print%28%29-to-file--tp24397445p24397445.html Sent from the R help mailing list archive at Nabble.com. __ 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] vectorizing a function
I'm sure I'm missing something obvious but I'm not seeing how to simply vectorize a function of two or more variables. Say I have f - function(x,y) if (x0) y else -y Now I have vectors x and y of equal length and I'd like to apply f element-wise. I.e. conceptually z - f(x,y) where x, y, z are vectors of the same length Some magic involving apply? Thanks -- View this message in context: http://www.nabble.com/vectorizing-a-function-tp24380064p24380064.html Sent from the R help mailing list archive at Nabble.com. __ 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] vectorizing a function (NEVERMIND)
Nevermind, indeed it is obvious: Vectorize ! Steve Jaffe wrote: I'm sure I'm missing something obvious but I'm not seeing how to simply vectorize a function of two or more variables. -- View this message in context: http://www.nabble.com/vectorizing-a-function-tp24380064p24380136.html Sent from the R help mailing list archive at Nabble.com. __ 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] sorting question
I've asked about custom sorting before and it appears that -- in terms of a user-defined order -- it can only be done either by defining a custom class or using various tricks with order Just wondering if anyone has a clever way to order vintages of the form 2002, 2003H1, 2003H2, 2004, 2005Q1, 2005Q2, etc some have H1 or H2, some have Q1,Q2,Q3,Q4, some are just plain years. They should be sorted in the obvious order. I can think of doing something with s'trsplit' and 'order' but anyone have anything better? (I still wonder why sorting with a user-defined function isn't supported. I guess I should follow the open source philosophy and contribute my own, but it seems that would involve implementing an explicit, iterative sort algorithm, whereas it would make more sense for it to be integrated with the internal sort function, if that were possible) Thanks -- View this message in context: http://www.nabble.com/sorting-question-tp24293430p24293430.html Sent from the R help mailing list archive at Nabble.com. __ 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] X-window graphics -- preventing window coming to front?
When I do dev.new(), the resulting window comes to the front and grabs the focus. Is there a way to prevent this so I can continue working in other windows while the graphics are being produced? Thanks -- View this message in context: http://www.nabble.com/X-window-graphicspreventing-window-coming-to-front--tp24167763p24167763.html Sent from the R help mailing list archive at Nabble.com. __ 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] searching help for partial matches
The situation is that I know there is a function and know approximately what the name is, and want to find the exact name. Is there a way of searching for near-matches (similar to unix apropos). For example, I know there is a function called something like allequal (or allequals or AllEquals or...). But ?allequal, etc, return nothing, only if I remember the name can I get help via ?all.equal. Thanks -- View this message in context: http://www.nabble.com/searching-help-for-partial-matches-tp24078042p24078042.html Sent from the R help mailing list archive at Nabble.com. __ 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] searching help for partial matches
help.search -- excellent I'm a bit confused though -- doc seems to say that ??pattern is same as help.search(pattern) but it doesn't work that way for me...I'd been trying ?? for some time without success. For example, ??allequal #returns No documentation for '?allequal' in specified packages and libraries: you could try '???allequal' It looks like it is treating ?allequal as the input to the ? function... Any idea what I need to get this work correctly? Thanks ?help.search help.search(allequal) help.search(apropos=allequal) ??allequal -- View this message in context: http://www.nabble.com/searching-help-for-partial-matches-tp24078042p24078399.html Sent from the R help mailing list archive at Nabble.com. __ 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] list of data.frames?
I'm trying to build up a list of data.frames by appending one by one. If x,y,z are data.frames, I can do somelist - list(x, y, z) (or even somelist - list(x=x, y=y, z=z) to get names) But if I start with somelist - list(x,y) and now want to go from this to list(x,y,z) I'm stuck. I've tried append(somelist, z) c(somelist, z) list(somelist, z) Each gives a different result but none are what I'm looking for. Any suggestions? Thanks -- View this message in context: http://www.nabble.com/list-of-data.frames--tp24079579p24079579.html Sent from the R help mailing list archive at Nabble.com. __ 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 tell if as.numeric succeeds?
Suppose I have a vector of strings. I'd like to convert this to a vector of numbers if possible. How can I tell if it is possible? as.numeric() will issue a warning if it fails. Do I need to trap this warning? If so, how? In other words, my end goal is a function that takes a vector of strings and returns either a numeric vector or the original vector. Assuming this doesn't already exist, then to write it I'd need a function that returns true if its input can be converted to numeric and false otherwise. Any suggestions will be appreciated. -- View this message in context: http://www.nabble.com/how-to-tell-if-as.numeric-succeeds--tp23874936p23874936.html Sent from the R help mailing list archive at Nabble.com. __ 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] logical indexing multidimensional arrays
Suppose I have an n-dimensional array and a logical vector as long as the first dimension. I can extract an n-dimensional subarray with a[ i, , , , .. ,] where there are n-1 commas (ie empty indices) Is there an alternative notation that would better lend itself to more generic use, e.g. to write a function that takes 'a' and 'i' and returns a[i, , , .. ,]? -- View this message in context: http://www.nabble.com/logical-indexing-multidimensional-arrays-tp23875985p23875985.html Sent from the R help mailing list archive at Nabble.com. __ 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] custom sort?
Sounds simple but haven't been able to find it in docs: is it possible to sort a vector using a user-defined comparison function? Seems it must be, but sort doesn't seem to provide that option, nor does order sfaics -- View this message in context: http://www.nabble.com/custom-sort--tp23770565p23770565.html Sent from the R help mailing list archive at Nabble.com. __ 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] custom sort?
hmm, that is what I was afraid of. I considered that but thought to myself, surely there must be an easier way. I wonder why this feature isn't available. It's there in scripting languages, like perl, but also in hardcore languages like C++ where std::sort and sorted containers allow the user to provide a comparison function (even for builtin types like int). It's hard to believe that you have to jump through more hoops to do a custom sort in R than in C++ ... You put a class on the vector... -- View this message in context: http://www.nabble.com/custom-sort--tp23770565p23770964.html Sent from the R help mailing list archive at Nabble.com. __ 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] Cox Proportional Odds Model
Is there a function, like coxph for the proportional hazard model, for fitting a discrete-time proportional odds model? -- View this message in context: http://www.nabble.com/Cox-Proportional-Odds-Model-tp23373426p23373426.html Sent from the R help mailing list archive at Nabble.com. __ 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.