Re: [R] avoiding eval parse with indexing

2013-05-26 Thread Florent D.
library(abind) asub(x, 1, 1) On Sun, May 26, 2013 at 10:43 AM, Bert Gunter wrote: > Martin: > > Well, assuming I understand, one approach would be to first get the > dim attribute of the array and then create the appropriate call using > that: > >> z <- array(1:24,dim=2:4) >> d <- dim(z) >> ix <-

[R] line profiling

2013-04-05 Thread Florent D.
Hello, This is about the new "line profiling" feature in R 3.0.0. As I was testing it, I find the results somewhat disappointing so I'd like to get your opinion. I put some poorly written code in a test.R file, here are the contents: double <- function(x) { out <- c() for (i in x) { out

Re: [R] memory management

2012-02-09 Thread Florent D.
This should help: > invisible(gc()) > > m0 <- memory.size() > mem.usage <- function(){invisible(gc()); memory.size() - m0} > Mb.size <- function(x)print(object.size(x), units="Mb") > > zz <- data.frame(a=runif(100), b=runif(100)) > mem.usage() [1] 15.26 > Mb.size(zz) 15.3 Mb > a <- zz$a >

Re: [R] Data Structure to Code

2012-01-29 Thread Florent D.
see dump() or dput(). On Sun, Jan 29, 2012 at 10:56 AM, Ajay Askoolum wrote: > Given: > > data(AirPassengers) > > I get a ts data structure AirPassengers in the workspace. > > How can I generate the code that can create that structure? That is, given an > example of a data structure, is there a

Re: [R] finding rows in a matrix that match a vector

2012-01-28 Thread Florent D.
Try this: mine <- 1:6 table.combos <- matrix(data = 1:12, nrow = 10, ncol = 6, byrow) row.is.a.match <- apply(table.combos, 1, identical, mine) match.idx <- which(row.is.a.match) total.matches <- sum(row.is.a.match) On Sat, Jan 28, 2012 at 2:10 AM, Jason J. Pitt wrote: > > Hi Melissa, > > Well,

Re: [R] R interactive = FALSE

2012-01-22 Thread Florent D.
You can also look at ?Rscript. There are tutorials online for writing such scripts. On Sun, Jan 22, 2012 at 9:31 AM, David Winsemius wrote: > > On Jan 22, 2012, at 6:01 AM, Ajay Askoolum wrote: > > I haven't been able to find an example of using R with interactive = >> FALSE. >> >> How do I star

Re: [R] how to save the R script itself into a rData file?

2012-01-21 Thread Florent D.
You said: << Lets say I changed the parameters x to 0.123, y to -0.456, z to -999.99 Then I have to save the R script file as "Experiment_001_x=0.123_y=-0.456_z=-999.99.r" and the result file as "Experiment_001_x=0.123_y=-0.456_z=-999.99.rData" >> I suggest you create instead a "Experiment.r" f

Re: [R] if/else statement without curly brackets gives a problem

2012-01-21 Thread Florent D.
It is well explained here http://www.burns-stat.com/pages/Tutor/R_inferno.pdf page 67. On Sat, Jan 21, 2012 at 9:56 PM, Ery Arias-Castro wrote: > Hello, > > This example seems strange to me: > > > if (2 > 3) print('Yes'); else print('No') > Error: unexpected 'else' in " else" > > > {if (2 > 3) p

Re: [R] dataframe: how to select an element from a row

2012-01-19 Thread Florent D.
Another possibility: df$Date[match(1800, df$myvalue)] match() stops at the first value encountered so it may be a bit faster than a full subset(), depending on your table size. Another difference: this approach would return NA if there was no match, subset(...)[1, ] would trigger an error. Depend

Re: [R] Word Wrap

