Re: [R] Subtracting POSIXct data/times
See the help page for the difftime() function, which will tell you how to specify the units of the differences. (when you don't specify, it chooses the units according to some rules) -Don At 4:24 PM -0400 6/14/10, James Rome wrote: I have two dataframe columns of POXIXct data/times that include seconds. I got them into this format using for example zsort$ETA - as.POSIXct(as.character(zsort$ETA), format=%m/%d/%Y %H:%M:%S) My problem is that when I subtract the two columns, sometimes the difference is given in seconds, and sometimes it is given in minutes. I don't care which it is, but I need to know which one I will get. DateTimeETA 2010-05-16 02:19:56 2010-05-16 03:46:35 ... Browse[1] mins = zsort$ETA - zsort$DateTime Browse[1] mins Time differences in hours [1] 1.444167 2.685000 3.077222 3.210278 3.248056 3.281944 3.281944 3.360278 3.360278 3.582778 4.57 5.506111 5.857778 6.150278 6.150278 6.243056 6.243889 6.248056 6.248611 6.248611 6.356667 attr(,tzone) But sometimes the answer is in seconds. # make a column with the minutes before landing zsort$MinBeforeLand = zsort$ETA - zsort$DateTime zsort$MinBeforeLand Time differences in secs [1] -50 136 221 878 1192 2263 3296 3959 4968 5846 8709 11537 12198 12442 12642 15952 18273 19952 20538 How do I specify the resultant units? Thanks, Jim Rome __ 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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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 can I control the x-axis tick labels
Try: plot(, xaxt='n', ) axis(1, at=c(1,2,3,4,5, ... all the rest of them ...) ) You might also need to use the label argument to axis() -Don At 5:18 AM +0200 6/7/10, mau...@alice.it wrote: I am trying to generate a plot whose x-axis values are the following: data_out[,1] [1] 1979 1958 1937 1916 1895 1874 1853 1832 1811 1790 1769 1748 1727 1706 1685 1664 1643 1622 1601 1580 1559 [22] 1538 1517 1496 1475 1454 1433 1412 1391 1370 1349 1328 1307 1286 1265 1244 1223 1202 1181 1160 1139 1118 [43] 1097 1076 1055 1034 1013 992 971 950 929 908 887 866 845 824 803 782 761 740 719 698 677 [64] 656 635 614 593 572 551 530 509 488 467 446 425 404 383 362 341 320 299 278 257 236 [85] 215 194 173 152 131 110 89 68 47 26 25 24 23 22 21 20 19 18 17 16 15 [106] 14 13 12 11 10987654321 The y-axis values are: data_out[,2] [1] 1.01709 1.09454 1.16331 1.21673 1.26098 1.30297 1.33905 1.37466 1.40690 1.43407 1.46022 [12] 1.48497 1.50939 1.53382 1.55764 1.58068 1.60422 1.62692 1.64918 1.67210 1.69456 1.71653 [23] 1.73706 1.75634 1.77562 1.79505 1.81393 1.83259 1.85197 1.87105 1.88949 1.90792 1.92649 [34] 1.94479 1.96263 1.98029 1.99677 2.01212 2.02692 2.04183 2.05648 2.07071 2.08329 2.09510 [45] 2.10593 2.11636 2.12563 2.13423 2.14317 2.15116 2.15739 2.16426 2.17121 2.17796 2.18490 [56] 2.19103 2.19751 2.20357 2.21056 2.21642 2.22264 2.22861 2.23448 2.24007 2.24601 2.25113 [67] 2.25593 2.25842 2.26077 2.26183 2.26223 2.26224 2.26133 2.25868 2.25493 2.25130 2.24704 [78] 2.24123 2.23236 2.22251 2.20842 2.19076 2.17490 2.15480 2.13716 2.11967 2.10428 2.08815 [89] 2.07497 2.06104 2.05527 2.05412 2.06270 2.08828 2.09092 2.09158 2.09290 2.09495 2.09370 [100] 2.09703 2.10551 2.10771 2.10916 2.11557 2.12741 2.13782 2.14946 2.16577 2.18978 2.21518 [111] 2.25129 2.27978 2.32903 2.40820 2.50683 2.64452 3.06182 3.97841 35.81950 I cannot get my ploy start with the tick label 1. Surprisingly it keeps starting from 0 regardless of the usage of the graphic parameter xlim. I tried many different combinations of the parameters appearing in the plot instruction. I also tried to sort the x-axis value in increasing order. plot(sort(data_out[,1]),sort(data_out[,2],decreasing=T),xlim=c(1,1979),ylim=c(1,35),main=Intrinsic Dimensionality of 2D Swiss Roll,font.main=2,cex.main=1.5,col.main=red,xlab=Number of Nearest-Neighbors,ylab=Dimension,type=l,lab=c(10,10,3)) How can I get the x-axis tick labels to be (a subset of) the x-axis values, that is: 123456789 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 47 68 89 110 131 152 173 194 215 236 257 278 299 320 341 362 383 404 425 446 467 488 509 530 551 572 593 614 635 656 677 698 719 740 761 782 803 824 845 866 887 908 929 950 971 992 1013 1034 1055 1076 1097 1118 1139 1160 1181 1202 1223 1244 1265 1286 1307 1328 1349 1370 1391 1412 1433 1454 1475 1496 1517 1538 1559 1580 1601 1622 1643 1664 1685 1706 1727 1748 1769 1790 1811 1832 1853 1874 1895 1916 1937 1958 1979 Thank you in advance, Maura tutti i telefonini TIM! [[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. -- - Don MacQueen Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 m...@llnl.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] A primitive OO in R -- where next?
This is not really OO at all, in my opinion. It's an example of the amazing flexibility of the language. I'd like to add on to what Erik said, with an example: sum(1:10) [1] 55 foo - sum foo(1:10) [1] 55 junk - list(a=sum) junk$a(1:10) [1] 55 sum is an R object; it happens to be a function. When I do foo - sum I'm creating another R object. It's also a function, so I use it with the same syntax. When I do junk - list(a=sum) I'm creating another R object. It's a list, and its first element, named 'a', is a function. Since the element is a function, I use it just like any other function, i.e, follow its name with a pair of parentheses with arguments between them. Note that in the last example it doesn't matter what the other elements in the list, if any, are. I could just as well do junk - list( foo=data.frame(x=1:4), b=c('x','y'), dd=sum) Then junk$dd(1:10) junk$dd( junk$foo$x ) are valid statements. There's no connection between using 'junk' both inside the parentheses and outside. Since junk$dd is a function, you can supply it with any R object, and it doesn't matter where that R object comes from. I doubt that it's documented in the way you might be expecting. It's a result of the generality of list elements -- they can be any R object. Hope this helps. -Don At 10:48 PM +0100 5/12/10, Ted Harding wrote: Greetings All, Out of curiosity, I've just done a very primitive experiment: Obj - list(Fun=sum, Dat=c(1,2,3,4)) Obj$Fun(Obj$Dat) # [1] 10 That sort of thing (much more sophisticated) must be documented mind-blowingly somewhere. Where? Where I stand right now: The above (and its immediately obvious generalisations, like Obj$Fun-cos) is all I know about it so far. Ted. E-Mail: (Ted Harding) ted.hard...@manchester.ac.uk Fax-to-email: +44 (0)870 094 0861 Date: 12-May-10 Time: 22:48:14 -- XFMail -- __ 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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] / Operator not meaningful for factors
At 3:50 PM -0700 5/3/10, John Kane wrote: I think that you are correct. R has the annoying habit of converting character data to factors when you don't want it to while it is importing data. This is because the in the option stringsAsFactors is set to TRUE for some weird historical reasons. Well, annoying is in the eye of the beholder. The reason is not weird at all; the original S language, upon which R is based, was designed first for statistical analysis. When the language was expanded to include advanced modeling capabilities (linear models, generalized linear models, and more) it became apparent that factors are the appropriate form for using categorical data in such models. it is still the R Project for Statistical Computing (see the R home page), so the default is unchanged. Hence, when users get factors when they were expecting numbers, it's virtually always because the have some non-numeric character strings mixed in with the data. R then defaults to interpreting it as categorical data, represented as a factor. -Don -- - Don MacQueen Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 m...@llnl.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] Event History Data Recoding
The reshape() function would be used to reorganize your event history data set so that the Discussion and Agreement dates are one above the other in the same column, presumably with another variable to flag which is which (although you don't seem to need that). Then, separately, you read your information data into R, reorganize it into the same structure as the reshaped event history, and then append it to the reshaped event history (using the rbind() function), and finally, sort. That's my best guess given what you've shown. But there are puzzles, i.e., dates in the inputs that are not in the output, and dates in the output that are not in the input. Event=0 shows up in the output but not in the input. The information dataset doesn't have event numbers. Reading the information file into R and reorganizing into columns with the correct structure for appending looks tricky. -Don At 5:18 PM +0200 4/23/10, Thomas Jensen wrote: Thanks Josh, But I am not sure if the reshape function can create new rows based on the meeting variable. For the second act there are three meetings, i.e. one meeting between discussion and agreement, and this should be entered as a separate row. Best, Thomas On Fri, 2010-04-23 at 07:45 -0700, Joshua Wiley wrote: If you are talking about changing from a wide format to long take a look at ?reshape --- Josh On Fri, Apr 23, 2010 at 7:09 AM, Thomas Jensen thomas.jen...@eup.gess.ethz.ch wrote: Dear R list, I have an event history data set that is structured like this: Legislative act Discussion Agreement Time Event Act12006-05-30 2006-06-19 201 Act22004-03-01 2004-06-14 105 1 . . . I have information on the meetings in the legislature between adoption periods in a separate variable (the start and stop dates are included): Act1 meeting:2006-05-30, 2006-06-19. Act2 meeting: 2004-03-22, 2004-04-26, 2004-06-14. I want to code this as a discrete event data set, so it should look like this: Legislative act Event Meeting Time Count Act1 0 2006-05-30 1 Act1 1 2006-06-07 2 Act2 0 2004-03-22 1 Act2 0 2004-04-26 2 Act2 1 2004-06-14 3 Can anyone tell me if it is possible to do this recoding in R, or do I have to do it by hand? Best, Thomas __ 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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] Creating artificial environmental landscape with spatial autocorrelation
See the RandomFields package. -Don At 4:56 PM -0700 4/21/10, Laura S. wrote: Dear all: Does anyone have any suggestions on how to make a spatially explicit landscape with spatial autocorrelation in R? In other words, a landscape where all cells have a spatial reference, and the environment values that are closer in space are more similar (positive spatial autocorrelation). Thank you, Laura ...many places you would like to see are just off the map and many things you want to know are just out of sight or a little beyond your reach. But someday you will reach them all, for what you learn today, for no reason at all, will help you discover all the wonderful secrets of tomorrow. -Norton Juster The Phantom Tollbooth ~ Imagination is more important than knowledge. -Albert Einstein [[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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] User inputs
At 4:24 PM -0800 4/20/10, chrisli1223 wrote: Hi everyone, I have been searching for answers for the following questions but I don't have much success. The following questions may actually be quite simple. Any help would be greatly appreciated. (1) I have written a script which requires user input. I am using the readline() command.However, everytime when I run the script, R does not wait for the user input and proceed to the next line. Is there something like par(ask=T) to solve this problem? (2) In my script, I want it to stop running when a certain condition is met. I have tried using the stop() function, but apparently R only stops reading that line and start reading the following lines. I have also thought about quit() but it is not quite what I want. May someone please lead me to the right function please? Use if() ## beginning of script ## various R that always run if ( condition is met) { ## here is where we stop cat('My condition is met, I am stopping\n') } else { cat('My condition is not met, I am continuing\n') ## put here all the commands that should run when the condition is not met } (3) When a minor error happens, I would like to get the user permission by pressing the return key before the script continues to run. What function should I be looking at? Use the try() function to control what happens when an error happens Many thanks, Chris -- View this message in context: http://*n4.nabble.com/User-inputs-tp2018251p2018251.html Sent from the R help mailing list archive at Nabble.com. [[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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] format() method
Were you expecting 01, and is that why you are puzzled? See ?strftime and the explanation of the %U format. It depends on where the first Sunday of the year happens to fall. -Don At 5:28 AM -0800 4/16/10, kafkaz wrote: Hello, I use format() function to get number of the week, like this: format(tmp,'%U') Recently, I have spotted something bizarre. For example, I have such object: (index(tmp$x.delta['2009'][1:16])) [1] 2009-01-02 CET 2009-01-09 CET 2009-01-16 CET 2009-01-23 CET [5] 2009-01-30 CET 2009-02-06 CET 2009-02-13 CET 2009-02-20 CET [9] 2009-02-27 CET 2009-03-06 CET 2009-03-13 CET 2009-03-20 CET [13] 2009-03-27 CET 2009-04-03 CEST 2009-04-09 CEST 2009-04-17 CEST dput(index(tmp$x.delta['2009'][1:16]),'%U',file='as.date') structure(c(1230850800, 1231455600, 1232060400, 1232665200, 123327, 1233874800, 1234479600, 1235084400, 1235689200, 1236294000, 1236898800, 1237503600, 1238108400, 1238709600, 1239228000, 1239919200), tzone = structure(, .Names = TZ), class = c(POSIXt, POSIXct)) To get number of the week I run: format(index(tmp$x.delta['2009'][1:16]),'%U') Here is the output - the weird thing is, that the first number of the week is 00. [1] 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 [16] 15 Is it the bug, my mistake or it is supposed to by like that? Thank you, kafka -- View this message in context: http://*n4.nabble.com/format-method-tp1999753p1999753.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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] SSH Through R Script
When I do what you're describing, I get prompted for my password: system('ssh -l usernm rmthost') use...@rmthost's password: After I enter my password, nothing seems to happen. But if I hit ctrl-c then I get a command line prompt, and it turns out that it's a shell prompt on the remote host. I can issue standard unix commands, and they execute on the remote host. To get back into R on my local host I type 'exit'. Or: Here's a couple of lines from one of my scripts that might help ... cmd - 'ssh -l username remotehost /bin/lpstat -a' lprs - read.table(pipe(cmd),fill=TRUE,as.is=TRUE)[,1] I get prompted for my password when I source these lines on my local host. With the result that the lprs object has the names of printers available on the remote host. -Don At 10:01 PM -0800 4/8/10, afoo wrote: Hi, I am trying to SSH to a remote server through R script. In other words, I would like to know how I can get a SSH connection to the remote server and then execute commands on that server with the R script. So in bash, I would normally type ssh -lusername remoteserver.com; press enter and then wait for the password prompt to key in my password. I have tried system(ssh remoteserver.com) but that doesn't work because, from what I know, SSH requires user interactivity - I am required to key in my password. I tried looking up about putting password as a command line parameter, but SSH doesn't allow that, my only option then is to set up a private/public key pair. But the admin of the remoteserver doesn't allow me to do that. Is there a way in which I can SSH in? Or is there a command in R that allows me to interact with the command prompts interactively? thanks, afoo -- View this message in context: http://*n4.nabble.com/SSH-Through-R-Script-tp1809635p1809635.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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] help in attach function
You can also read the help page for the conflicts() function. And try a commend like find('acid') (or any of the objects listed as being masked) to find out where the two or more objects with the same name are located. Oh, and looking at ?search would be good also. -Don At 4:31 PM -0400 4/7/10, Steve Lianoglou wrote: Hi, On Wed, Apr 7, 2010 at 4:24 PM, Changbin Du changb...@gmail.com wrote: Hi, r-community, This morning, I MET the following problem several times when I try to attach the data set. When I closed the current console and reopen the R console, the problem disappear. BUt with the time passed on, the problem occurs again. Can anyone help me with this? Yes, please read the documentation in ?attach attach(total) The following object(s) are masked from total ( position 3 ) : acid base cell_evalue cell_hit charged freq_cell freq_hypo freq_intra gene_id gene_name hydrophobic hypo_evalue hypo_hit log_cell log_hypo log_pfam num_cell num_genes operon_id outcome pfam_align pfam_evalue pfam_per_id polar position target total_length Specifically see the documentation for the `warn.conflicts` parameter. I'm guessing you have (global) variables in your workspace that have the same name as some of the columns in your `total` data.frame -- Steve Lianoglou Graduate Student: Computational Systems Biology | Memorial Sloan-Kettering Cancer Center | Weill Medical College of Cornell University Contact Info: http://*cbio.mskcc.org/~lianos/contact __ 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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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 GIS data in R
I'm currently doing a lot of simple GIS work in R, including points in polygon queries. My .Rprofile file has require(maptools) require(rgdal) With that as a starting point, I find that the data structures play well together. Define a coordinate reference system object with crs.ll - CRS('+proj=longlat +ellps=GRS80 +datum=NAD83 +no_defs') Load a shapefile with my.shp - readOGR('directoryname','filename', p4s=CRSargs(crs.ll) ) This will give you an object of class SpatialPolygonsDataFrame. readOGR() is in the rgdal package. readShapeSpatial or readShapePoly from the maptools package should work as well, and I used to use them, but lately I've been using readOGR(). Then the overlay() function in the sp package will do your #2. But I do think you'll need your points to be one of the SpatialPoints classes. Omitting the p4s argument from readOGR() might work, I'm not sure. Or, I think you might be able to just supply the text string, i.e., p4s='+proj=longlat +ellps=GRS80 +datum=NAD83 +no_defs' but I have found it handy to have several projections predefined, as in crs.ll - CRS('+proj=longlat +ellps=GRS80 +datum=NAD83 +no_defs') crs.utm - CRS('+init=epsg:32610') for use in the spTransform() function. Also, your question would go better on R-sig-geo mailing list. A final note, some plotting functions need to have the sp package earlier in the search() path than maptools. -Don At 9:37 AM -0600 4/1/10, Scott Duke-Sylvester wrote: I have a simple problem: I need to load a ERSI shapefile of US states and check whether or not a set of points are within the boundary of these states. I have the shapefile, I have the coordinates but I'm having a great deal of difficulty bringing the two together. The problem is the various GIS packages for R do not play well with each other. sp, shapefiles, maptools, etc all use different data structures. Can someone suggest a simple set of commands that will work together that will: 1) load the shapefile data. 2) Allow me to test whether or not a (lng,lat) coordinate pair are inside or outside the polygons defined in the shapefile. Many thanks, scott. -- Scott M. Duke-Sylvester Assistant Professor Department of Biology Office : 300 E. St. Mary Blvd Billeaud Hall, Room 141 Lafayette, LA 70504 Mailing address : UL Lafayette Department of Biology P.O.Box 42451 Lafayette, LA 70504-2451 Phone : 337 482 5304 Fax : 337 482 5834 email : smd3...@louisiana.edu __ 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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] removing text form the graph(outer region)
You have to look at the documentation for the function you used to create the calibration plot. Find out what options it has for controlling what text appears, and use them. If there are no options, then maybe there is a graphics parameter [ see help('par') ] that will prevent them. Otherwise, you will probably have to reconstruct the plot yourself using basic plotting functions. Or, assuming you used a plotting function from some package, contact the package author or maintainer. -Don At 11:27 AM -0400 3/24/10, paaventhan jeyaganth wrote: Dear R communities, when i do a calibration plot i have text inside the graph bottom left and right (outer region) saying x resampling added B=200, i want to get rid of this texts please advise me how can i do it Thanks very much Paaveen _ Take your contacts everywhere [[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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] Are loops handled differently in newer versions of R?
Try for ( i in seq(nrow(lab8.dat)) ) p_unadj[i]-calc.prob.t(lab8.dat[i,2], lab8.dat[i,3]) The first column of lab8.dat is a factor, and you're trying to use it as a loop index as if it were an integer. (Which I would consider dangerous.) No, loops are not handled differently, but automatic conversion of factors to numeric apparently is. -Don At 10:53 PM -0400 3/16/10, Michael Rennie wrote: Hi gang, I'm perplexed- I have some code that uses for() loops that works fine in R version 2.8 on my mac, worked fine in version 2.8 on my old windows machine, but doesn't work in version 2.10 on windows. The loop implements a function over a data frame (code is included below). In Mac (running version 2.8), the results of the loop are what I expect: p_unadj [1] 0.034939481 0.015743706 0.089287030 0.001098538 0.039290594 But in Windows (running version 2.10.1), I get a bunch of NA's... p_unadj A B CDE NA NA NA NA NA If I had to guess, I'd say that R v. 2.10 is handling the i in lab8.dat[,1] differently, given that it's keeping the row names in the output for p_unadj... but why would that stop it from applying the function? Any thoughts or suggestions are welcome. Cheers, Mike Here's the code... #build the dataset locn-c(A, B, C, D, E) n-c(28, 14, 21, 52, 35) corr.r-c(0.40, 0.63, 0.38, 0.44, 0.35) lab8.dat-data.frame(locn, n, corr.r) lab8.dat attach(lab8.dat) #write the function calc.prob.t-function(n, r) #given a sample size (n) and correlation coefficient (r), returns the probability for that test { df-n-2 t-(r-0)/(sqrt((1-r2)/df)) probt-2*(pt(t, df, lower.tail=FALSE)) probt } #try out the function... calc.prob.t(lab8.dat$n[1], lab8.dat$corr.r[1]) #it works. #write a loop to implement that function for every correlation in your dataset... p_unadj-numeric(length(lab8.dat[,1])) p_unadj-NULL p_unadj #all this just built an empty vector to store the results of our loop... for ( i in lab8.dat[,1] ) p_unadj[i]-calc.prob.t(lab8.dat[i,2], lab8.dat[i,3]) p_unadj #if executed on my Mac, running R v.2.8, this works (and did using 2.8 on my old windows machine). Running v. 2.10 in Windows, I get NAs. -- Michael D. Rennie, Ph.D. Postdoctoral Fellow, Environmental and Life Sciences Program Trent University 2140 East Bank Drive, DNA Building (2nd Floor) Peterborough, Ontario K9J 7B8 Vox:705.755.2287 Fax:705.755.1559 www.*people.trentu.ca/michaelrennie __ 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. -- - Don MacQueen Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 m...@llnl.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] Are loops handled differently in newer versions of R?
Joshua's explanation of rownames makes a lot more sense than my speculation about conversion to numeric. Rownames of dataframes is an area in which there have definitely been changes in R in the last year or two, give or take. (I don't recall details or timing) Therefore, I find it very plausible that in R 2.8.x the rownames of your dataframe were different than they are now in R 2.10.x, given how you constructed the dataframe. This then would be the explanation of why the script worked in 2.8.x and not in 2.10.x. -Don At 8:57 PM -0700 3/16/10, Joshua Wiley wrote: Michael, I have to agree with Don that using a factor as a loop index seems like a risky choice. At any rate, part of the problem is that you are referencing a nonexistant part of your dataframe. i is an index of characters, but your rownames are 1:5, not LETTERS[1:5]. If you give your dataframe rownames, you can then use your loop, see below. locn-c(A, B, C, D, E) n-c(28, 14, 21, 52, 35) corr.r-c(0.40, 0.63, 0.38, 0.44, 0.35) lab8.dat-data.frame(locn, n, corr.r) lab8.dat calc.prob.t-function(n, r) { df-n-2 t-(r-0)/(sqrt((1-r^2)/df)) # I'm assuming you mean r^2 here not r2 probt-2*(pt(t, df, lower.tail=FALSE)) probt } p_unadj-NULL # since you assign it to null anyways, there's not real point in the other assignment p_unadj for ( i in lab8.dat[,1] ) p_unadj[i] - calc.prob.t(lab8.dat[i,2], lab8.dat[i,3]) p_unadj # all NAs as you noticed rownames(lab8.dat) - lab8.dat$locn for ( i in lab8.dat[,1] ) p_unadj[i] - calc.prob.t(lab8.dat[i,2], lab8.dat[i,3]) p_unadj # now lab8.dat[A,2] etc. means something, and it works ## On Tue, Mar 16, 2010 at 7:53 PM, Michael Rennie mdren...@gmail.com wrote: Hi gang, I'm perplexed- I have some code that uses for() loops that works fine in R version 2.8 on my mac, worked fine in version 2.8 on my old windows machine, but doesn't work in version 2.10 on windows. The loop implements a function over a data frame (code is included below). In Mac (running version 2.8), the results of the loop are what I expect: p_unadj [1] 0.034939481 0.015743706 0.089287030 0.001098538 0.039290594 But in Windows (running version 2.10.1), I get a bunch of NA's... p_unadj A B CDE NA NA NA NA NA If I had to guess, I'd say that R v. 2.10 is handling the i in lab8.dat[,1] differently, given that it's keeping the row names in the output for p_unadj... but why would that stop it from applying the function? Any thoughts or suggestions are welcome. Cheers, Mike Here's the code... #build the dataset locn-c(A, B, C, D, E) n-c(28, 14, 21, 52, 35) corr.r-c(0.40, 0.63, 0.38, 0.44, 0.35) lab8.dat-data.frame(locn, n, corr.r) lab8.dat attach(lab8.dat) #write the function calc.prob.t-function(n, r) #given a sample size (n) and correlation coefficient (r), returns the probability for that test { df-n-2 t-(r-0)/(sqrt((1-r2)/df)) probt-2*(pt(t, df, lower.tail=FALSE)) probt } #try out the function... calc.prob.t(lab8.dat$n[1], lab8.dat$corr.r[1]) #it works. #write a loop to implement that function for every correlation in your dataset... p_unadj-numeric(length(lab8.dat[,1])) p_unadj-NULL p_unadj #all this just built an empty vector to store the results of our loop... for ( i in lab8.dat[,1] ) p_unadj[i]-calc.prob.t(lab8.dat[i,2], lab8.dat[i,3]) p_unadj #if executed on my Mac, running R v.2.8, this works (and did using 2.8 on my old windows machine). Running v. 2.10 in Windows, I get NAs. -- Michael D. Rennie, Ph.D. Postdoctoral Fellow, Environmental and Life Sciences Program Trent University 2140 East Bank Drive, DNA Building (2nd Floor) Peterborough, Ontario K9J 7B8 Vox:705.755.2287 Fax:705.755.1559 www.*people.trentu.ca/michaelrennie __ 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 Senior in Psychology University of California, Riverside 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. -- - Don MacQueen Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 m...@llnl.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] Getting multiple matrix-values using a single command
Everyone is being too complicated. c( A[1,2] , A[3,3] ) will do what you ask. A - matrix(seq(1,9),nrow=3) c( A[1,2] , A[3,3] ) [1] 4 9 But I would assume you have some more general problem in mind, and I do not know if this simple approach will meet those needs. -Don At 3:26 PM +0100 3/12/10, Nils Rüfenacht wrote: Dear all! I'm trying to get multiple values from a matrix by using a single command. Given a matrix A A - matrix(seq(1,9),nrow=3,ncol=3) How can I get e.g. the values A[1,2] = 4 and A[3,3] = 9 with a single command and without using any loop? My first idea was to generate a row- and a column vector for the indices, i.e. c(1,3) indicating row number 1 (for A[1,2]) and row number 3 (for A[3,3]) and similar for column-indices. Then I've tried to call A[c(1,3),c(2,3)] but instead of 4 , 9 the result is [,1] [,2] [1,]47 [2,]69 Any suggestions? Regards, Nils __ 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. -- - Don MacQueen Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 m...@llnl.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] using sprintf to pass a variable to a RMySQL query
I always use paste() i - 1 sqlcmd_ScaffLen - paste(SELECT scaffold.length FROM scaffold, scaffold2contig, contig2read WHERE scaffold.scaffold_id=scaffold2contig.scaffold_id AND scaffold2contig.contig_id=contig2read.contig_id AND contig2read.read_id LIKE '%MG, i ,%', sep='') That should create bits like LIKE '%MG1%' LIKE '%MG2%' and so on. You just have to get the nesting of the single and double quotes correct - the SQL requires single quotes, so use double quotes for the fixed character strings insidte paste(). That, and use sep='' to get rid of unwanted space characters. Using paste is also effective for constructs like IN (3,4,5) or IN ('a','b','c') though it can be necessary to nest one paste within another -Don At 2:06 PM +0100 3/8/10, alison waller wrote: Hello, I am using RmySQL and would like to iterate through a few queries. I would like to use sprintf but I think I'm having problems mixing and matching the sprintf syntax and the SQL regex. I have checked my sqlcmd and it works when I wan to match %MG1% but how do I iterate for i 1-72? Escape characters,? thanks in advance i-1 sqlcmd_ScaffLen-sprintf('SELECT scaffold.length FROM scaffold,scaffold2contig,contig2read WHERE scaffold.scaffold_id=scaffold2contig.scaffold_id AND scaffold2contig.contig_id=contig2read.contig_id AND contig2read.read_id LIKE '%MG%s%' ,i) = Here is my vague error message Error: unexpected input in: __ 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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] Why can't apply be used with as.factor on a data.frame ?
And just a small followup. To find out what class each column is, you wanted lapply(a,class) $x1 [1] numeric $x2 [1] factor $x3 [1] factor With regard to your solution, and why it works, it is my understanding that data frames are in some sense actually lists, each column corresponding to one element in a list. Hence, lapply() works column-wise on data frames. Also for this reason it's pretty easy to convert back and forth between data frames and lists . Provided, of course, that each element of the list has an appropriate structure; see this example: data.frame( list(a=1:2, b=3:4) ) a b 1 1 3 2 2 4 data.frame( list(a=1:2, b=3:7) ) Error in data.frame(a = 1:2, b = 3:7, check.names = FALSE, stringsAsFactors = TRUE) : arguments imply differing number of rows: 2, 5 No doubt there are subtle details, but don't ask me to provide details on what exactly the some sense is! -Don At 12:07 PM +0200 3/7/10, Tal Galili wrote: Hi all, Let's say I have a data.frame and wants to turn each of it's columns into a factor. My instinct would be to use as.factor with apply. But this won't work, and result with a data.frame of characters. I found another solution for how to achieve this, but I would also like to understand - *WHY* does it work this way? Here is an example script: a - data.frame(x1 = rnorm(100), x2 = sample(c(a,b), 100, replace = T), x3 = factor(c(rep(a,50) , rep(b,50 apply(a2, 2,class) # why is column 3 not a factor ? a[,3] # since it IS a factor. a2 - apply(a, 2,as.factor) # won't work - why not ? a2[,3] # Why was this just turned into a character ??? # A solution a2 - lapply(a, as.factor) a3 - as.data.frame(a2) str(a3) Thanks, 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. -- - Don MacQueen Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 m...@llnl.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] Date conversion problem
as.Date('17/02/2005','%d/%m/%Y') [1] 2005-02-17 (Read the documentation more carefully to distinguish between %y and %Y; I guess you tried lots of combinations but never tried the correct one, so just be more careful at matching what your data is with the format string you create.) -Don At 8:09 AM -0800 3/4/10, Newbie19_02 wrote: Hi All, I have a character data.frame that contains character columns and date columns. I've manage to convert some of my character columns to a date format using as.Date(x, format=%m/%d/%y). An example of one of my dates is PROCHIDtDeath icdcucd date_admission1 date_admission_2 CAO0004563 NANA 2005-09-01 NA CAO0073505 NANA 1998-03-05 NA CAO0079987 NANA 2002-04-14 NA CAO0182089 NANA 2007-06-10 11/06/07 CAO0194809 17/02/2005 I64 2004-09-04 14/02/05 CAO0204000 NANA 1999-05-31 NA CAO027 NANA 1999-07-29 NA CAO0330844 29/11/2001 I64NA NA CAO0395045 NANA 2007-02-13 14/02/07 CAO0507333 NANA 2005-10-08 NA I have converted date_admission1 from a character to a date. I used the same script to convert DtDeath but it returns the dates in this format: NA NA NA NA 0017-02-20 [6] NA NA 0029-11-20 NA NA [11] NA NA 0013-10-20 NA NA [16] NA 0007-12-20 NA NA NA [21] NA NA NA NA NA [26] NA NA NA NA NA [31] NA NA NA NA NA [36] NA NA NA NA NA [41] NA NA NA NA NA [46] NA 0029-01-20 0018-05-20 NA NA [51] NA NA NA NA NA [56] NA 0013-07-20 NA NA NA [61] NA 0026-07-20 NA NA NA [66] 0029-04-20 NA NA NA 0012-12-20 [71] NA NA NA NA NA [76] NA NA NA NA NA [81] NA NA 0022-01-20 NA 0029-05-20 [86] NA NA NA NA 0022-02-20 [91] NA I've tried as.Date(as.character(DtDeath, %d/%m/%y) just in case and have used different versions of the format (%m/%d/%Y, and the reverse)but still get the incorrect format. I'm not sure what the problem is? Thanks, natalie -- View this message in context: http://*n4.nabble.com/Date-conversion-problem-tp1578296p1578296.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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] A slight trap in read.table/read.csv.
There is, however, an important distinction. Quoting from ?TRUE (or ?logical): 'TRUE' and 'FALSE' are reserved words denoting logical constants in the R language, whereas 'T' and 'F' are global variables whose initial values set to these. All four are 'logical(1)' vectors. TRUE - 3 Error in TRUE - 3 : invalid (do_set) left-hand side to assignment In other words, the rule is T is TRUE unless otherwise defined by the user (ditto for F) So this rule apparently applies to input from a file. Using colClasses is then an example of otherwise defined by the user. I think it's logical (pun not particularly intended) and consistent (though perhaps not ideal, but that's another question...) -Don At 5:37 PM -0500 2/28/10, Gabor Grothendieck wrote: It is strange. Even in R itself T and F are not guaranteed to be TRUE and FALSE. T - 1:3 T [1] 1 2 3 On Sun, Feb 28, 2010 at 4:55 PM, Rolf Turner r.tur...@auckland.ac.nz wrote: I had occasion recently to read in a one-line *.csv file that looked like: CandidateName,NSN,Ethnicity,dob,gender Smith, Mary Jane,111222333,E,2/25/1989,F That F (for female) in the last field got transformed to FALSE. Apparently read.csv (and hence read.table) are inferring that if the entries of a file are all F's and T's then the field is interpreted as logical. If I change the file to CandidateName,NSN,Ethnicity,dob,gender Smith, Mary Jane,111222333,E,2/25/1989,F Mingdinkler, Melvin Queue,999888777,01/04/1942,M then the read functions correctly interpret the last field as being character. The translation of F into FALSE resulted in some mysterious contretemps in further analysis, which it took me a while to track down. I solved the problem by putting in a colClasses argument in my call to read.csv(). But I really think that the read functions are being too clever by half here. If field entries are surrounded by quotes, shouldn't they be left as character? Even if they are all F's and T's? Furthermore using F's and T's to represent TRUE's and FALSE's is bad practice anyway. Since FALSE and TRUE are reserved words it would make sense for the read function to assume that a field is logical if it consists entirely of these words. But T's and F's I don't think so. I would argue that this behaviour should be changed. I can see no downside to such a change. cheers, Rolf Turner ## Attention:\ This e-mail message is privileged and confid...{{dropped:9}} __ 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. -- - Don MacQueen Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 m...@llnl.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] Preserving lists in a function
, variable3=c(0.1,0.1,0.1,0.1), variable4=TRUE) if(length(list1$variable1)==0){list1$variable1=defaults$list1$variable1} if(length(list1$variable2)==0){list1$variable2=defaults$list1$variable2} if(length(list1$variable3)==0){list1$variable3=defaults$list1$variable3} if(length(list2$variable1)==0){list2$variable1=defaults$list2$variable1} if(length(list2$variable2)==0){list2$variable2=defaults$list2$variable2} if(length(list2$variable3)==0){list2$variable3=defaults$list2$variable3} if(length(list3$variable1)==0){list3$variable1=defaults$list3$variable1} if(length(list3$variable2)==0){list3$variable2=defaults$list3$variable2} if(length(list3$variable3)==0){list3$variable3=defaults$list3$variable3} if(length(list3$variable4)==0){list3$variable4=defaults$list3$variable4} return(list(list1=list1,list2=list2,list3=list3))} The outcome of execution the above function with the same commands produces the results that we wanted: myfunction.alternative( list1=list (variable1=1, + variable2=2), #variable 3 deliberately left out + + list2=list (variable1=variable1, + variable3=position changed, + variable2=variable2), + + list3=list (variable1=character, + variable2=24, + variable4=FALSE)) #variable 3 deliberately left out $list1 $list1$variable1 [1] 1 $list1$variable2 [1] 2 $list1$variable3 [1] 3 #list1$variable3 is assigned default despite being left out in the execution command $list2 $list2$variable1 [1] variable1 $list2$variable3 [1] position changed $list2$variable2 [1] variable2 $list3 $list3$variable1 [1] character $list3$variable2 [1] 24 $list3$variable4 [1] FALSE $list3$variable3 [1] 0.1 0.1 0.1 0.1 #list3$variable3 is assigned default despite being left out in the execution command Even though the function works, as you can see, the codes that enforce the defaults are very long and bulky. Such lengthy codes won't be efficient if we have a write a function containing a large number of lists. We tried to come up with ideas to try to shorten the codes, but so far none of them prove to be effective. What would be your recommendation to deal with such situation? It would be great if you would be able to help us our with this problem. We appreciate your help tremendously. Thank you. Sincerely, Shang [[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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] New Variable from Several Existing Variables
If your data is in a matrix named orgdata : newvar - apply(orgdata , 1, function(arow, if (all(arow=='Yes')) 'Yes' else 'No' newdata - cbind(orgdata, newvar) finaloutcome - newdata[ newvar=='Yes',] The key to this is the apply() function. I might have missed some parentheses... There are other ways; this is just one. I might think of a simpler one if I gave it more time... -Don At 4:40 PM -0800 2/26/10, wookie1976 wrote: I am new to R, but have been using SAS for years. In this transition period, I am finding myself pulling my hair out to do some of the simplest things. An example of this is that I need to generate a new variable based on the outcome of several existing variables in a data row. In other words, if the variable in all three existing columns are Yes, then then the new variable should also be Yes, however if any one of the three existing variables is a No, then then new variable should be a No. I would then use that new variable as an exclusion for data in a new or existing dataset (i.e., if NewVariable = No then delete): Take this: Column1, Column2, Column3 Yes, Yes, Yes Yes, No, Yes No, No, No No, Yes, No Yes, Yes, No Generate this: Column1, Column2, Column3, NewVariable1 Yes, Yes, Yes, Yes Yes, No, Yes, No No, No, No, No No, Yes, No, No Yes, Yes, No, No And end up with this: Column1, Column2, Column3, NewVariable1 Yes, Yes, Yes, Yes Any suggestions on how to efficiently do this in either the existing or a new dataset? Thanks, -- View this message in context: http://*n4.nabble.com/New-Variable-from-Several-Existing-Variables-tp1571574p1571574.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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] R error- more columns than column names
From the help page for the read.delim() function, under See Also, suggests: 'count.fields' can be useful to determine problems with reading files which result in reports of incorrect record lengths. This is sometimes helpful. -Don At 1:14 PM -0800 2/24/10, Euphoria wrote: Hi all! I am desperately trying to figure out the solution to this error, but nothing as of yet is working. As noted in an earlier post I am using GenABEL. In an attempt to read in the phenotype file, in the format .dat, R keeps giving me the error more columns than column names I have tried to read in the data without the headers; I have also tried to trim the data to remove any trailing tabs or spaces but it doesn't solve the problem. All missing values have been replaced with NA, and all data seems to have matching corresponding header value - each column has a matching column name. What could be the possible underlying problem? I have tried to problem-solve but clearly I am at a loss. Thanks for your help! Code: mix - load.gwaa.data (phe = Z:/CCFPhenotypesTAB.dat, gen = pedmap-0.raw, force = T) -- View this message in context: http://*n4.nabble.com/R-error-more-columns-than-column-names-tp1568052p1568052.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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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 strptime generating missing values where none appear to exist
PLEASE do read the posting guide http://*www.*R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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 make R plot under Linux
The classic test for a properly set up X11 is to issue the command xclock at the prompt (on the linux box). (this assumes the linux box has xclock installed, but that is highly likely) If it works, the X11 forwarding is set up. If not, then, evidently not, but in the latter case at least you say it isn't R. -Don At 12:46 PM -0800 2/22/10, xin wei wrote: hi, Guys: thank you so much for all the suggestion. Now I seem to be able to set up x11 forwarding in PUTTY. however, I still could not get plot and I get the following error msg: Error in function (display = , width, height, pointsize, gamma, bg, : X11 I/O error while opening X11 connection to 'localhost:20.0' Is this error msg indication of lack of appropriate plotting package on the server or the server is not properly set up for X11 forwarding? thanks -- View this message in context: http://*n4.nabble.com/how-to-make-R-plot-under-Linux-tp1562060p1565113.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. -- - Don MacQueen Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 m...@llnl.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] plot is not keeping the order of variable
Perhaps this example will help. x - factor(c('b','a','d'), levels=c('a','d','b')) y - 1:3 plot(x,y) And compare with z - factor(c('b','a','d')) plot(z,y) In the first, the plot is in the order that I chose. In the second it is in the order that R chose -- and R chose it according to a built-in and reasonable rule. -Don At 6:30 PM +0200 2/21/10, Or Duek wrote: Ok, It seems that the problem lays in the order (as Ista mentioned), But, when I ask R to order it he chooses to order it by size and not by the specific order I mentioned when I built the vector. Is it possible to tell him to keep the order as mentioned? Thank you. On Sun, Feb 21, 2010 at 6:16 PM, Ista Zahn istaz...@gmail.com wrote: Hi Or, I can't know for sure what your problem is without an example, but the first thing I would do is check to make sure that your labels are stored as an ordered factor (and that the order is correct). See ?factor for details. Best, Ista On Sun, Feb 21, 2010 at 10:57 AM, Or Duek ord...@gmail.com wrote: Hi, I created a simple data frame with one factor and one numerical variable. The factor was actually a vector of names of techniques to trimm reaction time data. I want to create a plot that shows the value of F test for every trimming method. So the data frame has its trim factor (who has those labels mean,2500,2000,1500,1000,log,inverse,1SD,2SD) and the numerical variable of the data frame has the F values for each one of those trimming method. When I ask R to plot it, it doesn't keep the order of the trimm verctor correctly and the plot confuses the order so the most left one will be 1500 and then 1SD etc. The values are correct but it is important for me to keep it in the same order I built it. How can I do it? Thank you very much, Or D. [[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. -- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://*yourpsyche.org [[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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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 source files from a search path?
Well, you still will need Sys.getenv() to get the value of the environment variable into R. Are you familiar with the function named list.files() ? This may do the job: source( list.files( Sys.getenv('SOMEENVVAR'), pattern='myfilename') ) But I haven't tested it. -Don (and your name or affiliation would still be appreciated, as a matter of R-help etiquette) At 5:42 PM -0600 2/12/10, blue sky wrote: On Fri, Feb 12, 2010 at 5:28 PM, Bert Gunter gunter.ber...@gene.com wrote: 1. Etiquette on this list is to sign posts with your real name. 2. Please use R's Help facilities (beyond RSiteSearch()) first before posting: ?help.search help.search(environment variable) I guess you are referring to 'Sys.getenv', which is not what I am looking for. To restate my question: How to source a file without specifying the full path, but by deriving the file's full path by searching in an environment variable. Of course, 'Sys.getenv' will be useful if I want to make such a function myself. But my question was to look for a better version of source(). Are you clear about my question now? -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of blue sky Sent: Friday, February 12, 2010 2:55 PM To: r-h...@stat.math.ethz.ch Subject: [R] How to source files from a search path? Suppose some environment variable (say MY_R_INC) has a number of paths. I want to source some file relative to some path in $MY_R_INC (just as #include in C++ does, which looks for header file in a number of directories). I RSiteSearch'ed, but I don't find any function that satisfies my need. Could somebody let me know if I overlooked something? __ 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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] Unexpected output in first iteration of for-loop
You have a mistake in how you're setting up the object named result before the loop. You set result - latentVariableNames. It is a vector of length 6, when you call the function with LV. The printout from the first iteration shows this. But then you rbind result with a data frame that has three columns. These are basically incompatible, but R (apparently silently) wraps the vector of length 6 into two rows of three columns. Here's an example: foo - letters[1:6] names(foo) - c('a','b','c') foo abc NA NA NA a b c d e f rbind(foo, data.frame(a='X', b='Y', c='Z',stringsAsFactors=FALSE)) a b c 1 a b c 2 X Y Z What I would do is something like this (untested): loopCronbach - function(latentVariableNames, groupingVariable) { n - length(latentVariableNames) tmp1 - tmp2 - numeric(n) for (i in 1:n) { tmp1[i] - calculateIndividualCronbach(get(latentVariableNames[i])) tmp2[i] - calculateGroupCronbach(get(latentVariableNames[i]), groupingVariable) } result - data.frame(latentVariable=latentVariableNames,Indiv=tmp1,Group=tmp2) names(result) - c(latentVariable, Indiv, Group) result ## no need to use return() } Building up a dataframe row by row using rbind() is in general an expensive way to do things -- although in this instance it's such a small dataframe that it doesn't matter. -Don At 10:56 AM +0100 2/11/10, Chaehan So wrote: Ok, you're right - may I rephrase: How should I modify the assignment of result - latentVariableNames so it produces the output without the first line? I thought result - NULL should do the job, but it didn't because of the following names(result) assignment (which I understand, but not how I can workaround it). Here's the output and code again: latentVariable Indiv Group 1 rPlanning rIterat rTDD 2 rPlanning0.79 0.84 3rIterat0.79 0.83 4 rTDD 0.9 0.96 5 rStandup0.83 0.82 6rRetros 0.9 0.95 7rAccess0.91 0.92 8 rAccTest0.87 0.9 # LV - c(rPlanning, rIterat, rTDD, rStandup, rRetros, rAccess, rAccTest) # loopCronbach - function(latentVariableNames, groupingVariable) { result - latentVariableNames names(result) - c(latentVariable, Indiv, Group) for (currentName in latentVariableNames) { print(currentName) print(result) tmp1 - calculateIndividualCronbach(get(currentName)) tmp2 - calculateGroupCronbach(get(currentName), groupingVariable) result - rbind(result,data.frame(latentVariable=currentName, Indiv=tmp1,Group=tmp2)) } return(result) } On Thu, Feb 11, 2010 at 3:31 AM, jim holtman jholt...@gmail.com wrote: It doing exactly what you asked it to do. You have the assignment: result - latentVariableNames and then you print it out in the loop. What were you expecting? On Wed, Feb 10, 2010 at 9:06 PM, Chaehan So chaehan...@gmail.com wrote: Dear r-helpers, why do I get an output in the first iteration of the for-loop which contains the string values of the input vector, and how can I avoid that? Here's the output (only line 1 is wrong) latentVariable Indiv Group 1 rPlanning rIterat rTDD 2 rPlanning0.79 0.84 3rIterat0.79 0.83 4 rTDD 0.9 0.96 5 rStandup0.83 0.82 6rRetros 0.9 0.95 7rAccess0.91 0.92 8 rAccTest0.87 0.9 # LV - c(rPlanning, rIterat, rTDD, rStandup, rRetros, rAccess, rAccTest) # loopCronbach - function(latentVariableNames, groupingVariable) { result - latentVariableNames names(result) - c(latentVariable, Indiv, Group) for (currentName in latentVariableNames) { print(currentName) print(result) tmp1 - calculateIndividualCronbach(get(currentName)) tmp2 - calculateGroupCronbach(get(currentName), groupingVariable) result - rbind(result,data.frame(latentVariable=currentName, Indiv=tmp1,Group=tmp2)) } return(result) } a - loopCronbach(LV, u_proj) [[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. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? -- Humboldt University Berlin, Germany Institute of Psychology Rudower Chaussee 18, Room 1221 12489 Berlin Germany Office: +49 30 2093 - 9337 Mobile: +49 171- 626 9373 [[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. -- -- Don
Re: [R] trouble with read.table and colClasses='raw'
The error message says there is no method for converting from 'character' to 'raw'. Apparently, R is seeing character data in the file, and is trying to convert it to raw, since you specified raw, and it can't. See, for example, as('aa','raw') Error in as(aa, raw) : no method or default for coercing character to raw (same error message) So I would ask, what are your data, really? Why are you asking for raw? Have you checked the help page for raw to make sure it's what you want? -Don At 5:23 PM +0100 2/11/10, Ivan Calandra wrote: Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit Content-length: 3983 Well, it's too complicated for me! Here are what I would do (limited since I'm still a newbie) 1) the syntax seems correct, it should work. The problem is somewhere else, coming from your own file. Did you try skipping the colClasses argument? To see how it looks like... If you can import it that way, try str(x) to see what you have. It might help you. 2) I've never had that much data to import, and for me read.table works well. You might want to wait for the experts! Ivan Le 2/11/2010 17:14, Johan Jackson a écrit : Hi Ivan, Thanks for the reply. Damn IT! My original post was screwed up. HERE is what I did: x - read.table(data,header=TRUE,colClasses=rep('raw',60)) #returns error: no method or default for coercing character to raw I've read the ?read.table and the colClasses argument. I'm still unclear: 1) colClasses is a character vector, is that right? That seems to be what the help says, but I get an error when I do the above. 2) what is the most efficient way to read in huge amounts of data? In the past I found that scan() and readLines() were slower than read.table. Thanks, JJ On Thu, Feb 11, 2010 at 8:53 AM, Ivan Calandra ivan.calan...@uni-hamburg.de mailto:ivan.calan...@uni-hamburg.de wrote: Hi! |colClasses| character. A vector of classes to be assumed for the columns. I'm not an R expert and I don't know what your flat file raw is, but the colClasses argument is to define whether the column will be treated as containing factors, logical, integer etc... For more on read.table, read the manual R Data Import/Export available on the R-project website. I don't know if it helps, but I hope it does! Ivan Le 2/11/2010 16:36, Johan Jackson a écrit : Hi all, First off, it is surprising that there are no examples of how to use read.table() under ?read.table ! I am trying to read in a flat file of type 'raw'. It has 1000 rows and 600K columns. I have the RAM to accomplish this, but can't get the data into R using read.table: x- read.table(data,header=TRUE,colClasses=rep(,60)) #returns error: no method or default for coercing character to raw Then I thought that maybe the colClasses vector needed to actually *be* the mode needed (here's where an example under ?read.table would help): x- read.table(data,header=TRUE,colClasses=rep(as.raw(1),60)) I waited on the latter command for a couple of hours before killing the process. What should the colClasses argument be? Should I be using another method to read the data into R? Previous experience using scan() and readLines() showed that read.table() was faster, at least for those examples, so I've stopped trying to use those other functions. Thank you, JJ [[alternative HTML version deleted]] __ R-help@r-project.org mailto: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 mailto: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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ R-help@r-project.org
Re: [R] Access variables by string
For your first question, use the get() function -Don At 5:18 PM +0100 2/11/10, Philipp Rappold wrote: Dear all, I have two probably very easy questions: (1) Is there a way to access certain variables by their string-based name representation? Example: numbers - c(one, two, three) varname - numbers print(varname[2]) (2) I need this functionality for a customized na.exclude() function that I am building, which should only exclude rows that have NA in certain columns. Maybe there is already a function which does exactly what I need, so I'd highly appreciate if someone could point me there ;) My current implementation looks like this: naexlcude - function(data, varnames) { for(v in varnames){ data = subset(data, !is.na(v)) } data } Best Philipp __ 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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] difftime result for days not an integer?
Which brings up another point. The help page for difftime specifies that it operates on date-time or date objects. But '2004-08-05' is neither of these, it is a character object. At this point, one might ask... I didn't give it what it asked for, what is it going to do? (might give me an error message, might do who knows what, but find out!) R is pretty good about automatic conversions between types, so it's reasonable that difftime() would convert the arguments to a valid type, if it can. But which one, since there are two valid types? And as we have seen, it is to date-time, not date. For some, but certainly not all, R functions, details like this can be discovered by typing the name of the function at the R prompt, without the parentheses. difftime function (time1, time2, tz = , units = c(auto, secs, mins, hours, days, weeks)) { time1 - as.POSIXct(time1, tz = tz) time2 - as.POSIXct(time2, tz = tz) ... etc ... -Don At 12:53 PM -0800 2/11/10, Thomas Lumley wrote: On Thu, 11 Feb 2010, Jonathan wrote: Anybody have an idea why I would get a non-integer value for the number of days here? difftime('2004-08-05','2001-01-03',units='days') Time difference of 1309.958 days Because it's not a whole number of 24-hour periods, due to daylight saving time: 0.958 is 23/24. Would you just round off? Yes, or use as.Date() if you only want to consider whole days R as.Date('2004-08-05')-as.Date('2001-01-03') Time difference of 1310 days -thomas Thomas Lumley Assoc. Professor, Biostatistics tlum...@u.washington.eduUniversity of Washington, Seattle __ 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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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 use a string to refer a function?
Don't really understand what you have in mind, but maybe this will do it. f - function(x=3) x^2 Now, if I want to get the function f, given that I only know its name f, I can do this: getf - get('f') getf(4) Or even: get('f')(5) [1] 25 Also, attributes(getf) $source [1] function(x=3) x^2 At 4:26 PM -0600 2/11/10, blue sky wrote: f=function(){ print('in f')} attr(f, 'source') [1] function(){ print('in f')} I have the above simple function. I can use the variable f to refer to the function and get the function source. Suppose that I have 'f' as a string (say I get it from ls()), could somebody let me know how to get the function from the name 'f'? __ 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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] expand the inside of command with rep ?
To construct the matrix that would result from your cbind() command, do this: x - 1:15 matrix( rep(x,5) , ncol=5) [,1] [,2] [,3] [,4] [,5] [1,]11111 [2,]22222 [3,]33333 [4,]44444 [5,]55555 [6,]66666 [7,]77777 [8,]88888 [9,]99999 [10,] 10 10 10 10 10 [11,] 11 11 11 11 11 [12,] 12 12 12 12 12 [13,] 13 13 13 13 13 [14,] 14 14 14 14 14 [15,] 15 15 15 15 15 This doesn't, of course, use cbind() at all. It uses a different and simpler method to directly construct the matrix. You can easily generalize it to more columns by using a variable instead of 5 in the rep() command, and for the ncol arg to matrix(). -Don At 12:27 PM -0800 2/10/10, Xu Wang wrote: Hi, I would like to be able to repeat a string within a command. I think there is an easy way but I can not figure out how. Here is an example. x-1:15 I would like to turn this into the following matrix: xm-cbind(x,x,x,x,x) But I would like to do so by having a command that repeats x within the cbind command. Does that make sense? I have tried various things mainly involving the commands rep and paste but cannot get it. cbind(rep('x',times=5)) I am guessing that this is not good programming etiquette? Something seems off about it. But still, I would like to know how to do it. I do understand that I can use a for loop to easily accomplish the desired output but am looking for a solution similar to the above syntax. Thank you for any ideas, Xu Wang -- View this message in context: http://*n4.nabble.com/expand-the-inside-of-command-with-rep-tp1476420p1476420.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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] Sorting
I have trouble making sense of the question, and I wonder if there is a terminology issue. For example, you have a list like this one: mylist - list( v1=1:3, v2=1:4, v3=1:5, v4=1:6) (That is, a list of vectors of varying lengths.) You want to apply a function to each pair of vectors: First to v1 and v2, Then to v2 and v3, Then to v3 and v4 ? And also to pairs v1 and v3, v1 and v4, and so on? Which one is larger, mylist$v1 or mylist$v2? Longer, yes, mylist$v2 is longer. But larger? And ultimately you want to have the list with its elements in some other order, perhaps v4 comes first, then v3, and so on? -Don At 1:21 PM -0500 2/6/10, David Neu wrote: Hi, I have a list of vectors (of varying lengths). I'd like to sort this list by applying a function to each pair of vectors in the list and returning information to sorting routine that let's it know which one is larger. To solve problems like this in Common Lisp, the sort function accepts a function as an argument. The arguments to this function are two elements of the list which is being sorted. The writer of the function returns t (TRUE in R) when the first argument to the function is larger than the second and nil (FALSE in R) otherwise. I'm wondering if there is some way to accomplish this in R. Many thanks for any help! Cheers, 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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] Incompatible types error
There appear to be quite a few problems. The first and most glaring to my eyes is that you initialize four vectors, Et, fx, Tx, and Fitness inside the loop. Since you set their lengths to 1000, one might assume that the 1000 values are to be calculated as the loop index, i, goes from 1 to 1000. But initializing them inside the loop each time wipes out any results calculated in previous iterations. Another is that having initialized them to numeric, you immediately assign a non-numeric value, that is, Et[i] is defined to be a function, not a number. There are other things, such as defining function(Eztx,t,A,n) and then inside the function immediately setting the value of A to Amp, which is defined outside the function. This means that if you later use the function, whatever you give to A when you use the function will be ignored, and Amp will be used instead. Then there's the expression runif[i](1,-50,50). But runif is a built-in R function, and you really can't use the [i] syntax after a function name. For example, suppose i=3. Then runif[3] Error in runif[3] : object of type 'closure' is not subsettable Finally, and of much less importance, the c() around c(1:1000) is completely unnecessary. It's fine to just do for (i in 1:1000) { I'm kind of guessing you have a fundamental misunderstanding about what a function is and does. I'd suggest starting simple and gradually building up to something like this. And please try to find some local help with R; from what I see you need more help than can be effectively given over email. -Don At 12:57 PM -0800 2/5/10, apellegrini001 wrote: I'm trying to write a loop for a series of nested functions. and I get an incompatible types error when trying to run it. It's supposed to be a simulation (1000 iterations) with a random value for rand being chosen each time. After each rand value is chosen, the rest of the functions are evaluated with this given rand value and their Fitness sum value is saved and plotted against rand. let me know if you see any obvious problems. Below is the Script #variables Amp=3 n=0.5 #variables nt-200 z0=1 z-rep(z0,nt) #overlying function t-seq(0,(2*pi),by=0.01) for(i in c(1:1000)){ Et-numeric(1000) fx-numeric(1000) Tx-numeric(1000) Fitness-numeric(1000) Et[i]-function(Eztx,t,A,n){ A=Amp Eztx[i]-A*sin(t)*n if(Tx[i]1){Eztx[i]/Tx[i] } else {Eztx[i]-Eztx[i] } fx[i]-function(mea,rand,prob,Et){ rand[i]-runif[i](1,-50,50) mea=0 prob[i]-function(pi,rand,Et,mea){ (1/(2*pi*rand[i]))*exp((-(Et[i](Eztx[i],t,A,n))-mea)^2)/(2*(rand[i]^2)) } prob(pi,rand[i],Et[i](Eztx[i],t,A,n),mea) } Tx[i]-function(fx,cdist){ cdist[i] -(1-((pnorm(Et[i](Eztx[i],t,A,n),mea,rand[i],)) -(pnorm(mea,mea,rand[i], Tzx[i]-fx[i](prob[i],rand[i],Et[i])*cdist[i] Tzx[i] } } Fitness[i]-function(Tx){ f[i]-Tx[i] Fit[i]-sum(f[i]) } } plot(Fit,rand) -- View this message in context: http://*n4.nabble.com/Incompatible-types-error-tp1470783p1470783.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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] sequence of equal-length numbers (for filenames)
One can also use formatC() with the flag option. -Don At 10:47 AM -0200 1/25/10, Henrique Dallazuanna wrote: Try sprintf: sprintf(%03d, Sequence) sprintf(file%03d.dat, Sequence) On Mon, Jan 25, 2010 at 10:39 AM, Îroutík zrou...@gmail.com wrote: Dear R-users, I'd like to create filenames in a mask file000.dat numbered from 1 to e.g. 123. The last problem I'm dealing with is creating the sequence of numbers with equal length, i.e. 001, 002, 023, 024, 122, 123. The closest I got is by a repetition: Sequence - c(1:123) for(i in c(1:length(Sequence))) { print( paste(rep(0, max(nchar(as.character(Sequence)))-nchar(as.character(Sequence[i]))), as.character(Sequence[i]), sep=)) } where pasting 0-replication the missing-times I 'm possibly creating the desired output. It's just that rep()'s output is not a vector and not subsequent atoms. and gives 02 and 02 instead of 002. Any idea hot to correct the function above or suggestions on file000.dat-mask filename, please? Thank you for your time. M [[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. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O __ 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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] Data Manipulation
Does this example help? a - matrix(letters[1:12], ncol=3) a [,1] [,2] [,3] [1,] a e i [2,] b f j [3,] c g k [4,] d h l write.table(a[,3,drop=FALSE],quote=FALSE,col.names=FALSE,row.names=FALSE) i j k l At 4:11 PM -0800 1/21/10, Peter Rote wrote: Thank you Dieter and Rolf, I have solved the slash Problem, but I still struggling with the output files. I have tried this by(AlexETF,AlexETF$Industry,function(a) {filename = paste(C:/ab/,gsub( ,,a$Industry[1]),.txt,sep=) print(filename) write.table(a,file=filename,col.names = FALSE) } ) and this by(AlexETF,AlexETF$Industry,function(a) {filename = paste(C:/ab/,gsub( ,,a$Industry[1]),.txt,sep=) print(filename) write(as.character(a),file=filename) } ) I want in each file just the ticker with out any quotations mark. CMM FMCN IPG MWW Thanks in advance, Peter -- View this message in context: http://*n4.nabble.com/Data-Manipulation-tp1018249p1073567.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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] loop on list levels and names
Without reading all the details of your question, it looks like maybe split() is what you want. split( dataset, paste(dataset$SPECSHOR,dataset$BONE) ) or split( dataset[,3], paste(dataset$SPECSHOR,dataset$BONE) ) -Don At 5:12 PM +0100 1/21/10, Ivan Calandra wrote: Hi everybody! To use some functions, I have to transform my dataset into a list, where each element contains one group, and I have to prepare a list for each variable I have (altogether I have 15 variables, and many entries per factor level) Here is some part of my dataset: SPECSHORBONEAsfcSmcepLsar cotautx454.39036929.2616380.001136 cotautx117.4457114.2918840.00056 cotautx381.02468215.3130170.002324 cotautx159.08178918.1345330.000462 cotautm160.6415036.4113320.000571 cotautm79.2380233.8282540.001182 cotautm143.2065511.9218990.000192 cotautm115.47699633.1163860.000417 cotautm594.25623472.5381310.000477 eqgretx188.2613248.2790960.000777 eqgretx152.4442162.5963250.001022 eqgretx256.6015078.2790960.000566 eqgretx250.81644518.1345330.000535 eqgretx272.39671124.4928790.000585 eqgretm172.632644.2918840.001781 eqgretm189.44109714.4254980.001347 eqgretm170.74378813.5644720.000602 eqgretm158.96084910.3852990.001189 eqgretm80.9724083.8282540.000644 gicamtx294.4940019.6567380.000524 gicamtx267.12676519.1280240.000647 gicamtx81.8886584.7820060.000492 gicamtx168.3290812.7299390.001097 gicamtx123.2960567.0074270.000659 gicamtm94.26488718.1345330.000752 gicamtm54.3173953.8282540.00038 gicamtm55.97888317.1675340.000141 gicamtm279.59799315.3130170.000398 gicamtm288.26255618.1345330.001043 What I do next is: list_Asfc - list() list_Asfc[[1]] - ssfamed[ssfamed$SPECSHOR=='cotau'ssfamed$BONE=='tx', 3] list_Asfc[[2]] - ssfamed[ssfamed$SPECSHOR=='cotau'ssfamed$BONE=='tm', 3] And so on for each level of SPECSHOR and BONE I'm stuck on 2 parts: - in a loop or something similar, I would like the 1st element of the list to be filled by the values for the 1st variable with the first level of my factors (i.e. cotau + tx), and then the 2nd element with the 2nd level (i.e. cotau + tm) and so on. As shown above, I know how to do it if I enter manually the different levels, but I have no idea which function I should use so that each combination of factor will be used. See what I mean? - I would then like to run it in a loop or something for each variable. It is by itself not so complicated, but I don't know how to give the correct name to my list. I want the list containing the data for Asfc to be named list_Asfc. Here is what I tried: seq.num - c(seq(3,5,1)) #the indexes of the variables for(i in 1:length(seq.num)) { k - seq.num[i] name.num - names(ssfamed)[k] list - list() list[[1]] - ssfamed[ssfamed$SPECSHOR=='cotau'ssfamed$BONE=='tx', i] list[[2]] - ssfamed[ssfamed$SPECSHOR=='cotau'ssfamed$BONE=='tm', i] names(list) - c(cotau_tx, cotau_tm) #I have more and the 1st question should help me on that too } After names(list) I need to insert something like: name_list - list But I don't know how to give it the correct name. How do we change the name of an object? Or am I on the wrong path? Thank you in advance for your help. Ivan PS: if necessary: under Windows XP, R2.10. [[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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] Question on Merge/Lookup
Perhaps using the R merge() function, possibly twice in succession, will do the job. (merge() does a one to many relational join, but with only two dataframes at at time). Or, there is an R package that lets you use the SQL language on dataframes. I don't recall its name, but a search on R packages should turn it up -- if someone else doesn't provide the name sooner. -Don At 10:52 AM -0800 1/22/10, GL wrote: I need to merge three datasets and don't know how. If I were using SQL, I would use df3, look up the characteristics of each date in df1 and the value for each observation in df2. df1 - unique list of Dates and characteristics of those dates Date, MM, WW, DOW df2 - the raw data Date, Place, Value df3 - all posibile combinations of Date + Place (via expand.grid(unique(df2$Date),unique(df2$Place)) Date, Place I need to end up with: Date, MM, WW, DOW, PLace, Value (plug 0 if combo doesn't exist in raw data). Appreciate any help! -- View this message in context: http://*n4.nabble.com/Question-on-Merge-Lookup-tp1112384p1112384.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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] R ON Mac
However, for there to be a difference between Mac and PC means that something else is going on. I would guess that the PC is set to a LOCALE for which , is the default decimal separator, and the Mac is set to a LOCALE for which . is the default decimal separator. (If locale settings affect the separator, that is. I don't know for sure whether they do. See ?locales to start learning more...) Either that or the input files are different on the two different computers. -Don At 7:07 AM -0800 1/14/10, Corey Sparks wrote: Hi, it appears that your corrdinates contain commas, instead of decimal points, R sees the commas and immediatly thinks the data are text, you should replace the commas with decimal points in a text editor. Corey gedasg wrote: hello, I have strange error. gyliai-read.table(file.choose(),header=T) summary(gyliai) xy gylis 307577,08: 1 6124296,56: 1 3,00 : 59 308613,01: 1 6124353,50: 1 2,80 : 51 313800,45: 1 6124530,65: 1 3,10 : 36 313840,17: 1 6124970,20: 1 2,90 : 32 313864,05: 1 6124991,68: 1 2,70 : 22 313869,26: 1 6125009,34: 1 3,43 : 5 (Other) :393 (Other) :393 (Other):194 coordinates(gyliai)=~x+y Error in .checkNumericCoerce2double(obj) : cannot retrieve coordinates from non-numeric elements Your version of R is up to date Error in .checkNumericCoerce2double(obj) : cannot retrieve coordinates from non-numeric elements - whats is that I use Gstat, mass, and sp packages. any ideas why this error shows to me? I check it on windows couple days ago it worked fine, but not on my mac, I don't have windows pc at the moment :) so help me :) Gedas -- View this message in context: http://*n4.nabble.com/R-ON-Mac-tp1013829p1013925.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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] To add text in a matrix
In R, see ?connectionfor information about functions similar to matlab's fopen. If what you're trying to do is write information and the matrix to a file, then you can at least get started with something along the lines of: cat('This is my information\nand a second line of it\n',file='myfile') write.table(mymatrix, 'myfile',append=TRUE) See also the sink() function -Don At 11:04 AM +0100 1/14/10, carfer...@alum.us.es wrote: Dear colleagues, I would need to add text (some rows of information) in a matrix. For example, given this matrix 1 2 3 4 5 6 7 8 9 I would need to add this info: THIS IS AN EXAMPLE OF a 3x3 MATRIX 1 2 3 4 5 6 7 8 9 I have been looking for a function that works similar to fopen in matlab, but unfortunately I have not found It in R. Thank you in advance for your help! Carlos Fernandez __ 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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] Giving parameters from shell
Try for yourself and see. (it's not difficult to try) For example, put the line in both places, followed by print(args) and see what happens. -Don At 11:49 PM +0200 1/14/10, cihan inan wrote: Content-Type: text/plain Content-Disposition: inline Content-length: 1496 I want to learn one more thing. You said args = commandArgs(TRUE) should I write this sentence in my function or out of my function area ? can you give me an example .r file ? 2010/1/14 Paul Hiemstra p.hiems...@geo.uu.nl cihan inan wrote: Hi I want to give parameters for my function from the shell. I mean I defined a function like these: work1.R : myfunc - function(x,y) { z = x + y z } and now I want to use shell to give parameters like ./work1.R (3,5) to get sum 8. so what should I do? [[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. Hi, Add the following shebang line at the top of your script: #! /path/to/Rscript The following lines will get you the parameters passed on by the user: args = commandArgs(TRUE) cheers, Paul -- Drs. Paul Hiemstra Department of Physical Geography Faculty of Geosciences University of Utrecht Heidelberglaan 2 P.O. Box 80.115 3508 TC Utrecht Phone: +3130 274 3113 Mon-Tue Phone: +3130 253 5773 Wed-Fri http://*intamap.geo.uu.nl/~paul -- Özgürlük için...http://*www.*pardus.org.tr/ [[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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] wrong with using subset
I would suggest that first you look at the results of (as.numeric(as.character(dfpr2_r$pr2))) 0.2 (dfpr2_r$landa 10) by itself. Does it give all FALSE ? Then look at each of the parts separately. What are the results of (as.numeric(as.character(dfpr2_r$pr2))) 0.2 and dfpr2_r$landa 10 Are there any TRUE among the results? Does as.numeric(as.character(dfpr2_r$pr2)) give what you expect? -Don At 5:20 PM +0200 1/13/10, Ahmet Temiz wrote: hello is it wrong with this expression: subset(dfpr2_r,(as.numeric(as.character(dfpr2_r$pr2))) 0.2 (dfpr2_r$landa 10)) it gives nothing regards -- Open WebMail Project (http://*openwebmail.org) -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean. __ 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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] Ask for histogram
If I do b - rnorm(4332) hist(b,probability=T,breaks=30,col='lightblue',ylim=c(0,1)) rug(b) The plot looks entirely reasonable. As far as being different from SAS, perhaps SAS and R use different breakpoints, that is, different boundaries between the histogram bars. -Don At 11:58 AM -0600 1/13/10, Yi Du wrote: Hi, I use a vector of data to draw the histogram, but it is different from the graph by SAS. Can you check it for me please? b is a column vector of 4332 hist(b,probability=T,breaks=30,col='lightblue',ylim=c(0,1)) rug(b) When I used rug, I find the records are smaller than 4332. I don't know where I did wrong. Thanks. -- Yi Du [[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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] Directory operations
To initialize an list: foo - list() ## and then extend it as in your code foo - c(foo, list(x=1)) foo $x [1] 1 foo - c(foo, list(y=1)) foo $x [1] 1 $y [1] 1 At 7:16 PM +0530 1/10/10, anupam sinha wrote: Hi Uwe, Thanks for your suggestion . Here's my code. I am confused as to how to initialize an empty list . Here I have used pairlist() *list.files()-org_xml_dirs for (i in org_xml_dirs) { setwd(file.path(/home/anupam/Research/Anupam_data/ORG_XML_FILES/,i)) org_xml-list.files() for(j in org_xml) { graph_list-pairlist() graph-parseKGML2Graph(j,genesOnly=TRUE) graph_list-c(graph_list,list(graph)) } org_met_net-mergeKEGGgraphs(graph_list) met_org-igraph.from.graphNEL(org_met_net,name=TRUE) write.graph(met_org,/home/anupam/Research/Anupam_data/ORG_XML_FILES/i.metnet,format=c(NCOL)) } * This is giving an error: Error in UseMethod(xmlAttrs, node) : no applicable method for xmlAttrs I apologise for asking a Bioconductor list related question on this list. Can you figure out the problem ? Thanks in advance. Regards, Anupam On Sun, Jan 10, 2010 at 12:54 AM, Uwe Ligges lig...@statistik.tu-dortmund.de wrote: On 09.01.2010 19:04, anupam sinha wrote: Hi Jim, Thanks for your suggestion. I tried scripting but gives me an error. Can you tell me what am I doing wrong here ? * list.files()-org_xml_dirs Please do turn that arrow around for (i in org_xml_dirs){ + setwd(/home/anupam/Research/Anupam_data/ORG_XML_FILES/i)} There is no directory .../i Your probably want: setwd(file.path(/home/anupam/Research/Anupam_data/ORG_XML_FILES/, i)) Uwe Ligges Error in setwd(/home/anupam/Research/Anupam_data/ORG_XML_FILES/i) : cannot change working directory * On Fri, Jan 8, 2010 at 11:37 PM, jim holtmanjholt...@gmail.com wrote: ?list.files ?file.info ?setwd You can get a list of all the files in a directory (list.files) and then do a file.info to determine which ones are the directories you want to search. A list.files on that directory will give you the list of file names that you can then process. On Fri, Jan 8, 2010 at 12:41 PM, anupam sinhaanupam.cont...@gmail.com wrote: Dear all, I have this directory structure : Dir1 Dir2 Dir3 Dir4 . A.xml D.xmlG.xml B.xml E.xml H.xml C.xml F.xml I.xml Within each of these directories (Dir1, Dir2 etc) there are a num of xml files (A.xml, B.xml etc). What I want to do is to enter into the first directory read all the xml files do certain operations come out of the directory and do the same thing for another directory. Can anyone help me out ? Thanks in advance for any suggestions. Regards, Anupam Sinha [[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 http://*www.*r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? -- Graduate Student, Laboratory of Computtational Biology, Centre For DNA Fingerprinting And Diagnostics, 4-1-714 to 725/2, Tuljaguda complex Mozamzahi Road, Nampally, Hyderabad-51 [[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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] R matching lat/lon pairs from two datasets?
I guess you want the subset of data whose lat/long pairs are present in data2? Try renaming your columns so that V1 and V2 are the same in both data frames (either lat,long, or long,lat, but not one way in one dataframe and the other way in the other one. Then use merge() -Don At 5:37 PM -0700 1/4/10, Douglas M. Hultstrand wrote: Hello, I am trying to match lat/lon from one dataset with the lat/lon from a second dataset and use that rows data for calculations. I am using match, but this is finding the first match and not comparing the pair, how can I determine if the lat/lon are the same? See example below. Is there a better way to determine to a matching pair of lat/lon values? Example Datasets: data2 V1V2 V3 1 -123.76 47.82 8 2 -123.75 47.82 11 data[1:2] V1 V2 1 47.82 -123.76 2 47.82 -123.75 3 47.82 -123.74 4 47.82 -123.73 #Subset of current R code : lat - data$V1 lon - data$V2 yrs - c(1,2,5,10,25,50,100,200,500,1000) lon2 - data2$V1 lat2 - data2$V2 ppt2 - data2$V3 for(i in 1:length(lat2)) { loc - match(lat2[i],lat) loc2 - match(lon2[i], lon) print(loc); print(loc2) #Need to test to make sure loc equals loc2 freq_ppt - c(data[i,4],data[i,6],data[i,8],data[i,10],data[i,12],data[i,14],data[i,16],data[i,18],data[i,20],data[i,22]) print(freq_ppt) return_value - approx(freq_ppt,yrs,xout=data2[i,3]) print(return_value) } Thanks for your help, Doug -- - Douglas M. Hultstrand, MS Senior Hydrometeorologist Metstat, Inc. Windsor, Colorado voice: 970.686.1253 email: dmhul...@metstat.com web: http://*www.*metstat.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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] input a list into a function
It works for me, without changes. See below. But I can recreate your error if I type the plus signs at the beginning of second and third lines. For your example, don't type any plus signs, except for the one in print(g[i]+x)} By the way, nothing that you did fits the description input a list into a function, because you haven't created any lists (your g is not a list). -Don At 5:27 PM -0800 12/25/09, Cat Morning wrote: Content-Type: text/plain Content-Disposition: inline Content-length: 595 I want to input a list into a function. But i get the error Error in +{ : invalid argument to unary operator. How do I avoid this error? Here is an example of this problem: g = c(2, 4, 8, 16, 32, 64, 128, 122, 110, 86, 38, 76, 18, 36, 72, 10, 20, 40, 80, 26) for (i in 1:20) + {for (x in 0:20) + print(g[i]+x)} [1] 2 [1] 3 [1] 4 [1] 5 [1] 6 [1] 7 [1] 8 [1] 9 [1] 10 [1] 11 [1] 12 [1] 13 [1] 14 [1] 15 [1] 16 [1] 17 [1] 18 [1] 19 [1] 20 [1] 21 [1] 22 Error in +{ : invalid argument to unary operator [[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. for (i in 1:20) + {for (x in 0:20) + print(g[i]+x)} [1] 2 [1] 3 [1] 4 [1] 5 [1] 6 [1] 7 [1] 8 [1] 9 [1] 10 [1] 11 [1] 12 [1] 13 [1] 14 [1] 15 [1] 16 [1] 17 [1] 18 [1] 19 [1] 20 [1] 21 [1] 22 [1] 4 [1] 5 [1] 6 [1] 7 [1] 8 [1] 9 [1] 10 [1] 11 [1] 12 [1] 13 [1] 14 [1] 15 [1] 16 [1] 17 [1] 18 [1] 19 [1] 20 [1] 21 [1] 22 [1] 23 [1] 24 [1] 8 [1] 9 [1] 10 [1] 11 [1] 12 [1] 13 [1] 14 [1] 15 [1] 16 [1] 17 [1] 18 [1] 19 [1] 20 [1] 21 [1] 22 [1] 23 [1] 24 [1] 25 [1] 26 [1] 27 [1] 28 [1] 16 [1] 17 [1] 18 [1] 19 [1] 20 [1] 21 [1] 22 [1] 23 [1] 24 [1] 25 [1] 26 [1] 27 [1] 28 [1] 29 [1] 30 [1] 31 [1] 32 [1] 33 [1] 34 [1] 35 [1] 36 [1] 32 [1] 33 [1] 34 [1] 35 [1] 36 [1] 37 [1] 38 [1] 39 [1] 40 [1] 41 [1] 42 [1] 43 [1] 44 [1] 45 [1] 46 [1] 47 [1] 48 [1] 49 [1] 50 [1] 51 [1] 52 [1] 64 [1] 65 [1] 66 [1] 67 [1] 68 [1] 69 [1] 70 [1] 71 [1] 72 [1] 73 [1] 74 [1] 75 [1] 76 [1] 77 [1] 78 [1] 79 [1] 80 [1] 81 [1] 82 [1] 83 [1] 84 [1] 128 [1] 129 [1] 130 [1] 131 [1] 132 [1] 133 [1] 134 [1] 135 [1] 136 [1] 137 [1] 138 [1] 139 [1] 140 [1] 141 [1] 142 [1] 143 [1] 144 [1] 145 [1] 146 [1] 147 [1] 148 [1] 122 [1] 123 [1] 124 [1] 125 [1] 126 [1] 127 [1] 128 [1] 129 [1] 130 [1] 131 [1] 132 [1] 133 [1] 134 [1] 135 [1] 136 [1] 137 [1] 138 [1] 139 [1] 140 [1] 141 [1] 142 [1] 110 [1] 111 [1] 112 [1] 113 [1] 114 [1] 115 [1] 116 [1] 117 [1] 118 [1] 119 [1] 120 [1] 121 [1] 122 [1] 123 [1] 124 [1] 125 [1] 126 [1] 127 [1] 128 [1] 129 [1] 130 [1] 86 [1] 87 [1] 88 [1] 89 [1] 90 [1] 91 [1] 92 [1] 93 [1] 94 [1] 95 [1] 96 [1] 97 [1] 98 [1] 99 [1] 100 [1] 101 [1] 102 [1] 103 [1] 104 [1] 105 [1] 106 [1] 38 [1] 39 [1] 40 [1] 41 [1] 42 [1] 43 [1] 44 [1] 45 [1] 46 [1] 47 [1] 48 [1] 49 [1] 50 [1] 51 [1] 52 [1] 53 [1] 54 [1] 55 [1] 56 [1] 57 [1] 58 [1] 76 [1] 77 [1] 78 [1] 79 [1] 80 [1] 81 [1] 82 [1] 83 [1] 84 [1] 85 [1] 86 [1] 87 [1] 88 [1] 89 [1] 90 [1] 91 [1] 92 [1] 93 [1] 94 [1] 95 [1] 96 [1] 18 [1] 19 [1] 20 [1] 21 [1] 22 [1] 23 [1] 24 [1] 25 [1] 26 [1] 27 [1] 28 [1] 29 [1] 30 [1] 31 [1] 32 [1] 33 [1] 34 [1] 35 [1] 36 [1] 37 [1] 38 [1] 36 [1] 37 [1] 38 [1] 39 [1] 40 [1] 41 [1] 42 [1] 43 [1] 44 [1] 45 [1] 46 [1] 47 [1] 48 [1] 49 [1] 50 [1] 51 [1] 52 [1] 53 [1] 54 [1] 55 [1] 56 [1] 72 [1] 73 [1] 74 [1] 75 [1] 76 [1] 77 [1] 78 [1] 79 [1] 80 [1] 81 [1] 82 [1] 83 [1] 84 [1] 85 [1] 86 [1] 87 [1] 88 [1] 89 [1] 90 [1] 91 [1] 92 [1] 10 [1] 11 [1] 12 [1] 13 [1] 14 [1] 15 [1] 16 [1] 17 [1] 18 [1] 19 [1] 20 [1] 21 [1] 22 [1] 23 [1] 24 [1] 25 [1] 26 [1] 27 [1] 28 [1] 29 [1] 30 [1] 20 [1] 21 [1] 22 [1] 23 [1] 24 [1] 25 [1] 26 [1] 27 [1] 28 [1] 29 [1] 30 [1] 31 [1] 32 [1] 33 [1] 34 [1] 35 [1] 36 [1] 37 [1] 38 [1] 39 [1] 40 [1] 40 [1] 41 [1] 42 [1] 43 [1] 44 [1] 45 [1] 46 [1] 47 [1] 48 [1] 49 [1] 50 [1] 51 [1] 52 [1] 53 [1] 54 [1] 55 [1] 56 [1] 57 [1] 58 [1] 59 [1] 60 [1] 80 [1] 81 [1] 82 [1] 83 [1] 84 [1] 85 [1] 86 [1] 87 [1] 88 [1] 89 [1] 90 [1] 91 [1] 92 [1] 93 [1] 94 [1] 95 [1] 96 [1] 97 [1] 98 [1] 99 [1] 100 [1] 26 [1] 27 [1] 28 [1] 29 [1] 30 [1] 31 [1] 32 [1] 33 [1] 34 [1] 35 [1] 36 [1] 37 [1] 38 [1] 39 [1] 40 [1] 41 [1] 42 [1] 43 [1] 44 [1] 45 [1] 46 -- - Don MacQueen Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 m...@llnl.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] write.csv and col.names=F
At 9:37 PM +0800 12/18/09, =?UTF-8?B?UmVleWFybl/mnY7mmbrmtItfMTA5MjgxMTM=?= wrote: On Fri, Dec 18, 2009 at 7:52 AM, kayj kjaj...@yahoo.com wrote: Hi All, I always have a problem with write.csv when I want the column names to be ignored, when I specify col.names=F, I get a header of V1 V2 V3 V4 etc. I tried that and found the same problem, however, I found write.table(mydata, file=data.csv,col.names=F) works. write.csv calls write.table to save data, is there something wrong with it? No. If you read the help page you will find that it answers your question. It says: 'write.csv' and 'write.csv2' provide convenience wrappers for writing CSV files. They set 'sep', 'dec' and 'qmethod', and 'col.names' to 'NA' if 'row.names = TRUE' and 'TRUE' otherwise. Notice that it sets col.names for you. If you want full control, use write.table(). If you want to write a table in a commonly used and pre-defined format (and without having to worry about various options) use write.csv(). That's why it is a called convenience wrapper. -Don -- Best Regards, Reeyarn T. Lee __ 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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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 reading binaries created with fortran
However, source code is available at http://water.usgs.gov/nrp/gwsoftware/modflow2005/modflow2005.html so it would seem that the details are available. -Don At 4:35 PM -0500 12/17/09, kapo coulibaly wrote: Duncan, I couldn't find clear details about the way fortran writes the binaries. I was hoping someone here has done it before. But the hexView package seems like a great idea i'll give it a shot. Thanks a bunch On Thu, Dec 17, 2009 at 4:02 PM, Duncan Murdoch murd...@stats.uwo.cawrote: On 17/12/2009 3:48 PM, kapo coulibaly wrote: Is it possible to read fortran binaries with R? I tried unsucessfully and my understanding is that fortran write binaries with leading and trailing bytes. I get numbers but not the right ones. Thanks ps: the binary I'm interested in reading is a MODFLOW output with a mix of character, double and integers. One other thing: the hexView package does a very nice job of displaying the file, so you can work out what the structure is if the documentation is unclear (or nonexistent). Duncan Murdoch [[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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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 print to file?
Use the online help: help.search('file') and then see: base::sink Send R Output to a File help.search('print') will get you there also, but not as directly. The cat() function is good also, as Gray mentioned. -Don At 2:12 PM +1800 12/19/09, Peng Yu wrote: I don't find a function to print a string to file. Would somebody let me know what function I should use? __ 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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] Why a list of NULL's are reduced to NULL?
I would study the third party function and learn all the types it can return. After all if you don't know what type it's going to return, how can you possibly make *any* confident use of its output? Do you actually have this situation? A third part function that is so poorly documented that you can't (1) predict the type of its return, and (2) learn what its possible outputs are? Is this function from an R package downloaded from CRAN? -Don At 12:24 PM -0600 12/11/09, Peng Yu wrote: On Fri, Dec 11, 2009 at 12:05 PM, hadley wickham h.wick...@gmail.com wrote: A very common situation is that the users don't know all the possible return types of 'some_third_party_function()'. If the users don't know all the return types, he/she can not make sure the return type of function(x) {...} be always the same. How do you deal with this case? It's not that common. It's pretty bad practice to return different types from a function depending on the input parameters. In many languages this isn't even possible. I know this is a bad practice. But R doesn't have a way to forbid such thing happen. To program defensively, I have to test even uncommon case, unless it is impossible. When you use a third party software in your code, do you just ignore the possibility that a function could return different types? The solution is to write a function that takes the output from the first function, inspects it, and coerces all possibilities to the same type. How do you figure out all the possibilities? You can look at its code. You can try the entire range of inputs you anticipate giving to it (you are in charge of the inputs, so you can do this). You can talk to the person who wrote it (if that person is not available, and the function is just some anonymous thing you got from somewhere, you probably shouldn't place any confidence it its correctness). __ 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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] Please help with a basic function
At 8:19 AM -0600 12/11/09, Mark Na wrote: Hello, I am learning how to use functions, but I'm running into a roadblock. I would like my function to do two things: 1) convert an object to a dataframe, 2) and then subset the dataframe. Both of these commands work fine outside the function, but I would like to wrap them in a function so I can apply the code iteratively to many such objects. Here's what I wrote, but it doesn't work: convert-function(d) { d-data.frame(d); #convert object to dataframe d-subset(d,select=c(time,coords.x1,coords.x2)) #select some columns } convert(data) #the problem is that data is the same as it was before running the function Objects (variables, data frames, etc) inside a function are in a different environment than those outside the function, and changes inside do not affect the outside unless you explicitly make it happen. In other words, you changed d inside the function, but did nothing to cause it to change data outside the function. Change your function like this convert-function(d) { d-data.frame(d); #convert object to dataframe ## no need to assign to d in the next line subset(d,select=c(time,coords.x1,coords.x2)) #select some columns } Then do: data - convert(data) Or the function could be convert-function(d) { d-data.frame(d); #convert object to dataframe d - subset(d,select=c(time,coords.x1,coords.x2)) #select some columns d } The use of return() as suggested in another response is unnecessary. Functions return the value of their last expression, though possibly invisibly if the last expression is an assignment. For example; ick - function(x) { x^2 } ick(4) [1] 16 ick(3) [1] 9 ick - function(x) { y - x^2 } foo - ick(4) foo [1] 16 The invisible return as in the second case is something I hadn't noticed before, and may deserve some reading of documentation. -Don The objects being processed through my function are SpatialPointsDataFrames but I'm quite sure that's not my problem, as I can process these outside of the function (using the above code) ... it's when I try to wrap the code in a function that it doesn't work. Thanks, Mark [[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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] Export R output to Word/RTF?
At 6:28 PM -0500 12/11/09, Wenjie Lee wrote: Hi R Experts, I'm aware of pdf(), jpeg(),... functions. But, 1. Is it also possible to export graphs directly to word or RTF? I use to copy and paste graphs but resolutions are not so great. I save graphs as png files, then use Word's Insert Picture (or equivalent) command. (Don't know what you mean by directly) 2. Also, is it possible to export your out to word file? I use sink() function to export it text files. I think you can export it to a text file whose name ends with .doc. Then Word will (should?) open it if you double-click on it. Any suggestions, thanks, Wenjie Lee [[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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] calculation problem when export and import data
After a=read.table(test.txt) do this: a - as.matrix(a) read.table() gives you a data frame. You are assuming it is a matrix, so change it into a matrix. -Don At 3:36 PM -0800 12/2/09, aegea wrote: Hello, I have a question on export and import data. Thank you for any suggestions. data 'simul' is generated as follows: N - 20 n - N/2 nsets - 10 simul - matrix(0,nsets,N) th- c(0,1, 1) for(i in 1:nsets){ simul[i,] - rnorm(N,mean= rep(th[1:2],N/2),sd=th[3]) } I exported data as follows: write.table(simul, file=D:\\test.txt, row.names=F, col.names=F) When I want to use this data, I imported as follows: a=read.table(D:\\test.txt) So far, it works well. When I deal with data, I need use each row to do calculations: for(rep in 1:nsets){ y - a[rep,] b-c(mean(y)+3, mean(y)-4) # cannot calculate mean(y), the mean of this row m-sd(y) # also cannot calculate sd(y) } I need a lot of calculation based on y, but after I imported data, R comes error on it. Could you please give me some suggestions? -- View this message in context: http://*n4.nabble.com/calculation-problem-when-export-and-import-data-tp947250p947250.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. -- - Don MacQueen Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 m...@llnl.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] sort a data frame by a vector
This looks like a job for match(). vec = c(C, A, B) dataDF = data.frame(A1 = c(B, A, C), A2 = c(1,2,3)) dataDF[match(dataDF$A1,vec),] A1 A2 3 C 3 2 A 2 1 B 1 -Don At 10:36 PM -0500 12/1/09, Hao Cen wrote: Hi, I have a a vector and a data frame with two columns vec = c(C, A, B) dataDF = data.frame(A1 = c(B, A, C), A2 = c(1,2,3)) I would like to sort the data frame by column A1 such that the order of elements in A1 is as the same as in vec. After the ordering, the data frame would be A1 A2 C 3 A 2 B 1 Any suggestions would be appreciated. Thanks in advance Jeff [[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. -- - Don MacQueen Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 m...@llnl.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] How do I run to or more R consoles on Mac OS X?
If myscript.r has as its first line: #! /usr/bin/Rscript and you make it executable chmod +x myscript.r Then you can run it by giving the command ./myscript.r -Don At 3:03 PM -0800 11/30/09, chronos.phenomena wrote: Thanks... that worked is there a way to run r script? for example open -n /Applications/R.app myscript.r when I use this syntax... I get my script file opened but not executed :( Thanks in advance Ken Knoblauch wrote: chronos.phenomena chronos.phenomena at gmail.com writes: This is really annoying me... when I click R application icon it brings already opened session in the focus and it DOESN'T open new session any ideas? In Leopard from a terminal, you can try open -n /Applications/R.app to open as many copies of the app as you like. Be careful though, because these inherit environment variables from the terminal session, not necessarily the same as those when running the app from the Finder. I was bitten by that the first time I tried this. Ken -- Ken Knoblauch Inserm U846 Stem-cell and Brain Research Institute Department of Integrative Neurosciences 18 avenue du Doyen Lépine 69500 Bron France tel: +33 (0)4 72 91 34 77 fax: +33 (0)4 72 91 34 61 portable: +33 (0)6 84 10 64 10 http://*www.*sbri.fr/members/kenneth-knoblauch.html __ 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. -- View this message in context: http://*n4.nabble.com/How-do-I-run-to-or-more-R-consoles-on-Mac-OS-X-tp930979p931723.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. -- - Don MacQueen Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 m...@llnl.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] explanation for left-side behaviour
Read the help page for substr(). It says that the first argument should be a character vector. The only one that works is the one where you gave it a character vector. You said only third one works. But you didn't explain what you mean by works. It's always a good idea on r-help to show both what you expected, and what you actually got, so that people can understand exactly what the question is. To explain a little further, let me number your three approaches. [1] substr(values,2,3) - .. [2] substr(as.character(values),2,3) - .. values - as.character(values) [3] substr(values,2,3) - .. With regard to case [1] It makes no sense to replace character substrings in a factor. factors are really numbers, not characters. It's just that they have additional attributes that make them (sometimes) print as if the were characters. But they're not. And the error message (that you didn't report) says exactly that. With regard to case [2]: values and as.character(values) are not the same thing. Therefore, replacing substrings in as.character(values) is not the same as replacing substrings in values. In this case, I would interpret the error message to indicate that R is trying to replace characters in a function. That makes sense, because you supplied a function, namely, as.character(). Case [3] works because you supplied a character vector. -Don At 9:57 AM +0100 12/1/09, Antje wrote: Hi there, I'm pretty sure that it's written down somewhere but I cannot find it so far. The little example shows different approaches to replace a substring. Only the last one works. I think it has something to do with the fact that substr is used on the left side. Can anybody refer to an explanation for this behaviour? Thanks a lot in advance! Antje values - factor(c(rep(abc,3), rep(bcd,3), rep(cde,3))) substr(values,2,3) - .. substr(as.character(values),2,3) - .. values - as.character(values) substr(values,2,3) - .. __ 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. -- - Don MacQueen Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 m...@llnl.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] sequence of commands in R
You don't see the standard deviations because only the final result (the output of summary() in your case) is output by the function, not the intermediate results (the results of the apply() function in your case). Try this: resumen-function(x) { print( apply(x,2,sd,na.rm=TRUE)) summary(x) } -Don At 9:17 PM +0100 11/29/09, Manuel Jesús López Rodríguez wrote: Dear all, I would like to know how could I execute a sequence or orders with just a function, i.e, that just typing the function name, R gives me all the parameters I want (for instance, if I want to see the summary, the standard deviation, the number of valid cases, etc of a dataframe just with one function). I have tried with the following, but just compute the second argument of the body, i.e., the summary: resumen-function(x) { apply(x,2,sd,na.rm=TRUE) summary(x) } Thank you very much for your help!! Manuel [[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. -- - Don MacQueen Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 m...@llnl.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] NAs introduced by coercion warning?
In my experience, it is generally due to something like this example: as.numeric(c('1','b','2')) [1] 1 NA 2 Warning message: NAs introduced by coercion There may be other operations that generate that error, I can't say for sure. But the above example illustrates what I look for when I get that particular message. -Don At 10:52 AM -0700 11/23/09, Omar Gonzalez Post wrote: I'm running cluster analysis on a data frame but when I calculate the distance I get this warning NAs introduced by coercion. What does this mean? [[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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] Convert Time Variable to Numeric
Perhaps something like: as.numeric(chron(times.=c('01:23:21','11:12:13'))) [1] 0.05788194 0.46681713 -Don At 7:02 AM -0800 11/24/09, RanjanV wrote: Hello Everyone I am new to R I would like to know how to deal with Time Variable. If I have a column of containing Time data in the format of hh:mm:ss such as 00:56:45, 01:24:36, 01:41:25, and so on.. I could find averages for this column using the Chron package. But now I need to plot a graph so I need to convert this Time variable to numeric. My data is as follows: Activity Time Required A00:56:45 B01:24:36 C01:41:25 D01:48:25 E01:51:25 F01:55:25 G01:59:25 and so on.. total rows in my data are 150 Can someone guide me regarding converting this Time variable to numeric? Thank you RanjanV -- View this message in context: http://*old.nabble.com/Convert-Time-Variable-to-Numeric-tp26497413p26497413.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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] Graphic Device - View/get all graphics
You might try putting readline('CR to continue') between the two sets of plot commands. -Don At 8:52 AM -0800 11/24/09, MarcioRibeiro wrote: Hi Listers, I am producing some graphics that the commands are in a FUNCTION... The problem is that I end up viewing just last graphic and in my FUNCTION there are 4 graphics with the PAR command function... Like those below... How do I view/get the other 3 graphics? Any help? Thanks in advance... histogram-par(mfrow=c(1,2)) hist(rw_mean_app,main='Bootstrap Method RWOriginal',xlab='Mean',ylab=' ') hist(rwy_mean_app,main='Bootstrap Method RWY',xlab='Mean',ylab=' ') par(histogram) histogram-par(mfrow=c(1,3)) hist(rw_median_app_ori,main='Bootstrap Method RWOriginal',xlab='Median',ylab=' ') hist(rw_median_app_mod,main='Bootstrap Method RWModified',xlab='Median',ylab=' ') hist(rwy_median_app,main='Bootstrap Method RWY',xlab='Median',ylab=' ') par(histogram) -- View this message in context: http://*old.nabble.com/Graphic-Device---View-get-all-graphics-tp26498560p26498560.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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] non-intuitive behaviour after type conversion
When you attach() something, it loads it into memory and there it stays. It is not a link, reference, or pointer to the original. Changing the original (the version in the dataframe), which is what you did, does not change the attached copy in memory. In essence, you did a type conversion on one copy, but afterwards started looking at the other copy. See also an interjected comments below. -Don At 8:54 AM + 11/23/09, Alan Kelly wrote: Deal list, I have a data frame (birth) with mixed variables (numeric and alphanumeric). One variable t1stvisit was originally coded as numeric with values 1,2, and 3. After attaching the data frame, this is what I see when I use str(t1stvisit) $ t1stvisit: int 1 1 1 1 1 1 1 1 2 2 ... This is as expected. I then convert t1stvisit to a factor and to avoid creating a second copy of this variable independent of the data frame I use: birth$t1stvisit = as.factor(birth$t1stvisit) if I check that the conversion has worked: is.factor(t1stvisit) [1] FALSE Now the only object present in the workspace in the data frame birth and, as noted, I have not created any new variables. So why does R still treat t1stvisit as numeric? is.factor(t1stvisit) [1] FALSE Yet when I try the following: is.factor(birth$t1stvisit) [1] TRUE So, there appears to be two versions of t1stvisit - the original numeric version and the correct factor version although ls() only shows birth as present in the workspace. Right. find('t1stvisit') will show you there are two of them, and where in memory they are located. If you type t1stvisit at the prompt, you always get the first one. The one in the attached dataframe is the second one. Use the search() function to show you the different locations in memory where objects can be found. When you did the attach(), did you get a message like: attach(tmp) The following object(s) are masked _by_ .GlobalEnv : x (yours would have referred to your variables, not the x in my example). That message tells you you have two variables of the same name, stored in two different locations in the search path. As a general rule, it's just plain confusing to have more than one object of the same name in more than one location. In your situation, I would get rid of the one that's not in the dataframe. But even then, if you change it in the dataframe you'll still need to detach and re-attach the dataframe, so using attach() is probably not the best choice in the long run. Maybe the with() function would meet your needs. If I type: summary(t1stvisit) Min. 1st Qu. MedianMean 3rd Qu.Max.NA's 1.000 1.000 2.000 1.574 2.000 3.000 29.000 I get the numeric version, but if I try summary(birth$t1stvisit) 123 NA's 180 169 22 29 I get the factor version. Frankly I feel that this behaviour is non-intuitive and potentially problematic. Nor have I seen warnings about this in the various text books on R. Can anyone comment on why this should occur? Many thanks, Alan Kelly Dr. Alan Kelly Department of Public Health Primary Care Trinity College Dublin __ 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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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 coordenates
See the spTransform function in the rgdal package. -Don At 7:05 PM -0500 11/20/09, Jimmy Martina Vasquez wrote: Hi everybody: Does anyone know how I can change geodesic coordenates into UTM ones? Thanks in advance for your cooperation, Jimmy M. _ Bing brings you maps, menus, and reviews organized in one place. a=TEXT_MFESRP_Local_MapsMenu_Resturants_1x1 [[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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] uniroot vs.optimize
Well, there is a difference between finding a root, and finding a minimum or a maximum. So you would use one or the other depending on which you need to do. -Don At 7:00 PM -0800 11/19/09, rkevinbur...@charter.net wrote: I looked at the descriptions for uniroot and optimize and they are somewhat different but the book reference is the same and I am wondering if there are reasons to pick one over the other? 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. -- - Don MacQueen Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 m...@llnl.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] R 2.10 memory leak on OS X
libSystem.B.dylib 0x97473012 thread_start + 34 Thread 3: 0 libSystem.B.dylib 0x97442286 mach_msg_trap + 10 1 libSystem.B.dylib 0x97449a7c mach_msg + 72 2 com.apple.CoreFoundation0x96a37e7e CFRunLoopRunSpecific + 1790 3 com.apple.CoreFoundation0x96a38aa8 CFRunLoopRunInMode + 88 4 com.apple.Foundation0x9447ecad -[NSConnection sendInvocation:internal:] + 3005 5 com.apple.Foundation0x9447de29 -[NSDistantObject forwardInvocation:] + 329 6 com.apple.CoreFoundation0x96ab784a ___forwarding___ + 986 7 com.apple.CoreFoundation0x96ab78b2 _CF_forwarding_prep_0 + 50 8 org.R-project.R 0xda65 -[RController readThread:] + 245 9 com.apple.Foundation0x94447dfd -[NSThread main] + 45 10 com.apple.Foundation0x944479a4 __NSThread__main__ + 308 11 libSystem.B.dylib 0x97473155 _pthread_start + 321 12 libSystem.B.dylib 0x97473012 thread_start + 34 Thread 0 crashed with X86 Thread State (32-bit): eax: 0x3c706f74 ebx: 0x96a690fe ecx: 0x0001 edx: 0x0007 edi: 0x00067ee2 esi: 0xf1c0a03d ebp: 0xbfffe6f8 esp: 0xbfffe6f0 ss: 0x001f efl: 0x00010282 eip: 0x07c7 cs: 0x0017 ds: 0x001f es: 0x001f fs: 0x gs: 0x0037 cr2: 0x00067ee2 #-- -- View this message in context: http://*old.nabble.com/R-2.10-memory-leak-on-OS-X-tp26422294p26422294.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. -- - Don MacQueen Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 m...@llnl.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] how can one break or stop or return from a script?
I don't know how to do this in the way you describe. Easy alternatives include: - putting the part of the script that is to be executed conditionally into a separate file, and then source it or not based on some condition. - simply wrapping the different parts of the script in if, then, else blocks. -Don At 1:37 PM -0800 11/16/09, Stu wrote: Hi, I am using a script to initialize variables in the global workspace. Based on some condition, I would like to stop evaluation of a script sourced on the command-line, without issuing an error. My current solution is the following hack that uses a repeat { } statement --- init.R --- #hack to enable setting of breakpoint repeat { ... if (condition) { break; } ... # remember to break !! break; } #end repeat EOF Thanks, - Stu __ 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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] Utility function to rotate log files?
I am wondering if there is a CRAN package that includes a utility function that will rotate file names, in the same sense that operating systems sometimes rotate log files. Or maybe there's something in base R. That is, we have a set of file names, say file1, file2, file3, and when the function is called, file3 is deleted, file2 is renamed file3, file1 is renamed file2, and the name of file1 is returned to the user's script, which then writes to file1. I've done some searching (RSiteSearch) but haven't found anything. Thanks -Don -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] Change working directory
In R for Macintosh, there is a Preferences setting that will do this. You can also drag and drop a file onto the R icon and I believe it will change the working directory to the directory that contains the file. On unix-like systems, using the command line, it's whatever directory you start R in. I don't use R on Windows, so I don't know there, but I imagine there may be a preferences setting, or perhaps the drag and drop method works. Or maybe create a shortcut in the directory you want to be the working directory? -Don At 6:34 AM -0800 11/13/09, anna_l wrote: Hello, I am using setwd() to change the working directory but I have to enter it everytime I open R, is there a way to set this permanently as a working directory? Thanx =^D -- View this message in context: http://*old.nabble.com/Change-working-directory-tp26337486p26337486.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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] adding zero to a number vector
Also, formatC(3,width=3,flag='0') formatC and sprintf are both referenced in the See Also part of the format help page. -Don At 9:42 AM -0600 11/9/09, Marc Schwartz wrote: On Nov 9, 2009, at 9:34 AM, anna freni sterrantino wrote: Hi ! I'd like to create a vector that has this kind of numeration 001 002 003 . . . 099 I have looked at format help page but couldn't get any hint on how to do it. Thanks Anna See ?sprintf sprintf(%03d, 1:99) [1] 001 002 003 004 005 006 007 008 009 010 011 [12] 012 013 014 015 016 017 018 019 020 021 022 [23] 023 024 025 026 027 028 029 030 031 032 033 [34] 034 035 036 037 038 039 040 041 042 043 044 [45] 045 046 047 048 049 050 051 052 053 054 055 [56] 056 057 058 059 060 061 062 063 064 065 066 [67] 067 068 069 070 071 072 073 074 075 076 077 [78] 078 079 080 081 082 083 084 085 086 087 088 [89] 089 090 091 092 093 094 095 096 097 098 099 Note the format specifies that the argument 1:99 should be formatted with leading zeroes to fill a 3 character width output. Importantly, note that the result is a character vector and not numeric values, though you can coerce back to numeric with ?as.numeric. HTH, Marc Schwartz __ 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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] Complicated For Loop (to me)
You're looking for the assign() function. See the first example in the help page for assign() Something like assign( paste( j,'.cd',i,'es.wash',sep='') , 1 ) instead of names.cd[i].es.wash - 1 paste() assembles the name as a character string, and then assign() assigns a value to a variable with that name. -Don At 8:41 AM -0800 11/9/09, agm. wrote: Hello, I'm trying to run a loop that will subset my data into specific sets by regions and by race/ethnicity. I'm trying to do this fairly compactly, and I cannot get this to work. A simple version of the code that I am trying to run is: names - c(white, black, asian, hispanic) for(j in names){ for(i in 1:9){ names.cd[i].es.wash - 1 es.cd[i].names.w - names.cd[i].es.wash +1 } ; } I want the loop to create these variables so that I would have for example: white.cd1.es.wash through white.cd9.wash and then the same for black, hispanic, and asian (However, none of these variables have been created yet outside of the loop) When I try to run this, I get the following error message: Error: unexpected symbol in: for(i in 1:9){ names.cd[i].es.wash Any idea what I am doing wrong? Thanks in advance for your help! I am still trying to get my hands around this programming syntax -- View this message in context: http://*old.nabble.com/Complicated-For-Loop-%28to-me%29-tp26269479p26269479.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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] Help with RGDAL
I did this recently with a kyngchaos binary build of rgdal, rgdal_0.6-12.tgz and R 2.9.2. I'll assume you have current rgdal binary package file in a directory somewhere. (looks like 0.6-18 is current) Here is what I did (with version 0.6-12). Note that I work from the command line, not from the R Gui, but it should work either way. cd to the directory containing the rgdal file. Start R (type R at the command line) At the R command prompt, give the command install.packages( 'rgdal_0.6-12.tgz' , type='binary', repos=NULL) That should do it, assuming any and all resources required by rgdal are already installed, and installed in places where the kyngchaos rgdal expects to find them. This is pretty well documented on the kyngchaos webpage, and it looks like you've been paying attention to that, since you already installed GDAL (the kyngchaos framework build of GDAL, right?) The kyngchaos build of rgdal may have other requirements besides GDAL, I don't remember. -Don At 10:51 AM -0700 10/30/09, Pablo Alvarez wrote: Hello, We (two mac users) have been attempting to install rgdal from http://*www.*kyngchaos.com/software:frameworks;, given that it is not available as a binary on the CRAN (binaries) of the Package Installer. I have also tried to solve this problem by looking on the net for an old question, and though I have found it, the answers do not help very much because our porgraming skills are embryonic. Our GIS professor has suggested we ask you guys how to proceed with the CRAN (source). Any suggestions or direction to an old document dealing with this? Thank you very much for your help, Pablo and Margarita PS. We have already installed: GDAL (for QGIS) and sp (for R) _ [[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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] convert list to numeric
An example will help show the difference between single vs. double brackets. ## xl is a list with three elements xl - list( a=1:3, b=2:7, c=c('X','Y')) xl $a [1] 1 2 3 $b [1] 2 3 4 5 6 7 $c [1] X Y ## with single brackets, we get a subset. A subset of a list is still a list xl[1] $a [1] 1 2 3 ## extract the first element of xl, as itself, whatever it is xl[[1]] [1] 1 2 3 ## with single brackets, we get a subset. In the next example the subset ## consists of the first and third elements of xl, i.e., a list having two elements xl[c(1,3)] $a [1] 1 2 3 $c [1] X Y ## Similarly for data frames, and note how the formatting is different when the object ## is printed to the screen xd - data.frame( a=1:3, b=2:4, c=c('X','Y','Z')) xd a b c 1 1 2 X 2 2 3 Y 3 3 4 Z class(xd) [1] data.frame xd[2] b 1 2 2 3 3 4 class(xd[2]) [1] data.frame xd[[2]] [1] 2 3 4 class(xd[[2]]) [1] integer At 6:22 AM -0800 11/2/09, dadrivr wrote: Great, that works very well. What is the purpose of double brackets vs single ones? I will remember next time to include a subset of the data, so that readers can run the script. Thanks again for your help! Benilton Carvalho wrote: it appears that what you really want is to use: task[[i]] instead of task[i] b On Nov 1, 2009, at 11:04 PM, dadrivr wrote: I would like to preface this by saying that I am new to R, so I would ask that you be patient and thorough, so that I'm not completely clueless. I am trying to convert a list to numeric so that I can perform computations on it (specifically mean-center the variable), but I am running into problems. I have imported the data set into task (data frame). The data frame is made of factors with variable names in the first row. I am running a loop to set a variable equal to a column in the data frame. Here is an example of my problem: for (i in 1:dim(task)[2]){ predictor.loop - c(task[i]) predictor.loop.mc - predictor.loop - mean(predictor.loop, na.rm=T) } I get the following error: Error in predictor.loop - mean(predictor.loop, na.rm = T) : non-numeric argument to binary operator In addition: Warning message: In mean.default(predictor.loop, na.rm = T) : argument is not numeric or logical: returning NA The column is entirely made up of numerical data, except for the header, which is a string. My problem is that I receive an error because the predictor.loop variable is not numerical, so I need to find a way to convert it. I tried using: predictor.loop - c(as.numeric(task[i])) But I get the following error: Error: (list) object cannot be coerced to type 'double' If I call the variable, I can assign it to a numerical list (e.g., predictor loop - task$variablename), but since I am assigning the variable in a loop, I have to find another way as the variable name would have to change in each loop iteration. Any help would be greatly appreciated. Thanks! -- View this message in context: http://*old.nabble.com/convert-list-to-numeric-tp26155039p26155039.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. -- View this message in context: http://*old.nabble.com/convert-list-to-numeric-tp26155039p26157105.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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] Help - Probability scale on an ECDF plot
You need to tell the mailing list what you have tried. That way you will get better help. Here is one solution. foo - rnorm(1000) probs - seq(0,1,.1) foo - rnorm(1000) bah - quantile(foo,probs) plot(bah,probs) But it turns out that ?ecdf tells you there is an ecdf function. And then plot(ecdf(foo)) gives an ECDF plot with a probability scale on the y axis. (So it's hard to imagine what you were doing that didn't give you a probability axis) -Don At 5:22 PM -0700 10/27/09, Jessica Goin wrote: Hello; Can anyone tell me how to set my y-axis to a probability scale on an ECDF plot? Or alternatively, how to generate a plot of percent cumulative probability against concentration? DASplusR does this and calls it a CP plot, but I would like to be able to generate this outside of DASplusR- Thank you! [[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. -- - Don MacQueen Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 m...@llnl.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] data frame is killing me! help
At 4:57 AM -0700 10/23/09, bbslover wrote: Steve Lianoglou-6 wrote: Hi, On Oct 22, 2009, at 2:35 PM, bbslover wrote: Usage data(gasoline) Format A data frame with 60 observations on the following 2 variables. octane a numeric vector. The octane number. NIR a matrix with 401 columns. The NIR spectrum and I see the gasoline data to see below NIR.1686 nm NIR.1688 nm NIR.1690 nm NIR.1692 nm NIR.1694 nm NIR.1696 nm NIR.1698 nm NIR.1700 nm 1 1.242645 1.250789 1.246626 1.250985 1.264189 1.244678 1.245913 1.221135 2 1.189116 1.223242 1.253306 1.282889 1.215065 1.225211 1.227985 1.198851 3 1.198287 1.237383 1.260979 1.276677 1.218871 1.223132 1.230321 1.208742 4 1.201066 1.233299 1.262966 1.272709 1.211068 1.215044 1.232655 1.206696 5 1.259616 1.273713 1.296524 1.299507 1.226448 1.230718 1.232864 1.202926 6 1.24109 1.262138 1.288401 1.291118 1.229769 1.227615 1.22763 1.207576 7 1.245143 1.265648 1.274731 1.292441 1.218317 1.218147 1.73 1.200446 8 1.222581 1.245782 1.26002 1.290305 1.221264 1.220265 1.227947 1.188174 9 1.234969 1.251559 1.272416 1.287405 1.211995 1.213263 1.215883 1.196102 look at this NIR.1686 nm NIR.1688 nm NIR.1690 nm NIR.1692 nm NIR. 1694 nm NIR.1696 nm NIR.1698 nm NIR.1700 nm how can I add letters NIR to my variable, because my 600 independents never have NIR as the prefix. however, it is needed to model the plsr. for example aa=plsr(y~NIR, data=data ,), the prefix NIR is necessary, how can I do with it? Perhaps using paste(). Maybe something like: paste('NIR', 1:600,sep=''.) or paste('NIR', seq(1686,1700,2),sep='.') I'm not really sue that I'm getting you, but if your problem is that the column names of your data.frame don't match the variable names you'd like to use in your formula, just change the colnames of your data.frame to match your formula. BTW - I have no idea where to get this gasoline data set, so I'm just imagining: eg. colnames(gasoline) - c('put', 'the', 'variable', 'names', 'that', 'you', 'want', 'here') -steve -- Steve Lianoglou Graduate Student: Computational Systems Biology | Memorial Sloan-Kettering Cancer Center | Weill Medical College of Cornell University Contact Info: http://*cbio.mskcc.org/~lianos/contact __ 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. thanks for you. but the numbers of indenpendence are so many, it is not easy to identify them one by one, is there some better way? -- View this message in context: http://*www.*nabble.com/data-frame-is-killing-me%21-help-tp26015079p26024985.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. -- - Don MacQueen Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 m...@llnl.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] replacing period with a space
There is also the fixed argument to gsub (read the help page!) gsub('.', ' ','a.b',fixed=TRUE) [1] a b -Don At 2:34 PM -0300 10/13/09, Henrique Dallazuanna wrote: You need escape the period: gsub(\\., , x$x) On Tue, Oct 13, 2009 at 2:26 PM, Dimitri Liakhovitski ld7...@gmail.com wrote: Dear R-ers! I have x as a variable in a data frame x. x-data.frame(x=c(aa.bb,cc.dd.ee)) x$x-as.character(x$x) x I am sorry for such a simple question - but how can I replace all periods in x$x with spaces? sub('.', ' ', x$x) - removes all letters to the left of each period... Thanks a lot for your advice! -- Dimitri Liakhovitski Ninah.com dimitri.liakhovit...@ninah.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. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O __ 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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] axis labels
See the 'mgp' argument to par(). You should be able to change mgp to get what you want. -Don At 5:58 PM +0100 10/13/09, jonas garcia wrote: Dear list, why does the distance between the axis labels and the tick marks looks different for x axis and y axis in the plot (see code below). In fact, the x axis labels look furthest from the tickmarks than in the y axis. How can I make them look the same? par(mfrow=c(1,1), cex.axis = 0.5, cex.lab = 0.5) plot(1,1, axes = F) axis(1) axis(2) Thanks in advance Jonas [[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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] Date-Time-Stamp input method for user-specific formats
something closer to what I expect. It is at least what looks like R's default text representation for POSIXct datetimes, even if it is not in my preferred format. spot[,1] [[1]] [1] 2009-09-01 BST [[2]] [1] 2009-09-01 00:00:01 BST [[3]] [1] 2009-09-01 00:00:02 BST [[4]] [1] 2009-09-01 00:00:03 BST -- View this message in context: http://*www.*nabble.com/Date-Time-Stamp-input-method-for-user-specific-formats-tp25757018p25757018.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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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 header csv file
in 1:nfiles)#Read first file { sink(directore where my text file is) filename-dir()[[year]]#take first file and read filename, so if year is 1, then filename will be 2004, is year is 2, filename will be 2005,... cat( Year: ,filename) sink() clima-read.csv2(filename, nrows=7) #open 2004.csv And in my text file would read Year: 2004 Now, I want to the same to months. (I have built a for loop to read months inside for loop to read years). When I import a csv file I get something like this JanurayFebruary 13.0 4.1 21.4 3.7 3 0.2 1.5 4 6.7 4.1 . . . I can use commands like clima$Januray or clima[[1]] but I just get precipitation values. However, I am not able to get the header of the column. If I would able to do that I could do the same as for years and export those headers to my text file. Does anyone know how I could do that? or does anyone know another way to do what I need? Would anyone use sink() and cat() commands to create a summary text like the one I need to do?. Probably my for loop is not the best, I am still a beginner with R, and probably there are some better forms to express in R what I need but I am working alone so there is nobody in person to help me so I apologize for my simple questions. Thanks in advance. Lucas _ [[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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] Bubble Plot
From looking at the code for bubble(), it doesn't appear there's any way to force special treatment of selected values. However, a simple work around would be to simply exclude the zero values from the plot(s) by subsetting your data creating a1 and a2. If you really want *no* representation, you're done. If you want them represented with the smallest dot size, then add them in afterwards with plot(), xyplot(), or spplot(), depending. Or, you could make a personal copy of bubble() [renamed, of course], and modify it to handle zeros in a special manner. That doesn't look too hard to do. -Don At 6:27 PM -0700 9/27/09, Marion Wittmann wrote: Hello, I am using the bubble plot and have been able to overlay two different data sets on the same graphic successfully. I would like to do the following and cannot: 1) suppress the zero values such that there is no representation of them on my plot (i.e., the zeroes show up as the smallest dot size, and I can't change this) 2) Give values to y or x axes with values, and labels My script looks as such: coordinates(data) = ~y + x a1 = bubble(data, Alive, zero.print = .,maxsize = 5.0, key.entries = 4*(1:6),col=c(0,3)) a2 = bubble(handcore, Dead, maxsize = 5.0, main = , key.entries = 5*(0:10),col=c(0,4)) print(b1, more = TRUE) print(b2, more = FALSE) Thanks in advance for your help. mw Marion Wittmann, Ph.D. Tahoe Environmental Research Center University of California Davis __ 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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] data frame's column names not the same as in CSV
At 1:58 AM -0400 9/26/09, Derek Foo wrote: Hello, I am trying to read in a csv file with column such as \\LS01\Processor(_Total)\% Processor Time with the command read.csv(file). However, the column name in the resulted data frame is changed to X..LS01.Processor._TotalProcessor.Time. Strangely, Not so strange. Data can be anything, but column names are names of variables. In R, as in most (all? many?) computer languages, variable names have rules they must follow. Yours don't follow R's rules. See Gabor's response to learn how to tell R to ignore the rules (in this particular instance). You will find, however, that later on, when you want to use those variables, it will be more difficult to use variables whose names do not follow the rules. when I experimented with just reading the csv with the head flag set to false, the text was read correctly as the same to the raw file. I am wondering if anyone has encountered a similar problem. If so, I would really appreciate if you can share your insight. Best Regards, Derek [[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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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 show number in the %f format?
There is also the formatC function, whose description is Formatting numbers individually and flexibly, using 'C' style format specifications. -Don At 2:28 AM -0400 9/24/09, David Winsemius wrote: On Sep 23, 2009, at 6:42 PM, Peng Yu wrote: On Wed, Sep 23, 2009 at 5:16 PM, David Winsemius dwinsem...@comcast.net wrote: On Sep 23, 2009, at 5:58 PM, Peng Yu wrote: Hi, I have the following matrix, which is printed %e format (in C's way). I am wondering how make it be printed in %f format (in C's way)? ??printf # scroll down to base package listings, the C function ?sprintf# the s/r function I tried the following command. The column names are missing and the command is a little complicated. Is there any better solution? t(apply(significant_analysis_results[,7:8],1,function(x){sprintf(%.7f,x)})) Why not apply to the column index? ... rather than to the row and then transposing. [,1][,2] Nab2 0.019 0.000 Rasal10.248 0.105 Ccndbp1 0.001 0.0002269 Svep1 0.000 0.000 Ppara 0.0008219 0.000 Pros1 0.009 0.000 Papss20.000 0.002 Hdac9 0.000 0.000 Adcyap1r1 0.000 0.000 Robo1 0.000 0.000 Sema3a0.000 0.000 Rab9b 0.110 0.011 Tgfb3 0.000 0.000 Slc9a90.0074608 0.000 Creb5 0.003 0.000 Ccnd1 0.0007869 0.001 Pafah1b3 0.000 0.068 Tiam2 0.000 0.000 Etv5 0.000 0.000 Hcrtr20.000 0.166 Regards, Peng significant_analysis_results[,7:8] pval(ki-wt) pval(ko-wt) Nab2 1.913348979e-06 2.731944670e-09 Rasal12.482254110e-05 1.054711084e-05 Ccndbp1 6.307674516e-08 2.268947934e-04 Svep1 0.0e+00 1.564526286e-12 Ppara 8.218961690e-04 2.802202914e-13 Pros1 8.787052919e-07 0.0e+00 Papss20.0e+00 2.190819073e-07 Hdac9 0.0e+00 8.881784197e-16 Adcyap1r1 2.085731587e-11 1.998401444e-15 Robo1 0.0e+00 0.0e+00 Sema3a4.903322193e-11 0.0e+00 Rab9b 1.099629676e-05 1.116694168e-06 Tgfb3 0.0e+00 0.0e+00 Slc9a97.460784795e-03 1.552167950e-09 Creb5 2.959174867e-07 8.973577437e-11 Ccnd1 7.868573521e-04 1.460805570e-07 Pafah1b3 1.576464070e-08 6.757446065e-06 Tiam2 0.0e+00 0.0e+00 Etv5 2.279731959e-12 0.0e+00 Hcrtr21.258646520e-10 1.661509722e-05 str(significant_analysis_results[,7:8]) num [1:20, 1:2] 1.91e-06 2.48e-05 6.31e-08 0.00 8.22e-04 ... - attr(*, dimnames)=List of 2 ..$ : chr [1:20] Nab2 Rasal1 Ccndbp1 Svep1 ... ..$ : chr [1:2] pval(ki-wt) pval(ko-wt) __ 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. David Winsemius, MD Heritage Laboratories West Hartford, CT __ 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. David Winsemius, MD Heritage Laboratories West Hartford, CT __ 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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] Reading data
What it is telling you is that it can't find the file. This could be because the file isn't there, or you've got a typo in the file name, that sort of thing. In your email, you have split the filename argument between two lines. I don't know whether this comes from what you did in R, or whether it was put there by the email software. If the former, try it without the line break, like this: rel - read.table(C:/Documents and Settings/ashta/My Documents/R_data/rel.dat, quote=,header=FALSE,sep=,col.names= c(id,orel,nrel)) NOT rel - read.table(C:/Documents and Settings/ashta/My Documents/R_data/rel.dat, quote=,header=FALSE,sep=,col.names= c(id,orel,nrel)) This is a bit of a wild guess, since I don't use R in Windows, and I don't know how the R GUI for Windows handles a line break in the filename in context of read.table(). But it could be the problem. Also, isn't there supposed to be a space between My and Documents? As best I can tell from your email, you don't have one -- but it's hard to tell because it's split into two lines. -Don At 8:42 AM -0400 9/23/09, Ashta wrote: Dear R-users, I am a new user for R. I am eager to lean about it. I wanted to read and summary of the a simple data file I used the following, rel - read.table(C:/Documents and Settings/ashta/My Documents/R_data/rel.dat, quote=,header=FALSE,sep=,col.names= c(id,orel,nrel)) summary(rel) Below is the error message, rel - read.table(C:/Documents and Settings/ashta/My Documents/R_data/rel.dat, quote=,header=FALSE,sep=,col.names= + c(id,orel,nrel)) Error in file(file, r) : cannot open the connection In addition: Warning message: In file(file, r) : cannot open file 'file=C:/Documents and Settings/sewalem/My Documents/R_data/rel.dat': Invalid argument summary(rel) Error in summary(rel) : object 'rel' not found Does it need a library? Where can I get the library? Any help is highly appreciated Ashta [[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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] Create directory and copy files in R
In your file.copy() command you have deskfile, but deskfile does not appear anywhere else. Perhaps you meant deskdir? After the error occurs, type traceback() and try to discern exactly where things went wrong. -Don At 5:05 PM +0300 9/21/09, Tammy Ma wrote: Content-Type: text/plain Content-Disposition: inline Content-length: 792 HI, All R users, My problem is: fn [1] C:/Documents and Settings/lma/Desktop/FamilyAEntrepreneurs/Entrepreneurs/Juha/book_log-20041210T095019.txt dpath [1] C:/Documents and Settings/lma/My Documents/Juha/book I want to make a function cyfun to copy all files in dir to deskdir but I always got the following problem: Error in file.exists(to) : invalid 'file' argument. Whats the problem?? cyfun-function(fn,dpath){ dir-dirname(fn) fn-basename(fn) deskdir-dir.create(dpath) file.copy(fn, deskfile) } Thanks! Tammy _ Drag n dropGet easy photo sharing with Windows Live Photos. http://*www.*microsoft.com/windows/windowslive/products/photos.aspx [[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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] xtable - print - suppress output
I think there is a conceptual issue here. The xtable() function does not actually create html. What it does is add some attributes to the dataframe that is given to it. Here's an example: tmp - data.frame( a =1:3, b= c('a','b','c') ) foo - xtable(tmp) class(foo) [1] xtable data.frame unclass(foo) $a [1] 1 2 3 $b [1] a b c Levels: a b c attr(,row.names) [1] 1 2 3 attr(,align) [1] r r l attr(,digits) [1] 0 2 2 attr(,display) [1] s d s The output of class(foo) tells us that foo is still a dataframe. It has two columns, a and b, just like tmp did. What it also has are some additional attributes, in this case some alignment information, some digits information, and some display information. And that is *all* that xtable() did. The real work is done by the function print.xtable(). This takes the dataframe and its additional attributes and prints it using either html or LaTeX syntax, depending on the 'type' argument. This is why xtable() has relatively few optional arguments, but print.xtable() has many. xtable() does not create html. print.xtable() creates html. In other words, there is no such thing as saving the html table into a variable. It just doesn't work that way. All that is possible is to write it (print it) to either the screen or a file. Which leads back to the question that one of the other responses asked ... what is the reason for saving it to an R object? What do you hope to accomplish by doing that, that you can't accomplish using print() ? Hope this helps -Don p.s. Besides xtable, other packages that help write html include Hmisc (already mentioned), hwriter, HTMLUtils, and R2HTML. Maybe one of them does things enough differently to do whatever it is you're looking for. At 12:13 AM +0200 9/22/09, Martin Batholdy wrote: Am 21.09.2009 um 23:59 schrieb Rolf Turner: On 22/09/2009, at 9:52 AM, Martin Batholdy wrote: hi, I use xtable to convert data.frames to html tables. But when I use the print-command I always get the whole output printed even if I just want to save the html table into a variable; table - print(xtable(CERAT), type=html) How can I suppress that output is printed? If you don't want it printed, then why the expletive deleted are you (explicitly!) using print??? Words fail me!!! cheers, Rolf Turner Because I don't get html code when I only use xtable(xy) ## Attention: This e-mail message is privileged and confidential. If you are not the intended recipient please delete the message and notify the sender. Any views or opinions presented are solely those of the author. This e-mail has been scanned and cleared by MailMarshal www.*marshalsoftware.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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] RODBC : using and passing queries that use in some arguments
Start by using single quotes in your paste() command. dumb example: sql - paste(' select x = 3 ') (the query is nonsense, I just wrote it to show how to get double quotes into the query) -Don At 6:45 PM +0200 9/21/09, BOISSON, Pascal wrote: Dear R users, I am trying to connect R to data that is in a Access Database but I have problem with the construction of queries using special characters. I am using RODBC package. The following is working : MyQuery-paste(SELECT first( (DateHeure) ) , avg(NNO3_AT322_OUT_moy) AS Cond FROM Colonne_3 ) Col3-sqlQuery(con, query=MyQuery) Col3 Expr1000 Cond 1 2009-06-23 10:15:02 579.9562 MyQuery-paste(SELECT first( format (DateHeure) ) , avg(NNO3_AT322_OUT_moy) AS Cond FROM Colonne_3 ) Col3-sqlQuery(con, query=MyQuery) Col3 Expr1000 Cond 1 23/06/2009 10:15:02 579.9562 But I have problems as soon as I want to use SQL functions in my query that use a double quote (eg FORMAT() function). I have no idea on how to build my sql instruction containing since the escape code \ does not seem to work/be sufficient in this case. (Nota : the argument I would like to use in the following call to format is 00 ) #Obvious error : MyQuery-paste(SELECT first( format(DateHeure, 00)) ) , avg(NNO3_AT322_OUT_moy) AS Cond FROM Colonne_3 ) Erreur : constante numérique inattendu(e) dans MyQuery-paste(SELECT first( format(DateHeure, 00 Expr1000 Cond 1 23/06/2009 10:15:02 492.0594 #Trying to solve the error using the escape code \ : MyQuery-paste(SELECT first( format(DateHeure, \00\)) ) , avg(NNO3_AT322_OUT_moy) AS Cond FROM Colonne_3 ) Col3-sqlQuery(con, query=cat(MyQuery)) SELECT first( format(DateHeure, 00)) ) , avg(NNO3_AT322_OUT_moy) AS Cond FROM Colonne_3 Erreur dans odbcQuery(channel, query, rows_at_time) : 'getEncChar' doit être appelé sur un CHARSXP Do you have any idea on how to proceed? With Best regards Pascal Boisson ___ Protegeons ensemble l'environnement : avez-vous besoin d'imprimer ce courrier electronique ? ___ Les informations figurant sur cet e-mail ont un caractere strictement confidentiel et sont exclusivement adressees au destinataire mentionne ci-dessus.Tout usage, reproduction ou divulgation de cet e-mail est strictement interdit si vous n'en etes pas le destinataire. Dans ce cas, veuillez nous en avertir immediatement par la meme voie et detruire l'original. Merci. This e-mail is intended only for use of the individual or entity to which it is addressed and may contain information that is privileged, confidential and exempt from disclosure under applicable law. Any use, distribution or copying of this e-mail communication is strictly prohibited if you are not the addressee. If so, please notify us immediately by e-mail, and destroy the original. Thank you. __ 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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] Replacing values in dataframes
What I would probably do is along these lines: iddf - data.frame(Sample.id=names(Prot.amount), new.id=Prot.amount[1,]) newNAD - merge( NAD, iddf) This is not tested, but it looks right to me, assuming I understand the structure of what you're trying to do. I'm also assuming that NAD has more than three rows, and that Prot.amount has as many columns as NAD has rows. And that you just showed us the first three rows of NAD and first three columns of Prot.amount in order to keep the email simple. One final note ... if Prot.amount is an object within R, it is *not* a file. You may have read it in from a file, of course, but it isn't a file inside R. I'm assuming it's a dataframe. -Don At 1:18 PM +0300 9/19/09, Monna Nygård wrote: Hi, This is a question of a newbie getting into the exciting world of R. I have several dataframes in the same format as NAD: NAD[1:3,1:3] Sample.Id Main.abs..1 Main.abs..2 148 10a 0.04836 0.04994 167 11a_1109 0.32245 0.36541 173 11b_1109 0.29293 0.32815 What I want to do is to replace the Sample.Id with a corresponding number.The number i have in another file,called Prot.amount Prot.amount[1:3,1] 10a 11a_1109 11b_1109 15.516 38.248 42.297 row.names(NAD)-(NAD[,1]) NAD$Sample.Id - replace(NAD$Sample.Id, NAD$Sample.Id==10a,Prot.amount[10a,1]) NAD[1:3,1:3] Sample.Id Main.abs..1 Main.abs..2 10a 15.516 0.04836 0.04994 11a_1109 11a_1109 0.32245 0.36541 11b_1109 11b_1109 0.29293 0.32815 So what I have tried to do is to write a function that would allow me to replace the values automatically of all dataframes. This I just can't get to work. Thank you so much in advance! _ [[elided Hotmail spam]] [[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. -- - Don MacQueen Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 m...@llnl.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] Any concept of 'by reference' or 'address of' in R?
I wouldn't assume R is making a copy; it depends on what you're doing with the dataframe. I'd suggest using Rprof to find out where the cpu time is being spent. If you can adapt your problem to use all numbers or all characters then you can store the data in a matrix instead of a data frame, and potentially speed things up. (That advice is based on my own experience from a fairly large number of years ago, so it's possible that developments in R since then could negate the advice.) -Don At 4:38 PM -0700 9/18/09, Mark Knecht wrote: Hi, Is there a way to pass the address of a data.frame through a set of functions in R? I've got some code which is slowing down I think because my data.frames are getting much larger - now approaching 1 million rows by 50-100 columns - and my functions - originally written for much smaller data elements are sampling these data.frames a couple of layers down. Before I rewrite them to do the sampling at the top level if there was a way to pass the address of the data.frame instead of the a copy of the whole thing then I suspect that would speed things up significantly. Thanks, Mark __ 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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] local sequence function
Try this: v - c(NA,NA,TRUE,TRUE,NA,TRUE,NA,TRUE,TRUE,TRUE) ick -rle(v) foo - unlist(apply(matrix(ick$lengths),1,seq)) foo[is.na(v)] - NA foo [1] NA NA 1 2 NA 1 NA 1 2 3 -Don At 5:20 PM +0200 9/14/09, smu wrote: hey, I can not find a function for the following problem, hopefully you can help me. I have a vactor like this one v = c(NA,NA,TRUE,TRUE,NA,TRUE,NA,TRUE,TRUE,TRUE) and I would like to the TRUE values by the their local sequence number. This means, the result should look thike this: c(NA,NA,1,2,NA,1,NA,1,2,3) Of course I could solve the problems using a loop, but this would be much to slow, because the real vector is much larger. Can you point me in the right direction? thank you! regards, Stefan __ 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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] Question about Factors
The suggestions from others to use lattice's xyplot, or ggplot2 are good. If you want an explicit loop you can do something like: for ( nm in unique(mydat$Name) ) { with( subset( mydf, Name==nm) , { plot(Time, Value, title=nm) readline('CR to continue ') } ) } At 5:19 PM -0700 9/13/09, Chris Li wrote: Hi all, I am new to R and I have got a question in regards to factors. Say I have a simple dataset like the following: Name Time Value a 1:00 1.25 a 2:00 1.26 b 1:00 1.29 b 2:00 1.28 c 1:00 1.21 c 1:30 1.20 c 2:00 1.23 I want to write a script that automatically plot value against time for a, b and c. Because I have got more than 1 datasets, therefore the name of the next dataset may consist d, e, h and g. So I will need a script that can detect the changes in Name automatically. Thank you very much for your time. Chris -- View this message in context: http://*www.*nabble.com/Question-about-Factors-tp25428665p25428665.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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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 recode with an if-type statement
I suppose there are a couple of ways... if (numbers==1) { distot - } else if (numbers ==2) { distot - } else if (numbers==3) { distot - } else distot - NA (letting you fill in the right hand side within each block) There's also distot - switch(numbers, w9zd9_1, (w9zd9_1 + w9zd9_2)/2, (w9zd9_1 + w9zd9_2 + w9zd9_3)/3, NA) In both cases I added an explicit result in case numbers is not one of the expected values. Generalizing this to an arbitrary value of numbers would be trickier. -Don At 11:05 AM -0400 9/14/09, Casey Klofstad wrote: I'm sure this is easy, but I'm having a hard time figuring out how to recode some data in R. I have a variable numpeers which is valued 1, 2, or 3. I also have three other variables called w9zd9_1, w9zd9_2, and w9zd9_3. I want to use these variables to create a new item called distot. Specifically, here is what I want to do: -if numpeers=1, then distot=w9zd9_1 -if numpeers=2, then distot=(w9zd9_1 + w9zd9_2)/2 -if numpeers=3, then distot=(w9zd9_1 + w9zd9_2 + w9zd9_3)/3 Thanks, in advance, for the help! -- Casey A. Klofstad University of Miami Department of Political Science Coral Gables, FL klofs...@gmail.com http://*www.*as.miami.edu/personal/cklofstad/ __ 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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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 determine if a variable is already set?
Do watch out, however, for *where* i exits. That is, if you type search() you will see a list of environments in which i might be found. You're probably assuming that i, if exists(i) is true, is in .GlobalEnv, but it might be in one of the other environments, in which case exists('i') will be TRUE, but it won't be the i you are looking for. I believe you need to quote then name also use:exits('i') not:exists(i) See ?exists (again) -Don At 10:19 AM -0700 9/12/09, carol white wrote: Thanks for your replies. I use the following script: if(!exists(i)) stop (set the variable i, call. = FALSE) but before the stop expression, Error gets displayed: Error: set the variable i Is there another function that stops the execution, prints an expression without printing Error or any other expression except the expression parameter? Best, --- On Fri, 9/11/09, Marc Schwartz marc_schwa...@me.com wrote: From: Marc Schwartz marc_schwa...@me.com Subject: Re: [R] how to determine if a variable is already set? To: carol white wht_...@yahoo.com Cc: r-h...@stat.math.ethz.ch Date: Friday, September 11, 2009, 10:21 AM On Sep 11, 2009, at 12:15 PM, carol white wrote: Hi, It might be a primitive question but how it is possible to determine if a variable is initialized in an environment? Suppose that we start a R session and wants to run a script which use the variable i. Which function could evaluate if i is already initialized or not and if not, then ask interactively the user to set it? This is to avoid the error message: object i is not found. Regards, Carol See ?exists Note that this will tell you if the object exists, not if it contains a specifically desired initial value. You would have to check for the latter after determining that the object does indeed exist. HTH, Marc Schwartz [[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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] Is there any month object like LETTERS ?
On my R 2.9.2 installation, ?Constants just says No documentation I don't know why Gabor and I would have different results. However, ?LETTERS does provide the requested information. -Don At 7:30 AM -0400 9/11/09, Gabor Grothendieck wrote: See ?Constants On Fri, Sep 11, 2009 at 3:13 AM, megh megh700...@yahoo.com wrote: There is an object LETTERS which displays all letters from a to z. Is there any similar object whicg displays the months as well in chronological order? like jan, feb,...,dec Thanks, -- View this message in context: http://*www.*nabble.com/Is-there-any-%22month%22-object-like-%22LETTERS%22---tp25396125p25396125.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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] tranform a table?
One could probably use one of the apply family of functions (apply, lapply, sapply), but having looked into it a bit, I think it's simpler to use an explicit loop. I doubt you'll encounter a need for greater efficiency, in the cpu time sense, unless your tables are huge. But the looping can be written more simply: require(xtable) df - data.frame(nm=letters[1:4], val1=1:4, val2=round(runif(4))) for (i in seq(nrow(df))) { print( xtable( t(df[i,])) , type='html', include.colnames=FALSE) cat('br') } One of the other packages for writing html from R objects might have a function that will automatically subset a dataframe in this particular way, but it strikes me as somewhat unlikely. -Don At 6:10 AM -0700 9/10/09, bbimber wrote: hello everyone, i'm new to R, so i hope you dont mind a fairly basic R question. we're using R to manipulate the results of SQL queries and create an HTML output. I'm starting with a table that looks essentially like this: NameField1 Field2 John value1 value2 Jane value3 value4 My table is stored as a dataframe. I'd like to efficiently produce an output that iterates through each row, transposes it and outputs an HTML table (one per row). like this: Name: John Field1: value1 Field2: value2 Name: Jane Field1: value3 Field2: value4 I can accomplish this by looping through each row, then outputting that row's table. This gets the job done, but it seems there must be a better way. I'm going to need to do this sort of conversion a lot, so the simpler the better. is there a better way to approach it than the code below? is there a more general term for the sort of transformation i'm trying to make that might help guide my searching? i realize i need to look into better methods of outputting HTML tables (like r2html). here's the basic idea. 'labkey.data' is the data frame produced by my SQL query: D-labkey.data H-colnames(D) T-t(D) L-length(D$id) output - for(i in 1:L) { R-my.row - D[i, ] R-t(R) Len-length(R) output - paste(output, table border=0) for(j in 1:Len) { output - paste(output,trtd, H[j],:/tdtd, R[j], /td) } output - paste(output, /tablep) } write(output, file=${htmlout:output}) Thanks for any help. -- View this message in context: http://*www.*nabble.com/tranform-a-table--tp25382806p25382806.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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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.csv in R with dynamic file (1st) argument
Alternatives to sprintf() include formatC() or prettyNum(), which would have to be used in combination with paste(). In Carl's solution, the file.path() function should be considered, since it automatically uses the correct path separators for the OS. Furthermore, eval() is not necessary. So, for example, x - formatC(scan(),width=4,flag='0') 1: 23 2: Read 1 item x [1] 0023 root.dir - 'D:\R\Data' read.csv( file.path(root.dir,x,paste(x,'.csv',sep=''))) ## those are all single quotes This isn't necessarily better than the sprintf() version, just different, and shows some additional useful functions(). This way of using formatC() requires that the user input only numbers: x - formatC(scan(),width=4,flag='0') 1: jk 1: Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, : scan() expected 'a real', got 'jk' I don't know what sprintf() would do with such input. -Don At 7:01 PM -0400 9/10/09, Carl Witthoft wrote: That will not work (or at least doesn't work for me. This does work: fnam-'thefilename.csv' #or build the name however you like fpath - 'macintoshhd/users/me/myfolder/ # or whatever you need read.csv(eval(paste(fpath,fnam,sep=)) #worked for me Carl --- Try this: read.csv(sprintf(D://R//Data//%04d//%04d.csv, x, x), header = TRUE) On Wed, Sep 9, 2009 at 9:32 PM, Steven Kang stochastick...@gmail.comwrote: Dear R users, I have numerous data sets (csv files) saved in the folder which has the same name as individual data. (i.e data x1 saved in x1 folder, data x2 in x2 folder etc) I would like to read in the desired data set name using 'scan' function and assign this inputted value to an object so that it can be used in the 'read.csv' function. For example, x - scan() 1: 0708 2: Read 1 item dat - read.csv(D://R//Data//x//x.csv, head=TRUE, sep = ,) Error in file(file, r) : cannot open the connection In addition: Warning message: In file(file, r) : cannot open file ('D://R//Data//x//x.csv': No such file or directory -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O __ 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. -- - Don MacQueen Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 m...@llnl.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] Select top three values from data frame
Do you want just the values (i.e., a vector), or do you also want the corresponding rows of the data frame? What if there is a tie, or do you know in advance that within any particular subset the values of B are unique? What if the subset that meets the constraints has fewer than 3 unique values? (which I think is the case in your example) tail( unique( sort( df$B[ df$A=='x' df$C 2 ] ) ) ,3 ) Should do it (but I haven't tested). Why does it get messy with over 100 columns? I'll pretend for the moment that you have exactly 100 columns: 1) you will be doing this many times, each time with a different sets of 3 columns? 2) you want the three highest values in each of 98 columns based on constraints on the other two? 3) you want the three highest values of B based on constraints on all of the other 99 columns? Depending on what changes when more columns are involved, you might be able to loop over columns with syntax like, for (nm in c('B','D','E') ) tail( unique( sort( df[[nm]][ df$A=='x' df$C 2 ] ) ) ,3 ) -Don At 1:36 AM -0700 8/26/09, Noah Silverman wrote: Hi, I'm trying to find an easy way to do this. I want to select the top three values of a specific column in a subset of rows in a data.frame. I'll demonstrate. ABC x21 x41 x32 y15 y26 y38 I want the top 3 values of B from the data.frame where A=X and C 2 I could extract all the rows where C2, then sort by B, then take the first 3. But that seems like the wrong way around, and it also will get messy with real data of over 100 columns. Any suggestions? __ 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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] Managing output
See minor comment below -Don At 8:07 PM + 8/26/09, Oliver Bandel wrote: - first part of email omitted - For many entries, it would be faster, if you just allocate the array that should be written, by just writing 0 into it, or empty strings or something like this... mydat[1:100] - 0 Simpler is mydat - numeric(100) mydat [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [38] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [75] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 mydat - 1 mydat[2] - 33 mydat[3] - -4 mydat [1] 1 33 -4 instead of mydat[1:100] - 0 use the length of the itemlist as second parameter. Ciao, Oliver __ 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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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 pass user input to a function?
And an alternative to nested if else's, in a case where they are actually needed, is switch -Don At 1:06 PM -0400 8/25/09, Ista Zahn wrote: Nice one Gabor! It works great, and is really simple. Bill Dunlap's solution of GET$pass.var - as.name(GET$pass.var) eval(substitute(mean(x), list(x=GET$pass.var))) also works. Thanks! On Tue, Aug 25, 2009 at 12:59 PM, Gabor Grothendieckggrothendi...@gmail.com wrote: Try this: x - get(GET$pass.var) mean(x) On Tue, Aug 25, 2009 at 12:26 PM, Ista Zahniz...@psych.rochester.edu wrote: Hi everyone, I'm building a website (http://*yourpsyche.org) using Jeffrey Horner's awesome Rapache module. I want to take user input, and pass it to an R script. At first I was simply using if else statements, but after a while I had so many nested if else's in my code that my head was spinning. So then I started using cat() and source() to write temporary files and read them back in (see example below). I've searched around, and I think there might be a better way to do it with substitute(), but I can't seem to figure it out (see attempt below). Here is a minimal example: ###set up simple example### GET - list(pass.var=b) a - 1:10 b - 11:20 ###using if else works but becomes confusing when I have a lot of variables to pass### if(GET$pass.var==a) + { + mean(a) + } else if(GET$pass.var==b) + { + mean(b) + } [1] 15.5 ###writing to a temporary file works but feels like a hack and results in many temp files### cat('print(mean(', GET$pass.var,'))', file=tmp.R,sep=) source(tmp.R) [1] 15.5 ###this seems promising but I can't figure it out### substitute(mean(x), list(x=GET$pass.var)) mean(b) ###is there a better way?### Thanks! -- Ista Zahn Graduate student University of Rochester __ 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. -- Ista Zahn Graduate student University of Rochester __ 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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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 apply a date format for data frame
Assuming your starts and ends are the number of days since some starting point (day 0), then following this example might do what you want: x - 45678 class(x) - 'Date' x [1] 2095-01-23 You will have to study the documentation to find out if your starting point is the same as R's, and if not, how to adjust. -Don At 11:42 PM -0700 8/24/09, rajclinasia wrote: Hi Everyone, i have a data frame like this labels starts ends priorities 1 firsttask 37987 38049 1 2 secondtask 38019 38112 2 3 thirdtask 38049 38144 3 4 fourthtask 38081 38207 4 5 fifthtask 38112 38239 5 now i want to apply a date format for the two variables they are starts and ends. please help in this aspect it would be appreciable. Thanks in Advance. -- View this message in context: http://*www.*nabble.com/how-to-apply-a-date-format-for-data-frame-tp25129192p25129192.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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] print selected variables
If your data is in a data frame named mydata and you want to print the 3rd, 5th and 10th variables: mydata[ , c(3,5,10) ] -Don At 12:38 AM -0700 8/20/09, rajclinasia wrote: Hi every one, I read one excel external file into R, in that R dataset i have 20 variables. now my querry is i want to print only selected variables (eg:10 variables) with complete data. pls send me the code it will be very helpful for us. Thanks in Advance. -- View this message in context: http://*www.*nabble.com/print-selected-variables-tp25057378p25057378.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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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] simple randomization question: How to perform sample in chunks
I believe this will do what you want: tmp1 - split(xx, xx$a) do.call(rbind, tmp1[ sample(length(unique(xx$a))) ]) The idea is to split the dataframe, and then reassemble in a random order. Whether or not it will be faster for a large dataframe, I don't know. There's probably also an indexing solution, perhaps using rle(), but I thought of this first... -Don At 6:22 PM +0300 8/20/09, Tal Galili wrote: Hello dear R-help group. My task looks simple, but I can't seem to find a smart (e.g: non loop) solution to it. Task: I wish to randomize a data.frame by one column, while keeping the inner-order in the second column as is. So for example, let's say I have the following data.frame: xx -data.frame(a= c(1,2,2,3,3,3,4,4,4,4) , b = c(1,1,2,1,2,3,1,2,3,4) ) I would like to shuffle it by column a, while keeping the order in column b. Here is my not-smart way of doing it: # R example xx -data.frame(a= c(1,2,2,3,3,3,4,4,4,4) , b = c(1,1,2,1,2,3,1,2,3,4) ) randomize.by.column.a - function(xx) { new.a.order - sample(unique(xx$a)) new.xx - NULL for(i in new.a.order) { xx.subset - xx[ xx$a %in% i ,] new.xx - rbind(new.xx , xx.subset) } return(new.xx) } randomize.by.column.a(xx) # END of - R example I would love for a better, faster, way of doing it. Thanks, Tal -- -- My contact information: Tal Galili Phone number: 972-50-3373767 FaceBook: Tal Galili My Blogs: http://*www.*r-statistics.com/ http://*www.*talgalili.com http://*www.*biostatistics.co.il [[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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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 do i vectorize relational queries in R
Does merge(data,meta) give what you want? FWIW: append to a dataframe would normally mean to add more rows (or at least that's how I use it), but you appear to be adding a column. data is the name of an R function, best avoided for other uses. You're assigning the column names the hard way. Try mydata - data.frame(Sample=c(1,1,1,2,2,2,3,3,3), Score=rep(2,9)) To avoid the factors, use meta - data.frame(Score=c(1,2,3), Stratum = I(c(Tree,Tree,Shrub)) ) ## that's an upper case letter i in I() or meta - data.frame(Score=c(1,2,3), Stratum = c(Tree,Tree,Shrub), stringsAsFactors=FALSE ) ## easily found in the help page for data.frame -Don At 3:21 PM -0700 8/19/09, chipmaney wrote: I am basically trying to append a value(vector) to one dataframe using a relational value from another dataframe. Obviously, I can use a loop to accomplish this. However, is there a way to vectorize it? Example: data - data.frame(c(1,1,1,2,2,2,3,3,3),rep(2,9)); names(data) - c(Sample,Score) meta - data.frame(c(1,2,3),c(Tree,Tree,Shrub)); names(meta) - c(Sample,Stratum) The following attempt at vectorizaton doesn't work: data$Stratum - meta$Stratum[which(data$Sample == meta$Sample)] data$Stratum [1] Tree NA NA Tree NA NA Tree NA NA And actually, when I try to run a loop, the operation converts the string to a factor. for (i in 1:length(data[,1])) data$Stratum[i] - meta$Stratum[which(meta$Sample == data$Sample[i])] data Sample Score Stratum 1 1 2 2 2 1 2 2 3 1 2 2 4 2 2 2 5 2 2 2 6 2 2 2 7 3 2 1 8 3 2 1 9 3 2 1 ArggI don't want a factor, and anyway I don't want to use a loop... Can anyone help with these two issues??? -- View this message in context: http://*www.*nabble.com/how-do-i-vectorize-relational-queries-in-R-tp25052929p25052929.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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062 __ 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.