Re: [R] negative value for AIC and BIC
Sure -2*log(x) can be negative, and it can outweigh the k*npar term. Just do: curve(-2*log(x)+2, 0.1, 10) # for AIC with npar = 1 abline(h=0, v=exp(1), lty=3) However, that only happens for x exp(1) or even bigger if npar 1. I think Olivier's real question is: do we believe in likelihoods 1 ? Cheers, Mike. Hannu Kahra wrote: Olivier, type ?AIC and have a look at the description Description: Generic function calculating the Akaike information criterion for one or several fitted model objects for which a log-likelihood value can be obtained, according to the formula -2*log-likelihood + k*npar, where npar represents the number of parameters in the fitted model, and k = 2 for the usual AIC, or k = log(n) (n the number of observations) for the so-called BIC or SBC (Schwarz's Bayesian criterion). AIC = -2*log-likelihood + k*npar can be negative as SBC, too. Hannu On 9/7/07, Olivier MARTIN [EMAIL PROTECTED] wrote: Hi all, I obtained negative values for AIC and BIC criteria for a particular model that I have developped... I don't remember to have negative values for these crietria for others applications, so I am a little suprised... Could anyone tell me if something is wrong or his conclusion concerning my model? Best regards, Olivier. __ 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. [[alternative HTML 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. -- View this message in context: http://www.nabble.com/negative-value-for-AIC-and-BIC-tf4400285.html#a12565296 Sent from the R help mailing list archive at Nabble.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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] Does anyone.... worth a warning?!? No warning at all
It's always seemed to me that 'mean' behaved as expected, and 'max' et al were peculiar. If you passed 2 or more vectors to a function would you really expect it to concatenate them before doing it's proper job? I'd rather expect it to behave like 'pmax' and compare them element by element. Maybe 'max' should generate a warning. Cheers, Mike Tom Willems-2 wrote: dear Mathew mean is a Generic function mean(x...) in wich x is a data object, like a data frame a list a numeric vector... so in your example it only reads the first character and then reports it. try x = c(1,1,2) mean(x) kind regards, Tom Disclaimer: click here [[alternative HTML 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. -- View this message in context: http://www.nabble.com/Re%3A-Does-anyone-worth-a-warning-%21--No-warning-at-all-tf4297988.html#a12247316 Sent from the R help mailing list archive at Nabble.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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] Values in horizontal versus vertical position on 'y' axe
? par# then scroll down to look at 'las' You probably want par(las=1) HTH, Mike. akki wrote: hi, When I do a graph. the values on y axe are vertical position. How can I put the values in horizontal position? thanks [[alternative HTML 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. -- View this message in context: http://www.nabble.com/Values-in-horizontal-versus-vertical-position-on-%27y%27-axe-tf4260726.html#a12124985 Sent from the R help mailing list archive at Nabble.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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] Error in using nlevels in apply function
apply(mydata,2,nelem,... passes the columns of mydata _one_by_one_ to nelem, so nelem only sees a vector, and complains if it's expecting a 2d object. Try this: nelem-function(x) nlevels(factor(x)) apply(mydata[,mycol.index],2,nelem) HTH, Mike. Sébastien-18 wrote: Dear R users, I am currently trying to create my first personnal function and use it with the apply function. The purpose of this function is to create a vector summarizing the number of levels in a given selection of data.frame columns. I tried to transpose the indexation method used by the nlevels function but it doesn't seem to work. I did not find anything uesful in the archives so could someone point to me where my mistake(s) is (are) ? Thanks in advance Sebastien #- mydata-data.frame(1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6,1:6) mycol.index-c(1,5,3) nelem-function(x,col.id) nlevels(factor(x[,col.id])) my.nlevels.col-apply(mydata,2,nelem,mycol.index) my.nlevels.col # #The error message is the following #Error in x[, col.id] : incorrect number of dimensions __ 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. -- View this message in context: http://www.nabble.com/Error-in-using-nlevels-in-apply-function-tf4225090.html#a12019950 Sent from the R help mailing list archive at Nabble.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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] Error when using the cat function
Your problem is with ifelse, not with cat. First clue is that ifelse(TRUE,{print(yes)},{print(no)}) # results in yes being printed TWICE. Try this: tmp - ifelse(TRUE,{print(yes)},{print(no)}) # one yes tmp # another yes Try: print(print(yes)) # prints yes and returns yes invisibly. This returned value is passed on to/by ifelse. Now try: print(cat(yes\n)) # yes appears, but cat(yes) returns NULL, which ifelse can't handle: ifelse(TRUE, NULL, whatever) # Gives the error you saw. What you need is if { } else { } : if(!inherits(dat[[n]], factor)) {cat(yes\n)} else {cat(no\n)} HTH, Mike. Stan Hopkins wrote: Is the following developed in my console output a recognized bug or am I using the cat function incorrectly? Thanks, Stan ifelse(class(data[[n]])!=factor,{print(yes)},{print(no)}) [1] yes [1] yes ifelse(class(data[[n]])!=factor,{cat(yes)},{cat(no)}) yesError in ans[test !nas] - rep(yes, length.out = length(ans))[test : incompatible types (from NULL to logical) in subassignment type fix cat(yes) yes class(data[[n]])!=factor [1] TRUE class(data[[n]]) [1] numeric n [1] 28 length(data[[n]]) [1] 955 class(data)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 2.5 1.5 1.7 2.5 3.0 2.6 1.8 2.5 [716] 0.9 3.1 1.5 2.1 2.5 0.6 1.9 1.7 3.7 7.4 2.4 3.3 3.2 [729] 1.2 1.3 2.0 1.4 3.4 1.7 3.5 1.7 2.0 1.3 0.8 3.0 1.9 [742] 1.9 20.6 3.8 3.8 1.2 1.5 3.2 6.1 5.8 6.6 4.0 5.7 4.0 [755] 3.0 4.7 6.8 6.9 4.1 1.9 4.5 3.8 2.7 2.3 2.5 2.3 2.6 [768] 3.8 1.8 2.4 1.8 1.9 6.1 5.1 4.0 3.8 2.8 3.4 3.1 2.3 [781] 7.5 3.0 3.0 3.1 2.4 6.0 2.3 5.0 2.8 2.7 2.2 5.0 5.0 [794] 3.0 9.0 7.0 7.0 7.0 9.0 8.0 9.0 2.0 4.0 4.0 3.0 3.0 [807] 2.0 2.0 3.0 4.0 3.0 3.0 7.9 11.0 16.0 4.0 7.7 5.0 6.6 [820] 16.0 9.0 19.0 4.0 4.0 7.5 6.6 22.0
Re: [R] maths characters in labels ylab padding
Help on this is in ?plotmath You can use ylab = expression(Y Label(m^2)) Use par(mar..) to increase the margin width, and then mtext to place y axis label: y - 1:10 x - rnorm(10,5,2000) par(mar = c(5,6,4,2) + 0.1) plot(x ~ y, ylab = , las = 1, type = n, bty = n ) mtext(expression(Y Label(m^2)), side = 2, line = 4) If you use this within a function, you'll probably want to reset the margins on exit: old.mar - par(mar = c(5,6,4,2) + 0.1) ; on.exit(par(old.mar)) HTH, Mike. Murray Pung wrote: I have checked out the help files, but cannot find details on how to use maths characters in ylab. Instead of m^2, I would like the 2 in superscript, if possible. I would also like to place more padding on the label so that the label is not obscured by the horizontal numbers. y - 1:10 x - rnorm(10,5,2000) plot(x ~ y, ylab = 'Y Label (m^2)', las = 1, type = n, bty = n ) Thanks Murray -- Murray Pung Statistician, Datapharm Australia Pty Ltd 0404 273 283 [[alternative HTML 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. -- View this message in context: http://www.nabble.com/maths-characters-in-labels---ylab-padding-tf4127592.html#a11738746 Sent from the R help mailing list archive at Nabble.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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] binned column in a data.frame
Try something like this: x - c(1,2,6,8,13,0,5,10, runif(10) * 100) tmp - x %/% 5 + 1 bin.names - paste((0:19) * 5, (1:20) * 5, sep=-) data.frame(Start = x, Binned_Start = bin.names[tmp]) This assumes you want (eg.) 5 in the 5-10 bin, not the 0-5 bin. You may also want to make Binned_Start into a factor rather than a character vector. HTH, Mike. João Fadista wrote: Dear all, I would like to know how can I create a binned column in a data.frame. The output that I would like is something like this: Start Binned_Start 10-5 20-5 65-10 85-10 13 10-15 ... Best regards João Fadista Ph.d. student UNIVERSITY OF AARHUS Faculty of Agricultural Sciences Dept. of Genetics and Biotechnology Blichers Allé 20, P.O. BOX 50 DK-8830 Tjele Phone: +45 8999 1900 Direct:+45 8999 1900 E-mail:[EMAIL PROTECTED] mailto:[EMAIL PROTECTED] Web: www.agrsci.org http://www.agrsci.org/ News and news media http://www.agrsci.org/navigation/nyheder_og_presse . This email may contain information that is confidential. Any use or publication of this email without written permission from Faculty of Agricultural Sciences is not allowed. If you are not the intended recipient, please notify Faculty of Agricultural Sciences immediately and delete this email. [[alternative HTML 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. -- View this message in context: http://www.nabble.com/binned-column-in-a-data.frame-tf4115787.html#a11705240 Sent from the R help mailing list archive at Nabble.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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] HELP FOR BUGS
You might find the 'arm' package useful. For a good introduction to heirarchical modeling, using 'arm' and also WinBUGS and R2WinBUGS, read Gelman, A; J Hill 2007. Data analysis using regression and multilevel/hierarchical models. Cambridge University Press. Cheers, Mike. Ali raza-4 wrote: Hi Sir I am very new user of R for the research project on multilevel logistic regression. There is confusion about bugs() function in R and BUGS software. Is there any relation between these two? Is there any comprehensive package for Multilevel Logistic modelling in R? Please guide in this regard. Thank You RAZA - Boardwalk for $500? In 2007? Ha! [[alternative HTML 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. -- View this message in context: http://www.nabble.com/HELP-FOR-BUGS-tf4078749.html#a11605645 Sent from the R help mailing list archive at Nabble.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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] Me again, about the horrible documentation of tcltk
I think it would help if the tcl/tk manuals were added to the RGui Help menu. Why google when they are on your hard drive already? Cheers, Mike Mike Prager wrote: Alberto Monteiro [EMAIL PROTECTED] wrote: How on Earth can I know what are the arguments of any of the functions of the tcl/tk package? [...] My impression is that you as supposed to look in tck/tk manuals. For example, Googling on tk tck getopenfile pointed to this Web page: http://www.tcl.tk/man/tcl8.5/TkCmd/getOpenFile.htm Hope that helps. -- Mike Prager, NOAA, Beaufort, NC * Opinions expressed are personal and not represented otherwise. * Any use of tradenames does not constitute a NOAA endorsement. __ 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. -- View this message in context: http://www.nabble.com/Me-again%2C-about-the-horrible-documentation-of-tcltk-tf4032019.html#a11460111 Sent from the R help mailing list archive at Nabble.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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] using self-written functions
I use something like Gavin's solution for functions which are not used too often. The problem with using 'source' is that the user environment gets cluttered. I 'save' the most useful functions to a single file (MMmisc.Rda), put it in the HOMEPATH directory (use Sys.getenv(HOMEPATH) to find where that is), then I put the line attach(what=paste(Sys.getenv(HOMEPATH), MMmisc.Rda, sep=/)) in Rprofile.site in C:\Program Files\R\R-2.5.0\etc folder. Works for me! You don't have the luxury of help files, etc, but it's easy to update. HTH -- Mike. Gavin Simpson wrote: On Thu, 2007-06-28 at 17:29 +0800, R. Leenders wrote: ... How can I access my own functions in R without having to copy-paste them everytime and run them manually so I can call them later? Do I need to learn how to write a package and attach the package to make the functions available at all times? Is there another way? Building a package is one way, and not that difficult once you've read the Writing R Extensions manual. An alternative is to have a directory where you keep R function scripts. Put your functions in here in text files with say a .R extension. Then in R you can source one or more of these R scripts as required, using the source() function. Say you have a directory, myScripts at the base of file system (/home/user say on Linux or C:\ on Windows). in this directory there is a file called my_r_function.R. To use this script/function in an R session, you would issue: ## replace /home/user/ with what ever is the correct path for your ## system source(/home/user/myScripts/my_r_function.R) Which would make available to your current session any functions defined in my_r_function.R. Read ?source for more information. HTH G thanks, James [[alternative HTML 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. -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% __ 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. -- View this message in context: http://www.nabble.com/using-self-written-functions-tf3993814.html#a11355842 Sent from the R help mailing list archive at Nabble.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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] warning in a loop
You can investigate what's gone wrong after the loop has failed by looking at the values of i, k, p, and t. Although d[(d[,(i+1)]%in%1),1] produces a vector, k has only one element. Same with p. Should then be obvious why the t.test produces an error. The problem is with the [i] index for k and p; take those away and it works. If you want to keep the values generated in the loop, make k and p lists and index with k[[i]] and p[[i]]. A better way to do this would be to use 'sample' to randomize the measured values in d[,1] and then use d[,2] to group them for testing. You can then use hundreds of iterations: t - rep(NA, 999) for(i in 1:999) { samp - sample(d[,1]) t - t.test(samp[d[,2]==1], samp[d[,2]==2])$p.value } sum(t 0.05) # How many were 'significant'? Note that I prefer to use t - rep(NA,...) to allocate space, rather than 1:999, so that NA appears as the result if there's a problem. Why not just do a randomization test? t - rep(NA, 1000) t[1] - mean(d[d[,2]==1,1]) - mean(d[d[,2]==2,1]) # This is the observed difference in means for(i in 2:1000) { samp - sample(d[,1]) t[i] - mean(samp[d[,2]==1]) - mean(samp[d[,2]==2]) } t - abs(t) # Skip this line if you want a 1-sided test sum(t = t[1])/1000 # This is the 'p-value' HTH, Mike. Tavpritesh Sethi wrote: hi all, I have a matrix with first column having some measurable values, these are indexed by the numerals 1,2 and 3 in the other columns of the data and may be interpreted as values for categories 1,2 and 3. I have written the following loop t-1:10 for(i in 1:10) + { + k[i]-d[(d[,(i+1)]%in%1),1] + p[i]-d[(d[,(i+1)]%in%2),1] + t[i]-t.test(k[i],p[i])$p.value + } Error in t.test.default(k[i], p[i]) : not enough 'x' observations In addition: Warning messages: 1: number of items to replace is not a multiple of replacement length 2: number of items to replace is not a multiple of replacement length As you might have understood, I want to test for difference between the two cagories: k and v. the second column of the data is the original categorization and the rest columns(3:10) are a matrix of randomized values between 1 to 3. (I have three categories) My purpose of doing so is to check whether significant difference comes up in the randomized data also. This is to check the effect of the small sample size of my data. Please suggest a way or an alternative to the above approach. [[alternative HTML 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. -- View this message in context: http://www.nabble.com/warning-in-a-loop-tf3969850.html#a11273379 Sent from the R help mailing list archive at Nabble.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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] how to ave this?
The simplest solution in this case would be: (x[[1]] + x[[2]])/2 But that approach would get messy with 2 matrices in your list. Maybe change your list to an array, then use 'apply': n - length(x) y - array(unlist(x), c(3,2,n)) apply(y, 1:2, mean) HTH, Mike Weiwei Shi wrote: one of my approaches is: x0 = sapply(mylist, cbind) and manipulate from x0 (x0[1:nrow(x0)/2, ] correponds to fc and the lower part is tt. but it is not neat way. On 6/22/07, Weiwei Shi [EMAIL PROTECTED] wrote: Hi, I have a list that looks like this: [[1]] fc tt 50 0.07526882 0.0 100 0.09289617 0.0 150 0.12359551 0.0 [[2]] fc tt 50 0.02040816 0.0 100 0.03626943 0.005025126 150 0.05263158 0.010101010 and I am wondering how to average it so that I have one matrix t0 at the end, and t0[1,1] = (0.075..+0.0204..)/2 Thanks, -- Weiwei Shi, Ph.D Research Scientist GeneGO, Inc. Did you always know? No, I did not. But I believed... ---Matrix III -- Weiwei Shi, Ph.D Research Scientist GeneGO, Inc. Did you always know? No, I did not. But I believed... ---Matrix III __ 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. -- View this message in context: http://www.nabble.com/how-to-ave-this--tf3965210.html#a11264004 Sent from the R help mailing list archive at Nabble.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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] Data consistency checks in functions
Take a look at Help Manuals (in PDF) An Introduction to R, section 10.3. R will recognise the 2nd argument as 'values' iff you define your function as: myfun - function(theta, values, X) You can use if(missing(values)) { values - some.expression(X) } to deal with cases where the user only supplies 1 argument. Where does 'X' come from? If it's a predefined default (maybe something like 'cbind(1:4, 1, 0)' ), better to define it within your function. If it's some object 'X' lurking in the user environment, it could be changed by the user, so you don't know what it might be. A way around this is to require the user to provide either a vector or a matrix as the second argument, then sort them out with: if(is.matrix(values)) { values - same.expression.as.before(values) } HTH, Mike. Anup Nandialath wrote: Dear friends, I'm writing a function with three arguments myfun - function(theta, X, values) { } in this function, I'm trying to write consistency checks. In order to compute the statistic of interest I only need theta and values. The idea of having X in there is that, if values is not provided by the user, then values is computed from X. my problem is I'm trying to write consistency checks. For instance if i say output - myfun(beta, val1), how do I ensure that R reads this as passing arguments to theta and values. In other words is it possible to bypass X completely if values is provided. Also how is it possible for R to recognize the second argument as being values and not X. This is important because X is a matrix and values is a vector. Therefore any checks using the dimensions of either one will land in trouble if it does not correctly capture that. Thanks in advance Sincerely Anup - [[alternative HTML 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. -- View this message in context: http://www.nabble.com/Data-consistency-checks-in-functions-tf3962728.html#a11264566 Sent from the R help mailing list archive at Nabble.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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] merge
Looking at the data, maybe what you need is an array: array(c(A, B), c(5,6,2), dimnames=list(rownames(A), colnames(A), c(obs,pred))) This allows you to keep the names and 'supernames'. This will work if A and B are matrices, not data frames, so you may have to use 'as.matrix' first. HTH, Mike. elyakhlifi mustapha wrote: Hello, ok I know how to do to merge matrix or data.frame by row.names but my true objectif is to merge for example this data.frame: A obs Rép1 Rép2 Rép3 Rép4 Rép5 Rép6 Var1 145 145 150 145 140 145 Var2 150 150 160 155 155 150 Var3 155 155 160 150 150 140 Var4 150 145 145 145 140 145 Var5 135 130 145 135 135 130 and B pred Rép1 Rép2 Rép3 Rép4 Rép5 Rép6 Var1 146.00 144.00 151.00 145.00 143.00 141.00 Var2 154.33 152.33 159.33 153.33 151.33 149.33 Var3 152.67 150.67 157.67 151.67 149.67 147.67 Var4 146.00 144.00 151.00 145.00 143.00 141.00 Var5 136.00 134.00 141.00 135.00 133.00 131.00 and the main difficulty is to keep the names and the supernames. -- View this message in context: http://www.nabble.com/merge-tf3953336.html#a11247082 Sent from the R help mailing list archive at Nabble.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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] Getting names of objects passed with ...
Thanks, Henrik, but 'foo' doesn't do what I want: x - some stuff second - more stuff foo(first=x, second) [1] first Brian's right: ...he wants the argument name if there is one otherwise the deparsed argument value, but clarification would be helpful. The function using this compares estimates of animal densities, CIs, etc using different models, with one object containing the results of one model. It extracts key results and AIC from these objects and does a summary matrix, with lowest AIC at the top, so the row names need to reflect the model used. If the object name is sufficiently explanatory -- eg. point.est.hazardRate -- then the deparsed argument value is fine as row name. But we need the option to be more specific if necessary, eg. with halfNormal=x1, hazardRate=x2. Just like 'rbind', in fact. Thanks, Mike -- View this message in context: http://www.nabble.com/Getting-names-of-objects-passed-with-%22...%22-tf3850318.html#a10910245 Sent from the R help mailing list archive at Nabble.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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] Getting names of objects passed with ...
Thanks very much to all of you. It looks like 'match.call' is the key, and both Brian's and Gabor's solutions work fine. --- Mike. -- View this message in context: http://www.nabble.com/Getting-names-of-objects-passed-with-%22...%22-tf3850318.html#a10913978 Sent from the R help mailing list archive at Nabble.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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] Getting names of objects passed with ...
Sorry, I responded a bit too hastily last night, without testing the two functions properly. Brian's is shorter but I think the one in my post is a bit more robust: Indeed! 'f1' only works if at least one of the arguments is named. Otherwise 'nm' is NULL and 'nchar(nm[i])' fails. 'f2' seems to have all combinations covered. Thanks again!! f1 - function(...) { +m - as.list(match.call(expand.dots=TRUE))[-1] +nm - names(m) +for(i in seq_along(m)) if(!nchar(nm[i])) nm[i] - deparse(m[[i]]) +nm + } f2 - function(...) { + x - list(...) + if (is.null(names(x))) names(x) - + names(x)[names(x) == ] - NA + mc - match.call()[-1] + ifelse(is.na(names(x)), as.character(mc), names(x)) + } f1(sin, cos) Error in if (!nchar(nm[i])) nm[i] - deparse(m[[i]]) : argument is of length zero f2(sin, cos) [1] sin cos __ 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. -- View this message in context: http://www.nabble.com/Getting-names-of-objects-passed-with-%22...%22-tf3850318.html#a10922269 Sent from the R help mailing list archive at Nabble.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 and provide commented, minimal, self-contained, reproducible code.
[R] Getting names of objects passed with ...
Is there a tidy way to get the names of objects passed to a function via the ... argument? rbind/cbind does what I want: test.func1 - function(...) { nms - rownames(rbind(..., deparse.level=1)) print(nms) } x - some stuff second - more stuff test.func1(first=x, second) [1] first second The usual 'deparse(substitute())' doesn't do it: test.func2 - function(...) { nms - deparse(substitute(...)) print(nms) } test.func2(first=x, second) [1] x I'm using nms - rownames(rbind(...)) as a workaround, which works, but there must be a neater way! rbind/cbind are .Internal, so I can't pinch code from there. Thanks, Mike. -- View this message in context: http://www.nabble.com/Getting-names-of-objects-passed-with-%22...%22-tf3850318.html#a10906614 Sent from the R help mailing list archive at Nabble.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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] tcltk crashing R after the (ab)use of tkwait
library(tcltk) tt - tktoplevel() done - tclVar(0) but - tkbutton(tt, text=OK, command=function() tclvalue(done) - 1) tkpack(but) tkwait.variable(done) works as fine as long as I click the OK. However, if I close the window (by clicking in the X), R enters into an infinite loop and there's no way of returning except by closing the R window. I have the same pbm with R 1.5.0 on Window XP Home SP2. I think it's doing what it's supposed to, ie. waiting for done==1, just that this never happens. I've stopped using tkwait.variable for just this reason, and use tkwait.window(tt). You have to be careful to recover the variables you need before closing the window, so the function attached to the 'OK' button should take care of that before calling tkdestroy. (See http://www.nabble.com/Dropdown-boxes-in-tcltk-and-R-tf3751327.html#a10603161) Variables can be created in .GlobalEnv with - which will be availble after the function exits. See also how Greg Snow uses a purpose-built environment in his 'TeachingDemos' package. Mike __ 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. -- View this message in context: http://www.nabble.com/tcltk-crashing-R-after-the-%28ab%29use-of-tkwait-tf3487962.html#a10821463 Sent from the R help mailing list archive at Nabble.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 and provide commented, minimal, self-contained, reproducible code.