2012-01-15 Thread Florent D.
see the 'fill' argument for cat(). On Sun, Jan 15, 2012 at 12:10 PM, Barry Rowlingson wrote: > On Sun, Jan 15, 2012 at 4:40 PM, Francisco > wrote: >> Hello, >> I have to write a big sentence with cat() and I would like that R >> automatically adds a new line when it is needed (when the text arr

Re: [R] Summing x1 to x6

2011-12-19 Thread Florent D.
>> I tried to be clever by trying get(paste(paste("x", 1:6, sep=""), >> collapse="+")) but it didn't work. Maybe you meant something like this: sapply(paste("x", 1:6, sep=""), function(x)mean(get(x))) On Mon, Dec 19, 2011 at 2:30 PM, David Winsemius wrote: > > On Dec 19, 2011, at 11:00 AM, Pab

Re: [R] Constrained Optimisation

2011-12-19 Thread Florent D.
try the lsei function from the limSolve package. On Mon, Dec 19, 2011 at 2:32 PM, Russell2 wrote: > Dear All > > I have a constrained optimisation problem, I want to maximise the following > function > > t(weights) %*% CovarianceMatrix %*% weights > > for the weights, > > subject to  constraints

Re: [R] Counting the occurences of a charater within a string

2011-12-01 Thread Florent D.
y(strsplit(x,"/"),length)-1 > > Cheers, > Bert > > On Thu, Dec 1, 2011 at 7:44 PM, Florent D. wrote: >> Resending my code, not sure why the linebreaks got eaten: >> >>> x <- data.frame(Col1 = c("abc/def", "ghi/jkl/mno"), string

Re: [R] transform data.frame holding answers --> data.frame holding logicals

2011-12-01 Thread Florent D.
I have this. I have modified your input structure to be a matrix. I think it is generally recommended to use matrices over data.frames when your data allows it, i.e., when you only have one type of data, here character(). Matrices are easier to work with. x <- matrix(  c('BMW', '', '',    'Mercedes

Re: [R] Counting the occurences of a charater within a string

2011-12-01 Thread Florent D.
count.slashes, 1)) Col1 Col2 1 abc/def1 2 ghi/jkl/mno 2 On Thu, Dec 1, 2011 at 10:32 PM, Florent D. wrote: > I used within and vapply: > > x <- data.frame(Col1 = c("abc/def", "ghi/jkl/mno"), stringsAsFactors = FALSE) > count.slashes <- fun

Re: [R] Counting the occurences of a charater within a string

2011-12-01 Thread Florent D.
I used within and vapply: x <- data.frame(Col1 = c("abc/def", "ghi/jkl/mno"), stringsAsFactors = FALSE) count.slashes <- function(string)sum(unlist(strsplit(string, NULL)) == "/")within(x, Col2 <- vapply(Col1, count.slashes, 1))          Col1 Col21     abc/def    12 ghi/jkl/mno    2 On Thu, Dec 1

Re: [R] how to call a function for each row

2011-11-30 Thread Florent D.
if you want to store the result in a column of your data.frame: within(df, Y <- 6*X1+7*X2+8*X3) On Wed, Nov 30, 2011 at 9:59 AM, David L Carlson wrote: > Isn't this even easier? > >> X1 <- c(1:3) >> X2 <- c(3, 4, 6) >> X3 <- c(5, 6, 1) >> Y <- 6*X1 + 7*X2 + 8*X3 >> Y > [1] 67 88 68 > > Or if yo

Re: [R] Parameters setting in functions optimization

2011-11-30 Thread Florent D.
better (smaller) than you first (bad) usage. On Wed, Nov 30, 2011 at 4:16 AM, Diane Bailleul wrote: > Le 11/30/2011 2:09 AM, Florent D. a écrit : > > Thanks for your answer ! > >> I also think your last write-up for LogLiketot (using a single >> argument "par&q

Re: [R] Parameters setting in functions optimization

2011-11-29 Thread Florent D.
Oh, and your message: In log(LikeGi(l, i, par[1], par[2])) : NaNs produced means your LikeGi is returning something negative. Can't take the log of it... On Tue, Nov 29, 2011 at 8:09 PM, Florent D. wrote: > I also think your last write-up for LogLiketot (using a single > argumen

Re: [R] Parameters setting in functions optimization

2011-11-29 Thread Florent D.
I also think your last write-up for LogLiketot (using a single argument "par") is the correct approach if you want to feed it to optim(). So now you have a problem with log(LikeGi(l, i, par[1], par[2])) for some values of par[1] and par[2]. Where is LikeGi coming from? a package or is it your ow

Re: [R] Comparing data

2011-11-28 Thread Florent D.
Forgot to suggest: if labSt is the 1st column in your file, you could just add "row.names = 1" to your read.csv call. On Mon, Nov 28, 2011 at 8:06 PM, Florent D. wrote: > Unlike a data.frame, a matrix can only hold one type of data. Since > you have a column of characters (

Re: [R] Comparing data

2011-11-28 Thread Florent D.
Unlike a data.frame, a matrix can only hold one type of data. Since you have a column of characters ("labSt") in your original data, turning it into a matrix will give you a matrix of characters. You can check it is the case by asking class(DF.m[1,1]). So you'll have to remove this labSt column be

Re: [R] Rprofile.site

2011-11-27 Thread Florent D.
source("c:\path\... ") On Sun, Nov 27, 2011 at 9:28 PM, Yen, Steven T wrote: > Dear All > > I inserted a frequently used function (subroutine) right into my > Rprofile.site which allows me to run it each time-works great. However, > this approach is obviously suitable for a short function or a s

Re: [R] generating a vector of y_t = \sum_{i = 1}^t (alpha^i * x_{t - i + 1})

2011-11-27 Thread Florent D.
1)[491:500] >  [1] 482 483 484 485 486 487 488 489 490 491 > -- > David. > > >> Michael >> >> On Nov 27, 2011, at 9:05 AM, Michael Kao wrote: >> >>> Hi Florent, >>> >>> That is great, I was working on solving the recurrence equation a

Re: [R] generating a vector of y_t = \sum_{i = 1}^t (alpha^i * x_{t - i + 1})

2011-11-27 Thread Florent D.
You can make things a lot faster by using the recurrence equation y[n] = alpha * (y[n-1]+x[n]) loopRec <- function(x, alpha){ n <- length(x) y <- numeric(n) if (n == 0) return(y) y[1] <- alpha * x[1] for(i in seq_len(n)[-1]){ y[i] <- alpha * (y[i-1] + x[i]) } return(y)

Re: [R] functions on rows or columns of two (or more) arrays

2011-08-04 Thread Florent D.
The apply function also works with multi-dimensional arrays, I think this is what you want to achieve using a 3d array: aaa <- array(NA, dim = c(2, dim(a))) aaa[1,,] <- a aaa[2,,] <- a2 apply(aaa, 3, function(x)lm(x[1,]~x[2,])) __ R-help@r-project.org m

Re: [R] Error message for MCC

2011-08-03 Thread Florent D.
You defined temp as a vector: > temp <- vector(mode="numeric", length = vl) but you try to extract from it as if it was a 2d object: colA <- temp[,compareA] Maybe you meant to use temp1 instead? More meaningful variable names might help avoid such mistakes. On Wed, Aug 3, 2011 at 6:14 PM, Matt