Re: [R] set.seed ( ) function
Tal Let me express some concern about using words like "true" or "real" in relation to random number generation - for exactly the same reasons as mentioned here: http://xianblog.wordpress.com/2010/09/07/truly-random/ Device random number generators (whether provided via web-services or not) should be regarded with as much skepticism as algorithmic generators, and they typically don't have a set.seed() function for reproducibility -- you would have to store the entire sequence. - Niels On 22/04/11 04.28, Tal Galili wrote: BTW, Ken Kleinman recently wrote a post on how to get a "real" random numbers (into R) from a web-service: http://www.r-bloggers.com/example-8-35-grab-true-not-pseudo-random-numbers-passing-api-urls-to-functions-or-macros/ <http://www.r-bloggers.com/example-8-35-grab-true-not-pseudo-random-numbers-passing-api-urls-to-functions-or-macros/> Cheers, Tal Contact Details:--- Contact me: tal.gal...@gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) -- On Fri, Apr 22, 2011 at 6:47 AM, Joshua Wileywrote: On Thu, Apr 21, 2011 at 8:34 PM, Penny Bilton wrote: Hi Josh, Thanks for your reply. The problem is have is in trying to retain the proportions of 2 groups in my data while sampling into training and test sets. I find that different arguments for set.seed give very different proportions of my 2 groups in the training and test sets. Sure, just because numbers are random does not guarantee that equal numbers from both groups will be sampled. Perhaps you are looking for some sort of constrained random sampling like sampling x from group 1 and x from group 2? If so, try calling sample() separately on each group (for help applying the same function to different groups, take a look at ?by or ?tapply for example). Josh PS cced back to list Penny. On 22/04/2011 3:27 p.m., Joshua Wiley wrote: Hi, On Thu, Apr 21, 2011 at 8:18 PM, Penny Bilton wrote: I am using /set.seed()/ before the /sample/ function. How does the length of the argument of /set.seed()/ and order of the digits affect how the sampling is carried out? You can use set.seed() to specify a particular seed so that while pseudo-random numbers are sampled, you can repeat it. For example: set.seed(10) rnorm(10) set.seed(10) rnorm(10) Specifically, I have used set.seed(123456789). Will this configuration give me a genuinely random sampling?? You will never get truly random sampling from a computer algorithm, but it is darn close and more than adequate in the majority of cases. 123456789 is just a length 1 vector containing the number 123456789, not 9 separate numbers. Google will be able to give you a lot of information on pseudo-random number algorithms as well as the concept of "seeds". Also see ?set.seed Cheers, Josh Thank you in anticipation. Penny. [[alternative HTML version deleted]] __ 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. -- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.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. [[alternative HTML version deleted]] __ 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. -- Niels Richard Hansen Web: www.math.ku.dk/~richard Associate Professor Email: niels.r.han...@math.ku.dk Department of Mathematical Sciences nielsrichardhan...@gmail.com University of Copenhagen Skype: nielsrichardhansen.dk Universitetsparken 5 Phone: +1 510 502 8161 2100 Copenhagen Ø Denmark __ 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] Matching a vector with a matrix row
Joshua and Luis Neither of you is exactly solving the problem as stated, see below. Luis, could you clarify if you want rows that are _equal_ to a vector or rows with entries _contained_ in a vector? If m <- matrix(c("A", "B", "C", "B", "A", "A"), 3, 2) LHS <- c("A", "B") then LHS equals the first row only, while apply(m, 1, function(x) all(x %in% LHS)) [1] TRUE TRUE FALSE finds the rows with entries contained in LHS and which(m %in% LHS) [1] 1 2 4 5 6 finds all entries in m that equals an entry in LHS. While you can turn the latter into the former, this will have some computational costs too. The R-code apply(m, 1, function(x) all(x == LHS)) [1] TRUE FALSE FALSE finds the rows that are equal to LHS. - Niels On 22/04/11 00.18, Joshua Wiley wrote: Hi Felipe, Since matrices are just a vector with dimensions, you could easily use something like this (which at least on my system, is slightly faster): results<- which(Matrix %in% LHS) I'm not sure this is the fastest technique thought. It will return a vector of the positions in "Matrix" that match "LHS". You can easily convert to row numbers if you want since all columns have the same number of rows. HTH, Josh On Thu, Apr 21, 2011 at 8:56 PM, Luis Felipe Parra wrote: Hello I am trying to compare a vector with a Matrix's rows.The vector has the same length as the number of columns of the matrix, and I would like to find the row numbers where the matrix's row us the same as the given vector. What I am doing at the moment is using apply as follows: apply(Matrix,1,function(x)all(x%in%LHS)) but this isn't too fast actually. I would like to know if any body knows an efficient (fast) way of doing this? The matrix contains stings (not numbers). Thank you Felipe Parra [[alternative HTML version deleted]] __ 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. -- Niels Richard Hansen Web: www.math.ku.dk/~richard Associate Professor Email: niels.r.han...@math.ku.dk Department of Mathematical Sciences nielsrichardhan...@gmail.com University of Copenhagen Skype: nielsrichardhansen.dk Universitetsparken 5 Phone: +1 510 502 8161 2100 Copenhagen Ø Denmark __ 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] Seeking help re: ? single apply command or an alternative
a1 * a3[a2] - Niels Dear R-help list, Sorry to trouble everyone. This seems like it could be achieved with a single command (? variant of apply), but I am not quite seeing it. I am trying to multiply one vector (a1 below) by corresponding values in a3 (as determined by matching element in a vector a2). The example below might be clearer. a1 <- c(4,3,4,6,8,9,2,3,4,6,7,4) a2 <- c("A","A","A","A","B","B","C","C","C","D","D","D") a3 <- c(2,4,1,0.5) names(a3) <- c("A","B","C","D") I would like the vector (8,6,8,12,32,36,2,3,4,3,3.5,2) as my final output result i.e. multiply each element of a1 by the matching a3 as determined by the corresponding element of a2 - that is, for the first 4 elements of a1, multiply by the value 2 found in a3 ("A"), the next 2 by 4 ("B"), the next 3 by 1.0 ("C"), and the last 3 by 0.5 ("D") I am trying to avoid the use of loops, because the actual dataset is much larger. Thanks very much! Min-Han [[alternative HTML version deleted]] __ 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. On 21/04/11 23.34, Min-Han Tan wrote: -- Niels Richard Hansen Web: www.math.ku.dk/~richard Associate Professor Email: niels.r.han...@math.ku.dk Department of Mathematical Sciences nielsrichardhan...@gmail.com University of Copenhagen Skype: nielsrichardhansen.dk Universitetsparken 5 Phone: +1 510 502 8161 2100 Copenhagen Ø Denmark __ 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] cube root
?Syntax for precedence of operators. On 16/04/11 22.51, Branimir K. Hackenberger wrote: This is some interesting: -8^(1/3) [1] -2 x=(-8:8) y=x^(1/3) y [1] NaN NaN NaN NaN NaN NaN NaN NaN 0.00 1.00 [11] 1.259921 1.442250 1.587401 1.709976 1.817121 1.912931 2.00 So, can anybody explain this?! (Why is x[1]^(1/3)=y[1]=NaN, but -8^(1/3)=-2?) Thx!!! [[alternative HTML version deleted]] __ 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. -- Niels Richard Hansen Web: www.math.ku.dk/~richard Associate Professor Email: niels.r.han...@math.ku.dk Department of Mathematical Sciences nielsrichardhan...@gmail.com University of Copenhagen Skype: nielsrichardhansen.dk Universitetsparken 5 Phone: +1 510 502 8161 2100 Copenhagen Ø Denmark __ 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] Krylov subspace computations of matrix exponentials
I use the very nice expm functions available from the expm and Matrix packages. My understanding is that for large sparse matrices the currently best methods available are Krylov subspace methods, but they are as far as I can tell not implemented in either of the packages mentioned, nor in any other R package I have found. Does anybody know if Krylov subspace methods are available from any R package? If not, is there anybody working on this or planning to do so? If not, I might be interested in making such an implementation, because I find myself in the need of fast methods for very large sparse matrices. A third question. There exists a Fortran implementation called Expokit with an ad hoc license stating that "Permission to use, copy, modify, and distribute EXPOKIT and its supporting documentation for non-commercial purposes, is hereby granted without fee, provided that this permission message and copyright notice appear in all copies." If I choose to use Expokit and just write an interface to R, would there be any problems with having such a copyright notice in the package? Thanks, Niels -- Niels Richard Hansen Web: www.math.ku.dk/~richard Associate Professor Email: niels.r.han...@math.ku.dk Department of Mathematical Sciences nielsrichardhan...@gmail.com University of Copenhagen Skype: nielsrichardhansen.dk Universitetsparken 5 Phone: +1 510 502 8161 2100 Copenhagen Ø Denmark __ 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] Avoiding a loop
I believe the solutions proposed ignore the recursive nature of the original problem and hence produce wrong solutions. P <- c(5, 7, 6.01, 6.01, 7) m <- rep(6, 5) S0 <- as.numeric(P>(m*1.005)) Then the original loop from Worik gives S <- S0 for(i in 2:length(S)){ if(S[i]==0 && S[i-1] == 1){ if(P[i] > m[i]){ S[i] <- 1 } } } > S [1] 0 1 1 1 1 The other solutions I have seen on the list suggest looking upfront on differences in S, and I am pretty sure the intentions were to produce S <- S0 v <- c(FALSE, (S[-1] == 0) & (S[-5] == 1)) & (P > m) where v equals c(FALSE, FALSE, TRUE, FALSE, FALSE) and then S[v] <- 1 giving > S [1] 0 1 1 0 1 When you recursively update a vector like this I don't know any general vectorization fix. - Niels On 08/04/11 00.58, Juan Carlos Borrás wrote: Kenn, I find your solution more elegant. 2011/4/8 Kenn Konstabel: 2011/4/8 Juan Carlos Borrás: #Use the indexes of S in a sapply function. N<- 10 S<- sample(c(0,1), size=N, replace=TRUE) v1<- sapply(c(1:N-1), function(i) S[i]&&S[i+1]) You can achieve the same v1 using v1.2<- S[2:N-1]& S[2:N] .. or if you insist on having NA as the first element, -- c(NA, v1.2) Vectorization is more efficient than loops but this need not be true for the *apply functions. # Then v2<- (P> m) __ 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. -- Niels Richard Hansen Web: www.math.ku.dk/~richard Associate Professor Email: niels.r.han...@math.ku.dk Department of Mathematical Sciences nielsrichardhan...@gmail.com University of Copenhagen Skype: nielsrichardhansen.dk Universitetsparken 5 Phone: +1 510 502 8161 2100 Copenhagen Ø Denmark __ 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] cv.lars() function in LARS package
Minxolee Cross validation is based on random splits of the data, hence it can not be expected that the results are exactly identical. If you want to do lasso, the glmnet package by the same authors is a better choice. - Niels On 07/04/11 16.08, minxolee wrote: I am trying to get familiar with the lars package and find out by repeating the cv.lars() function, I am not getting the same result consistently. Could someone help on that? Here is a simple reproducible example. library(lars) data(diabetes) attach(diabetes) cv.lars(x,y,K=2) cv.lars(x,y,K=2) The last two runs of cv.lars give out different cross-validation plots. Thanks, minxolee -- View this message in context: http://r.789695.n4.nabble.com/cv-lars-function-in-LARS-package-tp3434822p3434822.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. -- Niels Richard Hansen Web: www.math.ku.dk/~richard Associate Professor Email: niels.r.han...@math.ku.dk Department of Mathematical Sciences nielsrichardhan...@gmail.com University of Copenhagen Skype: nielsrichardhansen.dk Universitetsparken 5 Phone: +1 510 502 8161 2100 Copenhagen Ø Denmark __ 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] Lars package
Achin Without dwelling too much on the semantic problem, the answer to my understanding of your question is: No, there is no C code in the lars package, but there is a single Fortran routine in the package. The Fortran routine does not seem to do anything useful in itself. By the way, the more recent glmnet package by the same authors provides a faster alternative to lars (for lasso), and it works for glm's too. - Niels On 07/04/11 05.10, David Winsemius wrote: On Apr 7, 2011, at 3:43 AM, ac...@cse.iitb.ac.in wrote: Hello, I am a student of IIT Bombay and I am using lars for one of my projects. I wanted to enquire if lars is using C under the hood. Is it true? Can someone point me to the corresponding C function? Thank You very much. If you just type lars at the console (after loading the package) , you will see the uncommented code. Most of R has "C (or Fortran) under the hood", so there may be a semantic problem in answering that direct question. You can get the full source on any CRAN mirror. -- Niels Richard Hansen Web: www.math.ku.dk/~richard Associate Professor Email: niels.r.han...@math.ku.dk Department of Mathematical Sciences nielsrichardhan...@gmail.com University of Copenhagen Skype: nielsrichardhansen.dk Universitetsparken 5 Phone: +1 510 502 8161 2100 Copenhagen Ø Denmark __ 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] environment question: changing variables from a formula through model.frame?
Hi Tal On 29/01/11 13.25, Tal Galili wrote: Hello all, I came across a behavior of R with environments that I'm not sure what is causing it. It involves changing variables that are found through using model.frame on a formula inside a function. I wonder if it's a "bug" or a "feature". And in either case, how it might be managed. As far as I can tell, nothing unexpected happens. Here is a simple example: # let's say we have an x and y variables: temp_x<- rep(1,5) temp_y<- c(1:5) # we now create a function that will take a formula of y~x and will (try and *fail* to) change "y" (only) inside the environment of this function: foo1<- function(formu) { print("Before changing the 'y' variable in the formula") print(model.frame(formu)) temp_y<- model.frame(formu)[,1] +10 the_txt<- paste(names(model.frame(formu))[1], "temp_y", sep = "<-") eval(parse(text = the_txt)) # jutter out y var so to be able to handle identical values. print("After changing the 'y' variable in the formula") print(model.frame(formu)) # why isn't it printing the new y I just created for the enviornment of this function? } # running the function shows the problem: foo1(temp_y ~ temp_x) I am not really sure what you are trying to achieve. In your code you end up evaluating temp_y <- temp_y inside your function, and then print out the model.frame of the formula -- again. You did not modify anything in the model frame or the environment used to create the model frame. The model frame is a data frame, which in this case is created from the environment associated with the formula. # If I'll try it using<<-, this will change the y in the global environment (something I'd rather avoid) # for example: foo2<- function(formu) { print("Before changing the 'y' variable in the formula") print(model.frame(formu)) temp_y<- model.frame(formu)[,1] +10 the_txt<- paste(names(model.frame(formu))[1], "temp_y", sep = "<<-") eval(parse(text = the_txt)) # jutter out y var so to be able to handle identical values. print("After changing the 'y' variable in the formula") print(model.frame(formu)) # why isn't it printing the new y I just created for the enviornment of this function? It doesn't? } foo2(temp_y ~ temp_x) temp_y It changes the temp_y in the global environment. But as far as I can tell, either you do that our you change the variable in the environment of the function, which will not affect the result of a later call to model.frame. Alternatively, you might consider assign(names(model.frame(formu))[1], temp_y, environment(formu)) which explicitly assigns the new value to the variable in the environment associated with the formula. The difference is shown if you try foo3 <- function() { temp_x <- rep(1,5) temp_y <- c(1:5) foo2(temp_y ~ temp_x) } foo3() If your objective is to change data associated with the formula locally in a function before passing the formula on to some other function, you might consider passing on the modified model frame instead of modifying the data in the environment associated with the formula. Hope it helped, Niels --- I know this question is somewhat of an oddity, but I hope some of you might help me understand what is happening here. Best, Tal Contact Details:--- Contact me: tal.gal...@gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) -- [[alternative HTML version deleted]] ______ 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. -- Niels Richard Hansen Web: www.math.ku.dk/~richard Associate Professor Email: niels.r.han...@math.ku.dk Department of Mathematical Sciences nielsrichardhan...@gmail.com University of Copenhagen Skype: nielsrichardhansen.dk Universitetsparken 5 Phone: +1 510 502 8161 2100 Copenhagen Ø Denmark __ 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] dataframe: string operations on columns
On 2011-01-18 08:14, Ivan Calandra wrote: Hi, I guess it's not the nicest way to do it, but it should work for you: #create some sample data df<- data.frame(a=c("A B", "C D", "A C", "A D", "B D"), stringsAsFactors=FALSE) #split the column by space df_split<- strsplit(df$a, split=" ") #place the first element into column a1 and the second into a2 for (i in 1:length(df_split[[1]])){ df[i+1]<- unlist(lapply(df_split, FUN=function(x) x[i])) names(df)[i+1]<- paste("a",i,sep="") } I hope people will give you more compact solutions. HTH, Ivan You can replace the loop with df <- transform(df, a1 = sapply(df_split, "[[", 1), a2 = sapply(df_split, "[[", 2)) df <- cbind(df, do.call(rbind, df_split) seems to do the same (up to column names) but faster. However, all the solutions rely on there being exactly two strings when you split. The different solutions behave differently if this assumption is violated and none of them really checks this. You can, for instance, check this with all(sapply(df_split, length) == 2) Best, Niels R. Hansen Peter Ehlers Le 1/18/2011 16:30, boris pezzatti a écrit : Dear all, how can I perform a string operation like strsplit(x," ") on a column of a dataframe, and put the first or the second item of the split into a new dataframe column? (so that on each row it is consistent) Thanks Boris __ 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] Where is a package NEWS.Rd located?
Dear Kevin Just had the same "problem", that is, placed the NEWS.Rd in the package inst/ directory, and the news() function did not find the information. The help page for news() is, however, pretty clear: "..., if the given add-on package can be found in the given libraries, it is attempted to read its news in structured form from files 'inst/NEWS.Rd', 'NEWS' or 'inst/NEWS' (in that order)." Hence, installing NEWS.Rd in an inst/ directory works fine. I have no idea if an installed package should or shouldn't have an inst/ subdirectory, but from the help page the functionality seems intentional to me. - Niels On 06/01/11 12.35, Kevin Wright wrote: Andy, thanks for providing a clear way of saying it. I thought I was clear in the first place, but oh well). Here is the structure of my source files: hwpkg/DESCRIPTION hwpkg/R/hw.R hwpkg/inst/NEWS.Rd I'm using Windows XP. When I install this package, I do this: Rcmd INSTALL hwpkg Which results in ls c:/r/r-2.12.0/library/hwpkg/ -rwxr-x---+ 1 wrightkevi 355 Jan 6 14:19 DESCRIPTION drwxrwx---+ 2 wrightkevi 0 Jan 6 14:19 Meta -rwxr-x---+ 1 wrightkevi 18 Jan 6 14:19 NEWS.Rd drwxrwx---+ 2 wrightkevi 0 Jan 6 14:19 R drwxrwx---+ 2 wrightkevi 0 Jan 6 14:19 help drwxrwx---+ 2 wrightkevi 0 Jan 6 14:19 html As you see, there is no "inst/NEWS.Rd" file (NEWS.Rd has been moved UP a level), and so news(package="hwpkg") returns nothing. If I build the package into a zipfile and then install.packages(zipfile), the same problem occurs. Kevin On Thu, Jan 6, 2011 at 2:06 PM, Liaw, Andy wrote: I was communicating with Kevin off-list. The problem seems to be run time, not install time. News() calls tools:::.build_news_db(), and the 2nd line of that function is: nfile<- file.path(dir, "inst", "NEWS.Rd") and that's the problem: an installed package shouldn't have an inst/ subdirectory, right? Andy -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Duncan Murdoch Sent: Thursday, January 06, 2011 2:30 PM To: Kevin Wright Cc: R list Subject: Re: [R] Where is a package NEWS.Rd located? On 06/01/2011 2:19 PM, Kevin Wright wrote: Yes, exactly. But the problem is with NEWS.Rd, not NEWS. I'm not sure who you are arguing with, but if you do file a bug report, please also put together a simple reproducible example, e.g. a small package containing NEWS.Rd in the inst directory (which is where the docs say it should go) and code that shows why this is bad. Don't just talk about internal functions used for building packages; as far as we can tell so far tools:::.build_news_db is doing exactly what it should be doing. Duncan Murdoch pkg/inst/NEWS.Rd is moved to pkg/NEWS.Rd at build time, but for installed packages, "news" tried to load "pkg/inst/NEWS.Rd". I'm going to file a bug report. Kevin On Thu, Jan 6, 2011 at 7:29 AM, Kevin Wrightwrote: If you look at tools:::.build_news_db, the plain text NEWS file is searched for in pkg/NEWS and pkg/inst/NEWS, but NEWS.Rd in only searched for in pkg/inst/NEWS.Rd. Looks like a bug to me. I *think*. Thanks, Kevin On Thu, Jan 6, 2011 at 7:09 AM, Kevin Wrightwrote: Hopefully a quick question. My package has a NEWS.Rd file that is not being found by "news". The "news" function calls "tools:::.build_news_db" which has this line: nfile<- file.path(dir, "inst", "NEWS.Rd") So it appears that the "news" function is searching for "mypackage/inst/NEWS.Rd". However, "Writing R extensions" says "The contents of the inst subdirectory will be copied recursively to the installation directory" During the installation, mypackage/inst/NEWS.Rd is copied into the "mypackage" directory, not "mypackage/inst". What am I doing wrong, or is this a bug? Kevin Wright -- Kevin Wright -- Kevin Wright __ 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. Notice: This e-mail message, together with any attachments, contains information of Merck& Co., Inc. (One Merck Drive, Whitehouse Station, New Jersey, USA 08889), and/or its affiliates Direct contact information for affiliates is available at http://www.merck.com/contact/contacts.html) that may be confidential, proprietary copyrighted and/or legally privileged. It is intended solely for the use of the individual or entity named on this message. If you are not the intended recipient, and have received this message in error, please not
Re: [R] How to "bin"/average" time points?
Hi Kevin Here is one way: yourData <- c(0.730, 0.732, 0.743, 0.757,0.781, 0.731, 0.830, 0.832, 0.843, 0.857, 0.881, 0.831) nrGroups <- 2 lengthGroups <- 6 tapply(yourData, factor(rep(c(1,nrGroups), each = lengthGroups)), mean) and you will have to adjust the number of groups and if necessary the length of the groups to your needs. I am assuming that all groups are of the same length. Do you really want to average the time points? In that case you might want to consider the data structures for representing dates and times, see e.g. ?POSIXct or converting the times to minutes, say. - Niels On 25/11/10 12.49, DonDolowy wrote: Dear all, I am pretty new to R only having an introduction course, so please bare with me. I am doing my PhD at The Max Planck Institute of Immunobiology where I am analyzing some calorimetry data from some mice. I have a spreadsheet consisting of measurements of the respiratory exchange rate at different time points measured every 9 minutes over some days. My goal is "bin"/average the time points of each hour to only one measurements. E.g. [Time] - [Measurement] 12.09 - 0.730 12.18 - 0.732 12.27 - 0.743 12.36 - 0.757 12.45 - 0.781 12.54 - 0.731 --> should be averaged to fx one time point and one value, fx: 12.30 - [average of the six measurements] I know how to average the measurements in a whole column but how to average every six measurements automatically and also how to average every six time points and make a new sheet consisting of these data? I hope you guys are able to help, since we are really stuck here. I can of course do it manually but with>8000 measurements it will take lots of time. Thank you very much. Best regards, Kevin Dalgaard __ 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] interactive session
On 30/09/10 22.23, Peter Dalgaard wrote: On 09/30/2010 03:33 PM, Pam wrote: Thanks Niels but it won't do.. please copy and paste the 2 lines below together to your console in order to see what I mean: cat("?"); a<-readLines(n=1) b<-paste("t",a,sep="") anyone / any idea to overcome this problem? Best, Fatih You might want to source() a file with those lines rather than pasting them to the console. There's just no way you can retroactively insert text between two already submitted lines. Exactly, it works when you source() the file. - Niels You can do things like this, though {cat("?"); a<-readLines(n=1) "hey" b<-paste("t",a,sep="")} However, there's a catch {cat("?"); a<-readLines(n=1) + "hey" + b<-paste("t",a,sep="")} ?ada b [1] "tada" Notice that the "hey" doesn't print. -- Niels Richard Hansen Web: www.math.ku.dk/~richard Associate Professor Email: niels.r.han...@math.ku.dk Department of Mathematical Sciences nielsrichardhan...@gmail.com University of Copenhagen Skype: nielsrichardhansen.dk Universitetsparken 5 Phone: +45 353 20783 (office) 2100 Copenhagen Ø +45 2859 0765 (mobile) Denmark __ 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] interactive session
Hi Fatih I believe that readLines(n=1) will do the job. It works fine from the Windows RGui, but I noticed that it hangs my Aquamacs/ESS when R runs from there, and a C-g was needed (which may be completely irrelevant to you). Best, Niels On 30/09/10 08.55, Pam wrote: Hi guys, My concern is to create an automated process from the beginning to the end. I want to copy all my code together in one piece and paste it to R console and sit back and relax :) except one moment in which the program should ask me to enter a number.. and only then (only after getting a valid number from me) it should continue to read and process the rest of the code. I�tried�lots of things (readline, readLines, scan, interactive, ask, switch,...) and read manuals and searched help forums.. I found several similar questions but never a satisfying answer.. so now it became a challenge.. any idea how�to�overcome this problem (R 2.11.1 for Windows)? (an example�code is below)� Best, Fatih library(gtools) library(YaleToolkit) library(xts) � ### start of my�wrong�function! f<-function(w){ �w<-readline("which data? ") �w<-as.numeric(w) �ifelse(is.numeric(w)=="TRUE", w, f()) �} f() # end of my�wrong function v<- ## and output of my function should be a "v"�for example which I can use it in the next line�(v<-w� or something like that??) � ##the rest works fine p<-paste("t", v, ".txt", sep = "") t<-read.table(p, header=FALSE, sep="\t", dec=",", blank.lines.skip=FALSE) rownames(t)<-as.Date(t[,1],"%d.%m.%Y") colnames(t)<-c("date","start","high","low","end","w.average","lot", "volume") x<-as.xts(t) whatis(x)��� . . [[alternative HTML version deleted]] __ 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. -- Niels Richard Hansen Web: www.math.ku.dk/~richard Associate Professor Email: niels.r.han...@math.ku.dk Department of Mathematical Sciences nielsrichardhan...@gmail.com University of Copenhagen Skype: nielsrichardhansen.dk Universitetsparken 5 Phone: +45 353 20783 (office) 2100 Copenhagen Ø +45 2859 0765 (mobile) Denmark __ 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] drop.terms and [.terms ignores intercept
On 29/09/10 11.13, peter dalgaard wrote: On Sep 29, 2010, at 10:29 , Niels Richard Hansen wrote: The functions drop.terms and [.terms ignores if the intercept has been explicitly removed. Is that a deliberate feature? Perhaps rather an unimplemented one. The root cause is that both functions use reformulate() on the "term.labels" attribute, and there is no way to specify that you want to reformulate into a no-intercept formula. On the other hand, the modeling code will happily proceed with a no-intercept model even if there is no "-1" in formula part of a terms object, e.g. x<- terms(y~a+b) attr(x,"intercept")<- 0 lm(x) Call: lm(formula = x) Coefficients: a b 0.2263 0.4178 formula(x) y ~ a + b so I suppose that there is no really good excuse not to carry the "intercept" attribute over. As usual, with code as old as this, there is always the risk that something actually relies on current behavior. Thanks Peter. I expected something like that. As I see it, the intercept (or more often, removal of the intercept) is treated differently from other terms, and information on the presence of an intercept is stored in an attribute of the terms object rather than the formula. This attribute is not copied when using drop.terms or [.terms. I believe it should be, but as you say Peter, some may rely on the way the functions behave now. So basically, you should use code like x <- terms(y~a+b-1) z <- x[2] attr(z, "intercept") <- attr(x, "intercept") to make sure that the intercept information is preserved. - Niels -- Niels Richard Hansen Web: www.math.ku.dk/~richard Associate Professor Email: niels.r.han...@math.ku.dk Department of Mathematical Sciences nielsrichardhan...@gmail.com University of Copenhagen Skype: nielsrichardhansen.dk Universitetsparken 5 Phone: +45 353 20783 (office) 2100 Copenhagen Ø +45 2859 0765 (mobile) Denmark __ 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] drop.terms and [.terms ignores intercept
Dear R-help list The functions drop.terms and [.terms ignores if the intercept has been explicitly removed. Is that a deliberate feature? For instance, drop.terms(terms(~a+b-1),1) terms(~a+b-1)[2] R version 2.12.0 Under development (unstable) (2010-09-13 r52905) Best, Niels -- Niels Richard Hansen Web: www.math.ku.dk/~richard Associate Professor Email: niels.r.han...@math.ku.dk Department of Mathematical Sciences nielsrichardhan...@gmail.com University of Copenhagen Skype: nielsrichardhansen.dk Universitetsparken 5 Phone: +45 353 20783 (office) 2100 Copenhagen Ø +45 2859 0765 (mobile) Denmark __ 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] The permutation of one vector into another
match(b, a) ? Michael Right! Thanks, Michael. On 16 September 2010 17:13, Niels Richard Hansen wrote: > Dear R-help-list > > I have two character vectors > > a <- c("A", "B", "C") > b <- c("A", "C", "B") > > Then > > sapply(a, function(i) grep(i, b)) > > computes the permutation of the entries in 'b' needed > to bring 'b' into the same order as 'a'. > > I have searched around, but haven't been able to find > any existing function that compute this permutation. > Is there such a function doing this -- perhaps more > efficiently than the above? > > For those interested, I need the permutation to bring > the rows of a data frame into the same order as the > levels of a factor. > > Thanks, Niels > -- Niels Richard Hansen Web: www.math.ku.dk/~richard Associate Professor Email: niels.r.han...@math.ku.dk Department of Mathematical Sciences nielsrichardhan...@gmail.com University of Copenhagen Skype: nielsrichardhansen.dk Universitetsparken 5 Phone: +45 353 20783 (office) 2100 Copenhagen Ø +45 2859 0765 (mobile) Denmark __ 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] The permutation of one vector into another
Dear R-help-list I have two character vectors a <- c("A", "B", "C") b <- c("A", "C", "B") Then sapply(a, function(i) grep(i, b)) computes the permutation of the entries in 'b' needed to bring 'b' into the same order as 'a'. I have searched around, but haven't been able to find any existing function that compute this permutation. Is there such a function doing this -- perhaps more efficiently than the above? For those interested, I need the permutation to bring the rows of a data frame into the same order as the levels of a factor. Thanks, Niels -- Niels Richard Hansen Web: www.math.ku.dk/~richard Associate Professor Email: niels.r.han...@math.ku.dk Department of Mathematical Sciences nielsrichardhan...@gmail.com University of Copenhagen Skype: nielsrichardhansen.dk Universitetsparken 5 Phone: +45 353 20783 (office) 2100 Copenhagen Ø +45 2859 0765 (mobile) Denmark __ 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] 'seq' help page: seq_length -> seq_len?
In the Value section of the 'seq' help page it says 'seq_along' and 'seq_length' always return an integer vector. I believe it should be 'seq_along' and 'seq_len' always return an integer vector. as there are no seq_length function? Best, Niels -- Niels Richard Hansen Web: www.math.ku.dk/~richard Associate Professor Email: niels.r.han...@math.ku.dk Department of Mathematical Sciences nielsrichardhan...@gmail.com University of Copenhagen Skype: nielsrichardhansen.dk Universitetsparken 5 Phone: +45 353 20783 (office) 2100 Copenhagen Ø +45 2859 0765 (mobile) Denmark __ 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 termplot() with transformations of x
On 27/08/10 01.10, Peter Dunn wrote: Hi all I was playing with termplot(), and came across what appears to be an inconsistency. It would appreciate if someone could enlighten me: # First, generate some data: y<- rnorm(100) x<- runif(length(y),1,2) # Now find the log of x: logx<- log(x) # Now fit two models that are exactly the same, but specified differently: m1<- lm(y~log(x)) # Using log in the call m2<- lm(y~logx) # Using the variable logx # The two termplots() are different: par(mfrow=c(1,2)) termplot(m1) termplot(m2) I see two models that are identical, but produce different termplot()s. In both cases, the independent variable is log(x), but is just specified differently. If this is intended, what is the logic? Or am I missing something? I believe it is intended, and quite as expected. The plot is against the term variable on its original scale - not the transformed scale. - Niels Thanks. P. sessionInfo() R version 2.11.1 (2010-05-31) x86_64-apple-darwin9.8.0 locale: [1] en_AU.UTF-8/en_AU.UTF-8/C/C/en_AU.UTF-8/en_AU.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base Peter Dunn: Biostatistician (Room T4.12) School of Health and Sport Science Faculty of Science, Health and Education ML-34 University of the Sunshine Coast, Locked Bag 4 Maroochydore DC Qld 4558 Tel: +61 7 5456 5085 Fax: +61 7 5430 2896 Email: pdu...@usc.edu.au www.usc.edu.au CRICOS Provider Number: 01595D This communication is intended for the recipient only and should not be forwarded, distributed or otherwise read by others without express permission. The views expressed in this email are not necessarily those of the University of the Sunshine Coast. -- Niels Richard Hansen Web: www.math.ku.dk/~richard Associate Professor Email: niels.r.han...@math.ku.dk Department of Mathematical Sciences nielsrichardhan...@gmail.com University of Copenhagen Skype: nielsrichardhansen.dk Universitetsparken 5 Phone: +45 353 20783 (office) 2100 Copenhagen Ø +45 2859 0765 (mobile) Denmark __ 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] Passing arguments between S4 methods fails within a function:bug? example with raster package.
On 26/08/10 18.42, Martin Morgan wrote: On 8/26/2010 8:43 AM, Niels Richard Hansen wrote: setGeneric("myplus",function(x,y,...) standardGeneric("myplus")) setMethod("myplus",c(x="numeric",y="numeric"), function(x,y,z=0) x+y+z ) setMethod("myplus",c(x="numeric",y="list"), function(x,y,...) callGeneric(x,unlist(y),...) ) myplus(1,1) ## 2; OK myplus(1,1,1) ## 3; OK myplus(1,list(1)) ## 2; OK myplus(1,list(1),z=1) ## 3; OK a.c1 <- 1 myplus(1,list(1),z=a.c1) ## 3; OK test <- function(x,y) { a.c <- 1 ## Problem occurs when using '.' in variable name myplus(a,y,z=a.c) } test(1,list(1)) ## error test(1,1) ## 3; OK I get an error in both instances, because in fact there is no variable 'a' defined (anywhere) in the session. I do get your results if I define a global variable a <- 2 Oops, right. The reason for the original problem can be seen by looking at how the methods are implemented (I added parentheses in the setMethod calls for clarity) > showMethods(myplus, includeDef=TRUE) Function: myplus (package .GlobalEnv) x="numeric", y="list" function (x, y, ...) { callGeneric(x, unlist(y), ...) } x="numeric", y="numeric" function (x, y, ...) { .local <- function (x, y, z = 0) { x + y + z } .local(x, y, ...) } and in particular the c("numeric", "numeric") method has an extra argument z, and so has a nested .local function. This is how the methods package deals with method signatures that differ from the generic. I think, roughly, that when c("numeric", "numeric") tries finally to resolve it's argument 'x', it looks for a variable 'a' in the environment two levels up (method --> generic) from where it is being resolved. I believe that is true. If 'a' is put into the environment two levels up (using 'browser'), it is found. This is fine if the method has no .local environment (e.g., if c("numeric", "numeric") where defined as function(x, y, ...) { x +y }). I think that there is no general solution to this; the user could callGeneric or evaluate symbols from arbitrarily nested functions within either the calling or called methods themselves, and could arrive at a particular method through callGeneric or other routes (e.g., callNextMethod) that are not consistent in terms of the implied frame of evaluation. I could be mistaken. But as I understand it, the scoping rules solve such problems and locate the variable in any environment at a higher level? Replacing the callGeneric by a direct call of myplus removes the problem, it seems, though 'a' will still be three levels up. A simple solution is to provided named arguments to callGeneric, e.g., callGeneric(x=x, y=unlist(y), ...). I don't see how that solves the problem. - Niels These issues are likely to cause problems for eval / substitute expressions like the one posted by Joris yesterday, where there are implicit assumptions about the environment in which the experession is being evaluated. Martin -- Niels Richard Hansen Web: www.math.ku.dk/~richard Associate Professor Email: niels.r.han...@math.ku.dk Department of Mathematical Sciences nielsrichardhan...@gmail.com University of Copenhagen Skype: nielsrichardhansen.dk Universitetsparken 5 Phone: +45 353 20783 (office) 2100 Copenhagen Ø +45 2859 0765 (mobile) Denmark __ 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] Passing arguments between S4 methods fails within a function:bug? example with raster package.
Joris, I looked at the problem. Here is a minimal example reproducing the error setGeneric("myplus",function(x,y,...) standardGeneric("myplus")) setMethod("myplus",c(x="numeric",y="numeric"), function(x,y,z=0) x+y+z ) setMethod("myplus",c(x="numeric",y="list"), function(x,y,...) callGeneric(x,unlist(y),...) ) myplus(1,1) ## 2; OK myplus(1,1,1) ## 3; OK myplus(1,list(1)) ## 2; OK myplus(1,list(1),z=1) ## 3; OK a.c1 <- 1 myplus(1,list(1),z=a.c1) ## 3; OK test <- function(x,y) { a.c <- 1## Problem occurs when using '.' in variable name myplus(a,y,z=a.c) } test(1,list(1)) ## error test(1,1) ## 3; OK It looks like a bug. I went through all environments from inside the call of myplus("numeric","numeric") and a.c is nicely located in parent.frame(6) with the value 1. However, when it has a '.' (dot) in the variable name, the variable is not found. If it has a name without a '.' (dot) there is no problem: test <- function(x,y) { a <- 1 myplus(a,y,z=a) } test(1,list(1)) ## 3; OK - Niels On 26/08/10 15.32, Joris Meys wrote: Dear all, This problem came up initially while debugging a function, but it seems to be a more general problem of R. I hope I'm wrong, but I can't find another explanation. Let me illustrate with the raster package. For an object "RasterLayer" (which inherits from Raster), there is a method xyValues defined with the signature (object="RasterLayer",xy="matrix"). There is also a method with signature (object="Raster",xy="vector"). The only thing this method does, is change xy into a matrix and then pass on to the next method using callGeneric again. Arguments are passed. Now this all works smoothly, as long as you stay in the global environment : require(raster) a<- raster() a[]<- 1:ncell(a) origin<- c(-80,50) eff.dist<- 10 unlist(xyValues(a,xy=origin,buffer=eff.dist)) [1] 14140 14141 14500 14501 Now let's make a very basic test function : test<- function(x,orig.point){ eff.distance<- 10 p<- unlist(xyValues(x,xy=orig.point,buffer=eff.distance)) return(p) } This gives the following result : test(a,origin) Error in .local(object, xy, ...) : object 'eff.distance' not found huh? Apparently, eff.distance got lost somewhere in the parsetree (am I saying this correctly?) The funny thing is when we change origin to a matrix : origin<- matrix(origin,ncol=2) unlist(xyValues(a,xy=origin,buffer=eff.dist)) [1] 14140 14141 14500 14501 test(a,origin) [1] 14140 14141 14500 14501 It all works again! So something goes wrong with passing the arguments from one method to another using callGeneric. Is this a bug in R or am I missing something obvious? The relevant code from the raster package : setMethod("xyValues", signature(object='Raster', xy='vector'), function(object, xy, ...) { if (length(xy) == 2) { callGeneric(object, matrix(xy, ncol=2), ...) } else { stop('xy coordinates should be a two-column matrix or data.frame, or a vector of two numbers.') } } ) setMethod("xyValues", signature(object='RasterLayer', xy='matrix'), function(object, xy, method='simple', buffer=NULL, fun=NULL, na.rm=TRUE) { if (dim(xy)[2] != 2) { stop('xy has wrong dimensions; it should have 2 columns' ) } if (! is.null(buffer)) { return( .xyvBuf(object, xy, buffer, fun, na.rm=na.rm) ) } if (method=='bilinear') { return(.bilinearValue(object, xy)) } else if (method=='simple') { cells<- cellFromXY(object, xy) return(.readCells(object, cells)) } else { stop('invalid method argument. Should be simple or bilinear.') } } ) -- Niels Richard Hansen Web: www.math.ku.dk/~richard Associate Professor Email: niels.r.han...@math.ku.dk Department of Mathematical Sciences nielsrichardhan...@gmail.com University of Copenhagen Skype: nielsrichardhansen.dk Universitetsparken 5 Phone: +45 353 20783 (office) 2100 Copenhagen Ø +45 2859 0765 (mobile) Denmark __ 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] Problem with terms of the form (a >1) and subsetting of a terms object
On 26/08/10 09.30, Dimitris Rizopoulos wrote: I think you need an I(), i.e., form <- ~ I(a > 1) - 1 Yes, it solves the concrete problem, but does not really answer the question. Let me rephrase. Should the use of terms like (a>1) be discouraged in R and replaced by I(a>1) systematically, or is the behavior below unexpected? Best, Niels I hope it helps. Best, Dimitris On 8/26/2010 9:17 AM, Niels Richard Hansen wrote: I have the following problem mydata <- data.frame(a=1:3) form <- ~ (a>1) - 1 model.matrix(form,mydata) a > 1FALSE a > 1TRUE 1 1 0 2 0 1 3 0 1 ... However model.matrix(update(terms(form)[1],~.-1),mydata) (Intercept) a > 1 - 1TRUE 1 1 1 2 1 1 3 1 1 ... Taking terms(form)[1] extracts the formula with only the first term, but the parentheses are thrown away, and after the update, I get a different formula. Is that supposed to be so? Thanks, Niels -- Niels Richard Hansen Web: www.math.ku.dk/~richard Associate Professor Email: niels.r.han...@math.ku.dk Department of Mathematical Sciences nielsrichardhan...@gmail.com University of Copenhagen Skype: nielsrichardhansen.dk Universitetsparken 5 Phone: +45 353 20783 (office) 2100 Copenhagen Ø +45 2859 0765 (mobile) Denmark __ 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 with terms of the form (a >1) and subsetting of a terms object
I have the following problem mydata <- data.frame(a=1:3) form <- ~ (a>1) - 1 model.matrix(form,mydata) a > 1FALSE a > 1TRUE 1 1 0 2 0 1 3 0 1 ... However model.matrix(update(terms(form)[1],~.-1),mydata) (Intercept) a > 1 - 1TRUE 1 1 1 2 1 1 3 1 1 ... Taking terms(form)[1] extracts the formula with only the first term, but the parentheses are thrown away, and after the update, I get a different formula. Is that supposed to be so? Thanks, Niels -- Niels Richard Hansen Web: www.math.ku.dk/~richard Associate Professor Email: niels.r.han...@math.ku.dk Department of Mathematical Sciences nielsrichardhan...@gmail.com University of Copenhagen Skype: nielsrichardhansen.dk Universitetsparken 5 Phone: +45 353 20783 (office) 2100 Copenhagen Ø +45 2859 0765 (mobile) Denmark __ 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] Slow indexing access for Matrix
Consider the following little "benchmark" > require(Matrix) > tmp <- Matrix(c(rep(1,1000),rep(0,9000)),ncol=1) > ind <- sample(1:1,1) > system.time(tmp[ind,]) user system elapsed 0.004 0.001 0.005 > ind <- sample(1:1000,1,replace=TRUE) > system.time(tmp[ind,]) user system elapsed 0.654 0.006 0.703 > system.time(Matrix(as(tmp,"matrix")[ind,])) user system elapsed 0.005 0.000 0.006 First I access all 1 rows in a random order, which is fast, but when I access the first 1000 rows 1 times there is a considerable slowdown. Last I convert back and forth between matrix and Matrix and get a serious speedup. Am I missing a point here? Should I not use indexing with "[" for the sparse matrices if I have repeated indices? I'm running Mac OS X, version 10.5.6, with Matrix package version 0.999375-21. I hope that somebody can enlighten me on this issue. Thanks, Niels -- Niels Richard Hansen Associate Professor Department of Mathematical Sciences University of Copenhagen Universitetsparken 5 2100 Copenhagen Ø Denmark +45 353 20783 __ 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] Meaning of .local and the special token ..1 returned from match.call
I am writing a version of the subset function for a new class. I don't understand the behavior of match.call in this particular case, and I didn't seem to be able to find much help in the language definition or the email archive. Here follows a minimal example: setClass("myClass", representation(id = "factor") ) setMethod("subset","myClass", function(x,subset,...) match.call() ) tmp <- new("myClass",id=factor(1:10)) subset(x=tmp,subset=id >5) which gives me .local(x = x, subset = ..1) I want to call a further subset function, subset.data.frame, say, using the unevaluated expression "id > 5", but in this setup I don't understand how I should proceed. I can't find any explanation of .local and what the ..1 means, except that ..1 is a "special token". A small modification of the method as setMethod("subset","myClass", function(x,...) match.call() ) where I exclude explicitly mentioning the subset argument gives, however, what I expected: subset(x = tmp, subset = id > 5) This might be OK, and I am able to get everything to work without having an explicit subset argument in the class method -- by passing the ... -- but I think it would be nice to have the subset argument like in the S3 version of subset.data.frame. It seems that the issue is related to the fact that the generic subset method has the arguments (x,...). Is there a way to get around this so that my method can have explicit additional arguments like the subset-argument? Thanks for any help, Niels -- Niels Richard Hansen Associate Professor Department of Mathematical Sciences University of Copenhagen Universitetsparken 5 2100 Copenhagen Ø Denmark +45 353 20783 __ 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] Support vector model?
Dear Celine If you are considering using support vector machines in R, you might find it worthwhile to read the paper: http://www.jstatsoft.org/v15/i09/paper Best, Niels Celine Carret wrote: Dear All, Apologies for sending this email to both list, but at this point I'm not sure which one could help me the most. I have 4 sets of data, 1 test and 3 different sets of controls. The measurements are binary, with a matrix of 0 and 1 I'm measuring across time (rows, ~815) the behaviour of organelles in the cell by microscopy in response to different stimuli (several measurements for each set, 57 columns in total) Set 1: parasite test Set 2: no stimulus Set 3: inert stimulus (beads) Set 4: different pathogen Across time, a "zero" means nothing happens around my parasite introduced in the cell, a "1" means some cytoskeleton dynamics occurring around my parasite I want to give some statistical value to my observations in saying that the cytoskeleton dynamics are specific to my parasite at that frequency across time. I thought of comparing profiles, like a smooth profile to summarise all that is happening in each set and test for distances between 2 smoothed sets. But the timig when something is happening varies a lot, sometimes it's few seconds, sometimes minutes, sometimes only once per measurements, sometimes more for the same parasite.. I'm not sure how to proceed. I have been looking into e1071 package in R for support vector machine, but I'm not sure this will give me the right model. I am very grateful for any help / advice anyone can think of Thank you very much Celine -- Niels Richard Hansen Associate Professor Department of Mathematical Sciences University of Copenhagen Universitetsparken 5 2100 Copenhagen Ø Denmark +45 353 20783 __ 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] how to test for the empty set
G. Jay Kerns wrote: > Dear R-help, > > I first thought that the empty set (for a vector) would be NULL. > > x <- c() > x > > However, the documentation seems to make clear that there _many_ empty > sets depending on the vector's mode, namely, numeric(0), character(0), > logical(0), etc. This is borne out by > > y <- letters[1:3] > z <- letters[4:6] > intersect(y,z) > > which, of course, is non-NULL: > > is.null(character(0)) # FALSE > > So, how can we test if a vector is, say, character(0)? The following > doesn't (seem to) work: > > x <- character(0) > x == character(0) # logical(0) You could use length(x) == 0 > More snooping led to the following: > > wiki.r-project.org/rwiki/doku.php?id=tips:surprises:emptysetfuncs > > and at the bottom of the page it says "logical(0) is an empty set, > thus is TRUE". However, I get > > isTRUE(logical(0)) # FALSE > > but, on the other hand, > > all.equal(x, character(0)) # TRUE > > This would seem to be the solution, but am I missing something? and in > particular, is there an elegant way to check in the case that the mode > of the vector is not already known? > > Thanks in advance for any insight you may have. > > Best, > Jay > > > > > *** > G. Jay Kerns, Ph.D. > Associate Professor > Department of Mathematics & Statistics > Youngstown State University > Youngstown, OH 44555-0002 USA > Office: 1035 Cushwa Hall > Phone: (330) 941-3310 Office (voice mail) > -3302 Department > -3170 FAX > E-mail: [EMAIL PROTECTED] > http://www.cc.ysu.edu/~gjkerns/ > > __ > 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. -- Niels Richard Hansen Associate Professor Department of Mathematical Sciences University of Copenhagen Universitetsparken 5 2100 Copenhagen Ø Denmark +45 353 20783 __ 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.