Re: [R] A %nin% operator?
See Harrell's Hmisc package -- David Huffer, Ph.D. Deputy Director CSOSA/ORE Washington, DC -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Ken Williams Sent: Thursday, August 05, 2010 11:20 AM To: r-help@r-project.org Subject: [R] A %nin% operator? Sometimes I write code like this: qf.a - subset(qf, pubid %in% c(104, 106, 107, 108)) qf.b - subset(qf, !pubid %in% c(104, 106, 107, 108)) and I get a little worried that maybe I've remembered the precedence rules wrong, so I change it to qf.a - subset(qf, pubid %in% c(104, 106, 107, 108)) qf.b - subset(qf, !(pubid %in% c(104, 106, 107, 108))) and pretty soon my code looks like fingernail clippings (or Lisp) and I'm thinking about precedence rather than my original task. So I write a %nin% operator which I define as: `%nin%` - function (x, table) match(x, table, nomatch = 0L) == 0L and then I'm happy again. I wonder, would something like this find a home in core R? Or is that too much syntactic sugar for your taste? -- Ken Williams Sr. Research Scientist Thomson Reuters Phone: 651-848-7712 ken.willi...@thomsonreuters.com __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] write file to date-stamped folder
How about: test.table - matrix ( rnorm ( 25 ) , ncol = 5 ) outputDir = paste ( + getwd ( ) + , /OutputData- + , Sys.Date ( ) + , sep = + ) dir.create ( outputDir ) write.table ( + test.table + , paste ( + outputDir + , test.table.txt + , sep = / + ) + , sep = \t + ) -- David -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of suzylee Sent: Monday, August 31, 2009 3:45 AM To: r-help@r-project.org Subject: [R] write file to date-stamped folder Hello, I would like to be able to write all files produced on one day to an output directory with that date stamp, or alternatively stamp the date in the filename. So that if i run the same code the next day the files will not be overwritten. here's what i have to start with: baseDir = getwd() outputDir = paste(baseDir,/OutputData-, Sys.Date(),sep=) and lets say i want to write the table test.table to a file: write.table(test.table, test.table.txt, sep=\t) How do i make this write to outputDir? Thanks. -- View this message in context: http://www.nabble.com/write-file-to-date-stamped-folder-tp25219504p25219 504.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] changing equal values on matrix by same random number
You want to replace all 1s each with the same random nuumber from the uniform distribution, then all 2s each with the same random nuumber from the uniform distribution, and so forth? Are the arguments (i.e., the min and max of the distribution) to each call to runif identical? -- David - David Huffer, Ph.D. Senior Statistician CSOSA/Washington, DC david.huf...@csosa.gov - -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of milton ruser Sent: Wednesday, August 26, 2009 12:54 PM To: r-help@r-project.org Subject: [R] changing equal values on matrix by same random number Dear all, I have about 30,000 matrix (512x512), with values from 1 to N. Each value on a matrix represent a habitat patch on my matrix (i.e. my landscape). Non-habitat are stored as ZERO. No I need to change each 1-to-N values for the same random number. Just supose my matrix is: mymat-matrix(c(1,1,1,0,0,0,0,0,0,0,0, 0,0,0,0,2,2,2,0,0,0,0, 0,0,0,0,2,2,2,0,0,0,0, 3,3,0,0,0,0,0,0,0,4,4, 3,3,0,0,0,0,0,0,0,0,0), nrow=5) I would like that all cells with 1 come to be runif(1,min=0.4, max=0.7), and cells with 2 be replace by another runif(...). I can do it using for(), but it is very time expensive. Any help are welcome. cheers milton [[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. __ 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] changing equal values on matrix by same random number
You could try either of the two examples below. They both assume that min and max are invariant: mymat - matrix ( c ( 1 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 2 , 2 , 2 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 2 , 2 , 2 , 0 , 0 , 0 , 0 , 3 , 3 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 4 , 4 , 3 , 3 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ) , nrow = 5 ) ## this way gives same random values for ## each integer between 0 and ## max (mymat): milton - function ( x , min = 0.4 , max = 0.7 ) { .rep - runif ( n = max ( x ) , min = min , max = max ) for ( i in 1:max ( x ) ) { x[x == i] - .rep[i] } x } milton ( x = mymat ) ## this way gives different random values ## for each integer between ## 0 and max (mymat): milton - function ( x , min = 0.4 , max = 0.7 ) { for ( i in 1:max ( x ) ) { x [ x == i ] - runif ( sum ( x == i ) , min = min , max = max ) } x } milton ( x = mymat ) -- David - David Huffer, Ph.D. Senior Statistician CSOSA/Washington, DC david.huf...@csosa.gov - -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of milton ruser Sent: Wednesday, August 26, 2009 1:18 PM To: David Winsemius Cc: r-help@r-project.org Subject: Re: [R] changing equal values on matrix by same random number Hi David, Thanks for the reply. This is what I need: mymat[mymat==1] - runif(1,min=0.4,max=0.7) mymat [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [1,] 0.457316100200000 3 0 [2,] 0.457316100202000 0 0 [3,] 0.457316100202004 0 0 [4,] 0.00000002304 0 0 [5,] 0.00000000303 0 0 But as my real landscapes have values from 1 to large number (~10,), so I think that if I put this on a for() looping it will be very time expensive, and as I have a lot of landscapes, I need to speed up it. Any suggestion? bests milton On Wed, Aug 26, 2009 at 1:12 PM, David Winsemius dwinsem...@comcast.netwrote: On Aug 26, 2009, at 12:53 PM, milton ruser wrote: Dear all, I have about 30,000 matrix (512x512), with values from 1 to N. Each value on a matrix represent a habitat patch on my matrix (i.e. my landscape). Non-habitat are stored as ZERO. No I need to change each 1-to-N values for the same random number. Just supose my matrix is: mymat-matrix(c(1,1,1,0,0,0,0,0,0,0,0, 0,0,0,0,2,2,2,0,0,0,0, 0,0,0,0,2,2,2,0,0,0,0, 3,3,0,0,0,0,0,0,0,4,4, 3,3,0,0,0,0,0,0,0,0,0), nrow=5) I would like that all cells with 1 come to be runif(1,min=0.4, max=0.7), and cells with 2 be replace by another runif(...). First the wrong way and then the right way: mymat[mymat==1] - runif(1,min=0.4,max=0.7) mymat [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [1,] 0.457316100200000 3 0 [2,] 0.457316100202000 0 0 [3,] 0.457316100202004 0 0 [4,] 0.00000002304 0 0 [5,] 0.00000000303 0 0 All the values are the same, clearly not what was desired. Put it back to your starting point: mymat-matrix(c(1,1,1,0,0,0,0,0,0,0,0, + 0,0,0,0,2,2,2,0,0,0,0, + 0,0,0,0,2,2,2,0,0,0,0, + 3,3,0,0,0,0,0,0,0,4,4, + 3,3,0,0,0,0,0,0,0,0,0), nrow=5) # So supply the proper number of random realizations: mymat[mymat==1] - runif(sum(mymat==1),min=0.4,max=0.7) mymat [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [1,] 0.574566500200000 3 0 [2,] 0.695641800202000 0 0 [3,] 0.693546600202004 0 0 [4,] 0.00000002304 0 0 [5,] 0.00000000303 0 0 If you want to supply a matrix of max and min values for the other integers there would probably be an *apply approach that could be used. I can do it using for(), but it is very time expensive. Any help are welcome. cheers David Winsemius, MD Heritage Laboratories West Hartford, CT [[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. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http
[R] Adding logical vectors
When adding several logical vectors I expect each vector will be coerced to integers and these vectors will then be added. That doesn't always seem to be the case. For example: ( f1 - as.factor ( sample ( x , 25 , rep = T ) ) ) [1] x x x x x x x x x x x x x x x x x x x x x x x x x Levels: x ( f2 - as.factor ( sample ( y , 25 , rep = T ) ) ) [1] y y y y y y y y y y y y y y y y y y y y y y y y y Levels: y ( f3 - as.factor ( sample ( z , 25 , rep = T ) ) ) [1] z z z z z z z z z z z z z z z z z z z z z z z z z Levels: z is.na ( f1 [ sample ( 1:25 , 4 ) ] ) - + is.na ( f2 [ sample ( 1:25 , 4 ) ] ) - + is.na ( f3 [ sample ( 1:25 , 4 ) ] ) - TRUE ## this returns a numeric vector: is.na ( f1 ) + is.na ( f2 ) + is.na ( f3 ) [1] 0 0 0 0 0 1 1 0 0 0 1 0 0 1 0 0 1 0 1 2 2 0 1 0 1 ## but this returns a logical vector !is.na ( f1 ) + !is.na ( f2 ) + !is.na ( f3 ) [1] TRUE TRUE TRUE TRUE TRUE TRUE FALSE TRUE [9] TRUE TRUE TRUE TRUE TRUE FALSE TRUE TRUE [17] FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE [25] FALSE Can someone please explain why the returned value is a logical vector when I use the not operator but a numeric vector when I don't. What is special about the !is.na? it returns an object of class logical just like the is.na function: all.equal ( class ( !is.na ( f1 ) ) , class ( is.na ( f1 ) ) ) [1] TRUE Thanks! __ 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] if confusion
Because the line if... Is syntactically complete. Move the else statement to the line above it like tclass - Testing 1 2 3 if ( tclass == Testing 1 2 3 ) { cat ( Testing , tclass , \n ) } else { cat ( tclass , \n ) } david -- David - David Huffer, Ph.D. Senior Statistician CSOSA/Washington, DC david.huf...@csosa.gov - -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of rkevinbur...@charter.net Sent: Monday, August 03, 2009 4:41 PM To: r-help@r-project.org Subject: [R] if confusion Simple question: Why doesn't the following work? Or what 'R' rule am I missing? tclass - Testing 1 2 3 if(tclass == Testing 1 2 3) { cat(Testing, tclass, \n) } else { cat(tclass, \n) } I get an error 'else' is unexpected. Thank you. Kevin __ 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] time difference
On Wednesday, July 22, 2009 10:44 AM, Erin Hodgess wrote: ...I am looking at the ctime attribute of two different files. It contains the year, month, day, time of creation and time zone. Is there a way to determine the difference between the ctimes of two files, please... are you looking for something different from file1 - file.info ( '~/Desktop/file1.txt' ) $ ctime file1 [1] 2009-07-20 16:35:17 EDT file2 - file.info ( '~/Desktop/file2.txt' ) $ ctime file2 [1] 2009-02-12 18:58:47 EST file1 - file2 Time difference of 157.8587 days David -- David - David Huffer, Ph.D. Senior Statistician CSOSA/Washington, DC david.huf...@csosa.gov __ 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] assign question
How about: sapply ( 1:27 , function ( i ) { min ( get ( paste ( sa , i , sep = ) ) ) } ) See ?get david -- David - David Huffer, Ph.D. Senior Statistician CSOSA/Washington, DC david.huf...@csosa.gov - -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Erin Hodgess Sent: Monday, July 20, 2009 2:26 PM To: R help Subject: [R] assign question Dear R People: I have several vectors, sa1, sa2,...sa27 of varying lengths. I want to produce one vector xener[1:27] which has the minimum of each sa[i]. I'm trying to set up a loop and use the assign statement, but here are my results: for(i in 1:27) { + xener[i] - min(assign(paste(sa,i,sep=))) + } Error in assign(paste(sa, i, sep = )) : element 2 is empty; the part of the args list of '.Internal' being evaluated was: (x, value, envir, inherits) Any suggestions would be most welcome. Thanks in advance, Erin -- Erin Hodgess Associate Professor Department of Computer and Mathematical Sciences University of Houston - Downtown mailto: erinm.hodg...@gmail.com __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Finding missing elements by comparing vectors
Or... X = c (red, blue, green, black ) ; Y = c(red, blue, green, magenta, cyan) unique ( c ( X [X %in% Y] , Y [Y %in% X] ) ) [1] red blue green David -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Praveen Surendran Sent: Thursday, July 16, 2009 11:42 AM To: r-help@r-project.org Subject: [R] Finding missing elements by comparing vectors Hi, Is there a function in R to do a-b where a and b are two non-numeric sets (or intersection complement of these two sets). Kind Regards, Praveen. [[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. __ 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] Problems with lists...
Like, a = list (1:3,4:6,7:9) a [[1]] [1] 1 2 3 [[2]] [1] 4 5 6 [[3]] [1] 7 8 9 a [[2]] [2] [1] 5 a [[3]] [3] [1] 9 HTH -- David -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of voidobscura Sent: Thursday, July 16, 2009 12:11 PM To: r-help@r-project.org Subject: [R] Problems with lists... a [[1]] [1] 1 2 3 [[2]] [1] 4 5 6 [[3]] [1] 7 8 9 I need to access individual elements, such as the 5 or the 9. Can anyone please tell me the syntax to do this? tia -- View this message in context: http://www.nabble.com/Problems-with-lists...-tp24519517p24519517.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] DataFrame help
The easiest way is to just do something like this: mdat - matrix(c(4,2,3, 11,12,13), nrow = 2, ncol=3) mdat [,1] [,2] [,3] [1,]43 12 [2,]2 11 13 as.vector ( colSums ( mdat ) ) [1] 6 14 25 HTH -- David -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of voidobscura Sent: Thursday, July 16, 2009 2:26 PM To: r-help@r-project.org Subject: [R] DataFrame help Alright, so I am trying to write my own function to calculate column sums in a matrix. I want the result as a single list with the values. So far I have: csum-function(m) { a = data.frame(m) s = lapply(a,sum) return(s) } What is the easiest way to have it return in a format such as [1] 6 15 24 ? Thanks. -- View this message in context: http://www.nabble.com/DataFrame-help-tp24521881p24521881.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Transformation of data!
I'm guessing you want to perform some sort of transformation on all the elements in the matrix you've posted and that you've only presented those 3 elements as an example of how the transformation will affect those 3 elements. Is that right? -- David -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Andriy Fetsun Sent: Thursday, July 16, 2009 4:10 PM To: r-help-requ...@r-project.org; r-help@r-project.org Subject: [R] Transformation of data! Hi Colleagues, Could you please help? I get as the output of my calculations following [1] 0.00e+00 1.89e-04 3.933000e-05 1.701501e-04 2.040456e-04 [6] 3.119242e-04 2.545665e-04 1.893930e-03 1.303112e-03 9.880183e-04 [11] 1.504378e-03 1.549246e-03 5.877690e-04 4.771359e-04 8.528219e-04 How is it possible to transform the data to get a vector as following 10 0.017511063 11 0.017819918 12 0.017944472 Thank you in advance! -- Best regards, Andy [[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. __ 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 merging matrices
On Wednesday, July 15, 2009 8:28 AM, jurgen claesen wrote: ...I'm a relative new user of R and I have a problem with merging a collection of matrices. All matrices in this collection have the same dimension (532 rows and 532 columns), but can differ in the row and columns names. I'd like to merge these matrices in such a way that the resulting matrix only contains unique row- and column-names and that in case of a match between the row or column names the smallest value is retained As an example says more: A1-matrix(c(1:9), ncol=3, byrow=TRUE) rownames(A1)-colnames(A1)-c(a,b,c) a b c a 1 2 3 b 4 5 6 c 7 8 9 A2-matrix(c(7,1,3,10,2,7,3,1,8),ncol=3,byrow=TRUE) rownames(A2)-colnames(A2)-c(a,y,z) a y z a 7 1 3 y 10 2 7 z 3 1 8 I want something like this to be returned: a b c y z a 1 2 3 1 3 b 4 5 6 NA NA c 7 8 9 NA NA y 10 NA NA 5 6 z 3 NA NA 8 9 Two questions (a) how do you want to decide which of the elements gets dropped out---like the value 7 in A2 [1,1] above and (b) what goes in the bottom corner of the new matrix? I would have guessed i would have seen 2 7 1 8 in the corner. Was that a typo or do you really want to overwrite the values of the submatrix that *is* in A1? david -- David - David Huffer, Ph.D. Senior Statistician CSOSA/Washington, DC david.huf...@csosa.gov __ 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 merging matrices
Jurgen, I obviously didn't read the text of yours that I quoted (viz., and that in case of a match between the row or column names the smallest value is retained). My apologies. David -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of David Huffer Sent: Wednesday, July 15, 2009 11:07 AM To: jurgen claesen; r-help@r-project.org Subject: Re: [R] problem with merging matrices On Wednesday, July 15, 2009 8:28 AM, jurgen claesen wrote: ...I'm a relative new user of R and I have a problem with merging a collection of matrices. All matrices in this collection have the same dimension (532 rows and 532 columns), but can differ in the row and columns names. I'd like to merge these matrices in such a way that the resulting matrix only contains unique row- and column-names and that in case of a match between the row or column names the smallest value is retained As an example says more: A1-matrix(c(1:9), ncol=3, byrow=TRUE) rownames(A1)-colnames(A1)-c(a,b,c) a b c a 1 2 3 b 4 5 6 c 7 8 9 A2-matrix(c(7,1,3,10,2,7,3,1,8),ncol=3,byrow=TRUE) rownames(A2)-colnames(A2)-c(a,y,z) a y z a 7 1 3 y 10 2 7 z 3 1 8 I want something like this to be returned: a b c y z a 1 2 3 1 3 b 4 5 6 NA NA c 7 8 9 NA NA y 10 NA NA 5 6 z 3 NA NA 8 9 Two questions (a) how do you want to decide which of the elements gets dropped out---like the value 7 in A2 [1,1] above and (b) what goes in the bottom corner of the new matrix? I would have guessed i would have seen 2 7 1 8 in the corner. Was that a typo or do you really want to overwrite the values of the submatrix that *is* in A1? david -- David - David Huffer, Ph.D. Senior Statistician CSOSA/Washington, DC david.huf...@csosa.gov __ 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] Formatting a Table
Cvandy, is this close to what you need: printT - function ( .seq = seq ( 2 , 10 , 2 ) ) { + x - t ( sapply ( .seq , T , Lc ) ) + x - cbind ( + .seq + , rbind ( + format ( x [ 1 , ] * 100 ) + , format ( x [ -1 , ] , digits = 3 ) + ) + ) + dimnames ( x ) [[2]] - NULL + print ( x , quote = FALSE ) + } printT ( ) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] 2707580859095 [2,] 40.490 0.562 0.640 0.722 0.810 0.902 [3,] 60.398 0.475 0.562 0.657 0.762 0.876 [4,] 80.343 0.422 0.512 0.614 0.729 0.857 [5,] 10 0.306 0.385 0.477 0.583 0.705 0.843 Im not really sure what you mean by Line up the first row with the factors (decimal fractions). -- David - David Huffer, Ph.D. Senior Statistician CSOSA/Washington, DC david.huf...@csosa.gov - -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of cvandy Sent: Wednesday, July 08, 2009 9:21 AM To: r-help@r-project.org Subject: [R] Formatting a Table I've created a short program to print a table of learning curve factors. However, I cannot figure out how to format the table to: 1) Get rid of the [1]s in the first column and replace it with the values of N. 2) Line up the first row with the factors (decimal fractions). Thanks for any help. The complete program and output is as follows: Lc-seq(0.70,0.95,0.05) #Specify learning curves T-function(N,Lc) #Create a function to calc.time for Nth unit + { + N^(log(Lc,10)/log(2,10)) #Function + } for (N in seq(2,10,2)) + {if (N==2){print(T(N,Lc)*100)}else{print(T(N,Lc),digits=3)}} [1] 70 75 80 85 90 95 [1] 0.490 0.562 0.640 0.722 0.810 0.902 [1] 0.398 0.475 0.562 0.657 0.762 0.876 [1] 0.343 0.422 0.512 0.614 0.729 0.857 [1] 0.306 0.385 0.477 0.583 0.705 0.843 -- View this message in context: http://www.nabble.com/Formatting-a-Table-tp24391433p24391433.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] error: no such index at level 2
Godmar, I don't follow... q - list ( ) q [[ 105 ]] - as.numeric ( c ( 0 , 0 , 1 ) ) q [[ 104 ]] - as.numeric ( c ( 1 , 1 , 1 ) ) q [[ 10 ]] - as.integer ( c ( 3 , 3 , 1 ) ) crossRsorted - data.frame ( i = c ( 105 , 104 , 10 ) ) q [ crossRsorted [ , 1 ] ] [[1]] [1] 0 0 1 [[2]] [1] 1 1 1 [[3]] [1] 3 3 1 length ( q [ crossRsorted [ , 1 ] ] ) [1] 3 How'd you come up with length(q) [1] 165 length(q[ crossRsorted[,1] ]) [1] 15750 I must be missing something. -- David - David Huffer, Ph.D. Senior Statistician CSOSA/Washington, DC david.huf...@csosa.gov - -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Godmar Back Sent: Wednesday, July 08, 2009 9:58 AM To: Henrique Dallazuanna Cc: r-help@r-project.org; Petr PIKAL Subject: Re: [R] error: no such index at level 2 On Wed, Jul 8, 2009 at 9:40 AM, Henrique Dallazuanna www...@gmail.comwrote: Its because '[[' accept only element, so you need use '[': q[crossRsorted[,1]] This appears to be doing something different. For instance, my 'q' has 165 components, but what you suggest has 15750: length(q) [1] 165 length(q[ crossRsorted[,1] ]) [1] 15750 hardly what I want. Meanwhile, it looks as though [[ ]] does not vectorize its arguments, it curries them! Note that: q[[c(105,104)]] Error in q[[c(105, 104)]] : subscript out of bounds gives the same error as: q[[105]][[104]] Error in q[[105]][[104]] : subscript out of bounds Very mysterious, though, in all fairness, explained in help([[) where it says: '[[' can be applied recursively to lists, so that if the single index 'i' is a vector of length 'p', 'alist[[i]]' is equivalent to 'alist[[i1]]...[[ip]]' providing all but the final indexing results in a list. which leads to square one: how to express select all r[i] where q[[i]] fulfills some predicate? - Godmar [[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. -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Godmar Back Sent: Wednesday, July 08, 2009 9:58 AM To: Henrique Dallazuanna Cc: r-help@r-project.org; Petr PIKAL Subject: Re: [R] error: no such index at level 2 On Wed, Jul 8, 2009 at 9:40 AM, Henrique Dallazuanna www...@gmail.comwrote: Its because '[[' accept only element, so you need use '[': q[crossRsorted[,1]] This appears to be doing something different. For instance, my 'q' has 165 components, but what you suggest has 15750: length(q) [1] 165 length(q[ crossRsorted[,1] ]) [1] 15750 hardly what I want. Meanwhile, it looks as though [[ ]] does not vectorize its arguments, it curries them! Note that: q[[c(105,104)]] Error in q[[c(105, 104)]] : subscript out of bounds gives the same error as: q[[105]][[104]] Error in q[[105]][[104]] : subscript out of bounds Very mysterious, though, in all fairness, explained in help([[) where it says: '[[' can be applied recursively to lists, so that if the single index 'i' is a vector of length 'p', 'alist[[i]]' is equivalent to 'alist[[i1]]...[[ip]]' providing all but the final indexing results in a list. which leads to square one: how to express select all r[i] where q[[i]] fulfills some predicate? - Godmar [[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. __ 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 each row
Something like this? dataframeA - data.frame ( + unique.id= c(1,1,3,3,3,5,7,7, 9) + , x1=rnorm(9) + , x2=rnorm(9) + , x3=rnorm(9) + ) dataframeB - data.frame ( + unique.id= c(2,3,4,5,5,5,6,7,9,10,10) + , x4=rnorm(11) + , x5=rnorm(11) + , x6=rnorm(11) + ) match.counts - function ( x , y ) { + out - cbind ( + table ( x [ which ( x %in% y ) ] ) + , table ( y [ which ( y %in% x ) ] ) + ) + dimnames ( out ) [[2]] - c ( N in x , N in y ) + out + } match.counts ( dataframeA$unique.id , dataframeB$unique.id ) N in x N in y 3 3 1 5 1 3 7 2 1 9 1 1 -- David - David Huffer, Ph.D. Senior Statistician CSOSA/Washington, DC david.huf...@csosa.gov - -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of tathta Sent: Wednesday, July 08, 2009 11:10 AM To: r-help@r-project.org Subject: [R] matching each row I have two dataframes, the first column of each dataframe is a unique id number (the rest of the columns are data variables). I would like to figure out how many times each id number appears in each dataframe. So far I can use: length( match (dataframeA$unique.id[1], dataframeB$unique.id) ) but this only works on each row of dataframe A one-at-a-time. I would like to do this for all of the rows in dataframe A, and then put the results in a new variable: dataframeA$count I'm new to R, so please be patient with me! Sorry if this question has already been answered, my search of the archives only brought up one relevant post, and I didn't understand the answer to it http://www.nabble.com/match-to20799206.html#a20799206 thx -- View this message in context: http://www.nabble.com/matching-each-row-tp24393051p24393051.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] how to count number of elements in a vector that are not NA ?
How about countN - function ( v ) { sum ( !is.na ( v ) ) - sum ( is.na ( v ) ) } -- David - David Huffer, Ph.D. Senior Statistician CSOSA/Washington, DC david.huf...@csosa.gov - -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Godmar Back Sent: Tuesday, July 07, 2009 2:57 PM To: R-help@r-project.org Subject: [R] how to count number of elements in a vector that are not NA ? Hi, is there a simpler way to count the number of elements in a vector that are not NA than this: countN - function (v) { return (Reduce(function (x, y) x + y, ifelse(is.na(v), 0, 1))) } ? - Godmar __ 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] how to count number of elements in a vector that are not NA ?
On Tuesday, July 07, 2009 3:20 PM, Godmar Back wrote: ...That would be wrong, wouldn't it, if the other replies are correct Yes. It was wrong. This isn't: countN - function ( v ) { length ( v ) - sum ( is.na ( v ) ) } But there are really tons of ways to do it. Even your way is not wrong... -- David - David Huffer, Ph.D. Senior Statistician CSOSA/Washington, DC david.huf...@csosa.gov __ 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] Sweave: multiline Sexpr?
Is there any way to have Sexpr span multiple lines? -- David - David Huffer, Ph.D. Senior Statistician CSOSA/Washington, DC david.huf...@csosa.gov - __ 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] Sweave: multiline Sexpr?
Duncan, it's the first example you provide that I'm looking to do. These Sexpr quickly exceed 80 columns and I was wanting to make them more readable in the latex code by blocking and indenting the R code. Dieter suggests putting the R code into a code block then Sexpr-ing that object. That seems like the way to go, but it seems not to take full advantage of the language. Thank you both. David -Original Message- From: Duncan Murdoch [mailto:murd...@stats.uwo.ca] Sent: Tuesday, June 30, 2009 1:16 PM To: David Huffer Cc: r-help@r-project.org Subject: Re: [R] Sweave: multiline Sexpr? On 30/06/2009 12:44 PM, David Huffer wrote: Is there any way to have Sexpr span multiple lines? For input or output? I.e. do you want \Sexpr{x + y} or do you want the value to display on multiple lines? I think you can't do the first. To do the second, returning a character value with an embedded newline should work. For example, echo=FALSE= string - this goes on \n two lines @ \Sexpr{string} (I think you really need all those escapes to get two backslashes in the output!) I don't know if it will happen for 2.10.x, but I would like to extend \Sexpr to be just as flexible as the = notation, by putting the Sweave options in a Latex-like option: \Sexpr[fig=true]{ plot(rnorm(1000))} You can do this now in R-devel in the new Sweave-like syntax in Rd files (except they don't support figs yet), but not yet in the original. Duncan Murdoch __ 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] SAS Macro Variable in R
On Tuesday, June 23, 2009 1:40 PM, David Young wrote: ...Hi I'm new to R and would like to implement a SAS-like macro variable in R. What I'd like to do is take the simple R code below and change the =TEF to different letters to refer to different companies' data for download... # DOWNLOADS FILES FROM YAHOO INTERNET download.file('http://ichart.yahoo.com/table.csv?s=TEF.MC a=00b=1c=2003d=05e=23f=2009g=dignore=.csv', 'c:\\projects\\stock data\\data\\test.csv',quiet=TRUE) ...As you can see the text I want to change is within the quoted Internet address. Is this possible in R... How about something like: download.ichart - function ( symbol , destpath = 'c:/projects/stock data/data/' ) { url - paste ( http://ichart.yahoo.com/table.csv?s=; , substitute ( symbol ) , .MCa=00b=1c=2003d=05e=23f=2009g=dignore=.csv , sep = ) destfile - paste ( destpath , substitute ( symbol ) , .csv , sep = ) download.file ( url , destfile , quiet = TRUE ) } download.ichart ( TEF ) -- David - David Huffer, Ph.D. Senior Statistician CSOSA/Washington, DC david.huf...@csosa.gov __ 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] IP-Address
On Wednesday, June 17, 2009 3:33 PM, edwinedw...@web.de wrote: Sorry, David has just told my that it was a mistake in my example (Thanks David). I had a wrong idea. The right idea is: make a ip range, when the number increament without an gap (and with maximum number: 255, see example down). In case my initial example would be: 162.131.58.1 162.131.58.2 162.131.58.3 162.131.58.4 162.131.58.5 162.131.58.6 The Range is: 162.131.58.1 - 162.131.58.6 162.132.58.20 162.132.58.20 (no range) 162.252.20.21 162.252.20.21 (no range) 162.254.20.22 162.254.20.22 (no range) 163.253.7.23 163.253.7.23 (no range) 163.253.20.25 163.253.20.25 (no range) 161.138.45.226 161.138.45.226 (no range) Another example: [...] Edwin, here's a function that does what you want. it probably doesn't return the ranges the way you'll need them, but you can play around with that part: iprange - function ( x ) { ip.set - x ip - do.call ( rbind , lapply ( ip.set , function ( x ) { as.numeric ( unlist ( strsplit ( as.character ( x ) , split = . , fixed = TRUE ) ) ) } ) ) ip - cbind ( ip , ip [ , 1 ] * 256^3 + ip [ , 2 ] * 256^2 + ip [ , 3 ] * 256 + ip [ , 4 ] ) ip.set - ip.set [ order ( ip [ , 5] ) ] ip - ip [ order ( ip [ , 5] ) , ] index.start - which ( c ( -Inf , diff ( ip [ , 5] ) ) != 1 ) index.end - c ( index.start [-1] - 1 , tail ( index.start , 1 ) ) iprange - cbind ( ip.set [ index.start ] , ifelse ( ip.set [ index.start ] == ip.set [ index.end ] , NA , ip.set [ index.end ] ) ) cat (ip addresses:\n) cat ( ip.set , sep = \n ) cat (\nip ranges:\n) cat ( paste ( ip.set [ index.start ] , ifelse ( ip.set [ index.start ] == ip.set [ index.end ] , (no range) , paste ( to , ip.set [ index.end ] ) ) ) , sep = \n ) invisible ( iprange ) } test - iprange ( c ( 162.131.58.1 , 163.253.7.23 , 162.131.58.2 , 163.253.20.25 , 162.131.58.3 , 161.138.45.226 , 162.131.58.4 , 169.131.58.1 , 162.131.58.5 , 169.131.58.2 , 162.131.58.6 , 169.132.58.3 , 162.132.58.20 , 250.131.58.4 , 162.252.20.21 , 250.131.58.5 , 162.254.20.22 , 250.131.58.7 ) ) test HTH -- David - David Huffer, Ph.D. Senior Statistician CSOSA/Washington, DC david.huf...@csosa.gov __ 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] by-group processing
On Thursday, May 07, 2009 7:45 PM, David Freedman wrote: ...how about: d=data[order(data$ID,-data$Type),] d[!duplicated(d$ID),] Does the -data$Type argument to the order function work? -- David - David Huffer, Ph.D. Senior Statistician CSOSA/Washington, DC david.huf...@csosa.gov __ 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] read SAS file
Do you have the foreign package loaded? -- David __ 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] Repository missing hmisc
I'm trying to install the Hmisc package, however it doesn't appear in the list after utils:::menuInstallPkgs(). Any help? -- David __ 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] replicating dataframe rows
On Monday, September 29, 2008 1:59, Dimitris Rizopoulos wrote: On Monday, September 29, 2008 1:26, milton ruser wrote: ...I have a data.frame like... place-c(place1, place2, place3, place4, place5) population-c(100,200,300,50,30) my.df-data.frame(cbind(place,population)) ...and I would like to expand my data.frame using population variable. So, for each line of my data.frame I would like that the new data.frame have many rows as the population collumn... ...try this... place - c(place1, place2, place3, place4, place5) population - c(100, 200, 300, 50, 30) my.df - data.frame(place, population) Or... place - c(place1, place2, place3, place4, place5) population - c(100, 200, 300, 50, 30) my.df - data.frame(place, population) my.df [ rep ( row.names ( my.df ) , as.numeric ( as.character ( my.df$population ) ) ) , ] -- David __ 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] Most common level of a factor by
I'm looking for something along the lines of which ( table ( x ) == max ( table ( x ) ) ) to find the most common level of one factor by several other factors. For instance, I've got X - data.frame ( + x = factor ( sample ( c ( A , B , C , D ) , 20 , r = T ) ) + , z1 = factor ( sample ( c ( Before , After ) , 20 , r = T ) ) + , z2 = factor ( sample ( c ( Red , Green , Blue ) , 20 , r = T ) ) + , z3 = factor ( sample ( 0:6 , 20 , r = T ) ) + ) X x z1z2 z3 1 D After Blue 0 2 D Before Green 3 3 A Before Red 5 4 C After Green 6 5 C Before Green 6 6 C Before Green 0 7 C Before Red 1 8 C Before Red 5 9 A Before Blue 3 10 A After Green 4 11 D After Red 3 12 C After Green 5 13 A After Red 0 14 B After Red 6 15 B Before Red 3 16 A Before Blue 4 17 B Before Blue 5 18 A After Blue 1 19 B Before Green 1 20 C Before Red 2 and i would like to be able to say which category of x was the most common for each combination of z1, z2, and z3. So, here, which category of x was the most common for Before,Red,0; Before,Red,1; ... Before,Red,6; Before,Green,0; Before,Green,1; ... Before,Green,6;... This seems simple rather as i type it out, but i havent been able to come up with the right approach so far. its friday night so maybe i should just go home and wait until monday... -- David __ 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] Rpart description of tree groups
I'm making a few functions to generate latex files describing rpart objects that are then \input-ed into a larger document. So far, the functions I have generate paragraphs containing enumerations of the predictors in pruned trees and the number of formed groups. Its easy enough to recover these. For instance, R print ( tree ) n= 878 node), split, n, loss, yval, (yprob) * denotes terminal node 1) root 878 110 Absent (0.8747153 0.1252847) 2) P3XMAR2=No 845 96 Absent (0.8863905 0.1136095) * 3) P3XMAR2=Yes 33 14 Absent (0.5757576 0.4242424) 6) PADV=Yes 7 0 Absent (1.000 0.000) * 7) PADV=No 26 12 Present (0.4615385 0.5384615) 14) ACPS=No 12 5 Absent (0.583 0.417) * 15) ACPS=Yes 14 5 Present (0.3571429 0.6428571) * R varsInTree - as.vector ( + tree $ frame $ var [ tree $ frame $ var %nin% leaf] # %nin% from Hmisc + ) R varsInTree [1] P3XMAR2 PADVACPS R nGroupsInTree - sum ( tree $ frame $ var == leaf ) R nGroupsInTree [1] 4 R What i havent been successful at so far is generating an enumerated list of the groups formed. I wanted to do something similar to the print.rpart but instead of enumerating nested lists I wanted a description of the terminal nodes. For instance, from the tree shown above i wanted to generate something like The resultant model separated sampled respondents into 4 groups. These groups (and predicted values) included respondents having \begin{inparaenum}[(a)] \item P3XMAR2=No (Absent); \item P3XMAR2=Yes and PADV=Yes (Absent); \item P3XMAR2=Yes and PADV=No and ACPS=No (Absent); and \item P3XMAR2=Yes and PADV=No and ACPS=Yes (Present); \end{inparaenum} So far, I've just generated placeholders for the \items and then i edit them by hand. Does anyone have advice on recovering the formed groups? -- David __ 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.