Re: [R] data frame vs. matrix

2014-03-17 Thread Göran Broström
m all to a common type (often character), so it may give you the wrong result in addition to being unnecessarily slow. Bill Dunlap TIBCO Software wdunlap tibco.com -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Duncan Murdoch Sent: Sunda

Re: [R] data frame vs. matrix

2014-03-17 Thread Göran Broström
On 2014-03-17 01:31, Jeff Newmiller wrote: Did you really intend to make all of the x values the same? Not at all; the code in the loop was in fact just nonsense. The point was to illustrate the huge difference in execution time. And that the relative difference seems to increase fast with th

Re: [R] data frame vs. matrix

2014-03-17 Thread Göran Broström
On 2014-03-16 23:56, Duncan Murdoch wrote: On 14-03-16 2:57 PM, Göran Broström wrote: I have always known that "matrices are faster than data frames", for instance this function: dumkoll <- function(n = 1000, df = TRUE){ dfr <- data.frame(x = rnorm(n), y = rnorm(n)) if (df){

Re: [R] data frame vs. matrix

2014-03-16 Thread Jeff Newmiller
Did you really intend to make all of the x values the same? If so, try one line instead of the for loop: dfr$x[ 2:n ] <- dfr$x[ 1 ] If that was merely an error in your example, then you could use a different one-liner: dfr$x[ 2:n ] <- dfr$x[ seq.int( n-1 ) ] In either case, the speedup is con

Re: [R] data frame vs. matrix

2014-03-16 Thread William Dunlap
wdunlap tibco.com > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf > Of Duncan Murdoch > Sent: Sunday, March 16, 2014 3:56 PM > To: Göran Broström; r-help@r-project.org > Subject: Re: [R] data frame v

Re: [R] data frame vs. matrix

2014-03-16 Thread Duncan Murdoch
On 14-03-16 2:57 PM, Göran Broström wrote: I have always known that "matrices are faster than data frames", for instance this function: dumkoll <- function(n = 1000, df = TRUE){ dfr <- data.frame(x = rnorm(n), y = rnorm(n)) if (df){ for (i in 2:NROW(dfr)){ if

Re: [R] data frame vs. matrix

2014-03-16 Thread Rui Barradas
Hello, This is to be expected. Matrices can hold only one type of data so the problem is solved once and for all, data frames can have many types of data so the code to handle them must determine which type to handle on every access. Hope this helps, Rui Barradas Em 16-03-2014 18:57, Göran

[R] data frame vs. matrix

2014-03-16 Thread Göran Broström
I have always known that "matrices are faster than data frames", for instance this function: dumkoll <- function(n = 1000, df = TRUE){ dfr <- data.frame(x = rnorm(n), y = rnorm(n)) if (df){ for (i in 2:NROW(dfr)){ if (!(i %% 100)) cat("i = ", i, "\n") dfr

Re: [R] Data frame vs matrix quirk: Hinky error message?

2012-05-01 Thread David Winsemius
On May 1, 2012, at 1:33 PM, Bert Gunter wrote: AdvisoRs: Is the following a bug, feature, hinky error message, or dumb Bert? mtest <- matrix(1:12,nr=4) dftest <- data.frame(mtest) ix <- cbind(1:2,2:3) mtest[ix] <- NA mtest [,1] [,2] [,3] [1,]1 NA9 [2,]26 NA [3,]3

Re: [R] Data frame vs matrix quirk: Hinky error message?

2012-05-01 Thread Rui Barradas
Hello, Bert Gunter wrote > > Duncan: > > Maybe there **is** a bug, then. > > > zmat <- matrix(1:12,nr=4) >> zdf <- data.frame(zmat) >> ix <- cbind(c(FALSE,TRUE),c(TRUE,TRUE)) >> zmat[ix] > [1] 2 3 4 6 7 8 10 11 12 >> zdf[ix] > [1] 2 3 4 6 7 8 10 11 12 >> zmat[ix] <- NA >> zmat >

Re: [R] Data frame vs matrix quirk: Hinky error message?

2012-05-01 Thread Rui Barradas
P.S. The way the logical matrix is constructed is NOT general purpose. Quoting myself quoting Bert, > > Actually, it works, as long as the logical index matrix has the same > dimensions as the data frame. > > zmat <- matrix(1:12,nr=4) > zdf <- data.frame(zmat) > > # Numeric index matrix. > ix <-

Re: [R] Data frame vs matrix quirk: Hinky error message?

2012-05-01 Thread Duncan Murdoch
nd Data Analysis Division Olympia, WA 98504-5204 -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- project.org] On Behalf Of Bert Gunter Sent: Tuesday, May 01, 2012 11:46 AM To: Duncan Murdoch Cc: r-help@r-project.org Subject: Re: [R] Data frame vs matrix q

Re: [R] Data frame vs matrix quirk: Hinky error message?

2012-05-01 Thread Bert Gunter
lympia, WA 98504-5204 > > >> -Original Message- >> From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- >> project.org] On Behalf Of Bert Gunter >> Sent: Tuesday, May 01, 2012 11:46 AM >> To: Duncan Murdoch >> Cc: r-help@r-project.org &

Re: [R] Data frame vs matrix quirk: Hinky error message?

2012-05-01 Thread Nordlund, Dan (DSHS/RDA)
Research and Data Analysis Division Olympia, WA 98504-5204 > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- > project.org] On Behalf Of Bert Gunter > Sent: Tuesday, May 01, 2012 11:46 AM > To: Duncan Murdoch > Cc: r-help@r-project.org &g

Re: [R] Data frame vs matrix quirk: Hinky error message?

2012-05-01 Thread Duncan Murdoch
On 01/05/2012 2:45 PM, Bert Gunter wrote: Duncan: Maybe there **is** a bug, then. > zmat<- matrix(1:12,nr=4) > zdf<- data.frame(zmat) > ix<- cbind(c(FALSE,TRUE),c(TRUE,TRUE)) > zmat[ix] [1] 2 3 4 6 7 8 10 11 12 > zdf[ix] [1] 2 3 4 6 7 8 10 11 12 > zmat[ix]<- NA > zmat

Re: [R] Data frame vs matrix quirk: Hinky error message?

2012-05-01 Thread David L Carlson
nal Message- > From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- > project.org] On Behalf Of Bert Gunter > Sent: Tuesday, May 01, 2012 1:46 PM > To: Duncan Murdoch > Cc: r-help@r-project.org > Subject: Re: [R] Data frame vs matrix quirk: Hinky error message?

Re: [R] Data frame vs matrix quirk: Hinky error message?

2012-05-01 Thread Bert Gunter
Duncan: Maybe there **is** a bug, then. > zmat <- matrix(1:12,nr=4) > zdf <- data.frame(zmat) > ix <- cbind(c(FALSE,TRUE),c(TRUE,TRUE)) > zmat[ix] [1] 2 3 4 6 7 8 10 11 12 > zdf[ix] [1] 2 3 4 6 7 8 10 11 12 > zmat[ix] <- NA > zmat [,1] [,2] [,3] [1,]159 [2,] NA N

Re: [R] Data frame vs matrix quirk: Hinky error message?

2012-05-01 Thread Duncan Murdoch
On 01/05/2012 2:12 PM, Bert Gunter wrote: Many thanks, Ista: I only looked in "].default" so the answer is: Alternative 4: dumb Bert. Rap knuckles with ruler. Actually, indexing by a logical matrix doesn't make much sense to me in either case, as it does not have the effect of selecting indivi

Re: [R] Data frame vs matrix quirk: Hinky error message?

2012-05-01 Thread Bert Gunter
Many thanks, Ista: I only looked in "].default" so the answer is: Alternative 4: dumb Bert. Rap knuckles with ruler. Actually, indexing by a logical matrix doesn't make much sense to me in either case, as it does not have the effect of selecting individual elements, which is what numeric matrix

Re: [R] Data frame vs matrix quirk: Hinky error message?

2012-05-01 Thread Ted Harding
On 01-May-2012 17:33:23 Bert Gunter wrote: > AdvisoRs: > > Is the following a bug, feature, hinky error message, or dumb Bert? > > mtest <- matrix(1:12,nr=4) > dftest <- data.frame(mtest) > ix <- cbind(1:2,2:3) > mtest[ix] <- NA > mtest > [,1] [,2] [,3] > [1,]1 NA9 > [2,]

Re: [R] Data frame vs matrix quirk: Hinky error message?

2012-05-01 Thread Ista Zahn
Hi Bert, The failure itself is the documented behavior: ?'[.data.frame' says "Matrix indexing ('x[i]' with a logical or a 2-column integer matrix 'i') using '[' is not recommended, and barely supported. For extraction, 'x' is first coerced to a matrix. For replacement, a logical m

[R] Data frame vs matrix quirk: Hinky error message?

2012-05-01 Thread Bert Gunter
AdvisoRs: Is the following a bug, feature, hinky error message, or dumb Bert? > mtest <- matrix(1:12,nr=4) > dftest <- data.frame(mtest) > ix <- cbind(1:2,2:3) > mtest[ix] <- NA > mtest [,1] [,2] [,3] [1,]1 NA9 [2,]26 NA [3,]37 11 [4,]48 12 ## But ...