Re: [R] gridding values in a data frame
dxc13 wrote: Hi all, I have a data frame that looks like such: LATITUDE LONGITUDE TEMPERATURE TIME 36.73 -176.4358.32 1 and this goes on for a A LOT more records, until time=1200 I want to create a 5 degree by 5 degree grid of this data, with the value of temperature in the appropriate grid cell. I want one grid for each time value. dxc13 The following appears to work, but is most definitely *not* in the spirit of R -- it's more like a C program written in R. Undoubtedly someone will come up with a much more clever method. (This is always my problem; I come up with a workable solution, but it's not elegant.) Note: the grid has to have 37 rows, since latitude -90 to +90 (inclusive) takes 37 5-degree increments; since -180 and +180 longitude are the same, you would never have them as separate numbers. d - read.csv(weather.csv) for (i in 1:1200) { x - d[d$TIME==i,] if (length(x$TIME) 0) { print(sprintf(# of elements for time %d: %d, i, length(x$TIME))) grid - matrix(NA, nrow=37, ncol=72) for (j in 1:length(x$TIME)) { lat - 1 + trunc((90 + x$LATITUDE[j]) / 5) long - 1 + trunc((180 + x$LONGITUDE[j]) / 5) grid[lat,long] - x$TEMPERATURE[j] } write(t(grid), file=sprintf(time%d.csv, i), ncolumns=72, sep=,) } } -- View this message in context: http://www.nabble.com/gridding-values-in-a-data-frame-tp23319190p23325204.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.
Re: [R] save history only option under Linux
alex lee wrote: Is there any options that I can save history only? The only save option is --save, which saves both data and history. However I want to restore the history for my next R session.. If there is no such option that meets my needs. Is there any other way to work out, such as configuration in the profile file, etc. ? You could start R with the --no-save option, and then do a savehistory() call as the last command before you quit. -- View this message in context: http://www.nabble.com/save-history-only-option-under-Linux-tp23257673p23267977.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.
Re: [R] first time poster
rilleyel wrote: i am trying to answer the following question, and having no luck: Focus your analysis on a comparison between respondents labeled “Low” (coded 1) on attend4 and respondents labeled “High” (coded 4). Then, examine the variance of distributions. That is, run a command var.test. I feel like I need to recode somehow and create 2 new variables, one for the low responses, one for the high responses. I do not know how to 'get into' the variable to deal with just the answers... I'm not sure I understand what you want, but I'm guessing you have a data frame with data like this, where the attend4 column has the high or low ratings, and you want to compare variances on some other variable (called score in this example): d - data.frame( attend4=c(1,4,1,1,4), score=c(20,30,21,22,24)) d attend4 score 1 120 2 430 3 121 4 122 5 424 To separate the scores for people who had low scores vs. high scores, do something like this: lows - d$score[d$attend4 == 1] lows [1] 20 21 22 highs - d$score[d$attend4 == 4] highs [1] 30 24 Now you can do a var.test on lows vs. highs. Hope this helps. -- View this message in context: http://www.nabble.com/first-time-poster-tp22745190p22762547.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.
Re: [R] windows vs. linux code
Sherri Heck wrote: I have been given some Rcode that was written using a Linux OS, but I use Windows-based R. The person that is giving it to me said that it needs to run on a Linux system. Does anyone have any insight and/or can verify this. I haven't yet obtained the code, so I haven't been able to try it yet. Linux and Windows use different end-of-line delimiters, but R doesn't care about that. If the code uses the system( ) call to directly invoke an OS command, then that code is almost certainly tied down to one operating system. For example, system(date) works fine on Linux, but doesn't on Windows. -- View this message in context: http://www.nabble.com/windows-vs.-linux-code-tp22215667p22215776.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.
Re: [R] windows vs. linux code
Sherri Heck wrote: i am asking if, in general, r code can be written on a linux-based system and then run on a windows-based system. Yes, if you avoid system-dependent calls like, um, system grin/, there should be no problem. For example, normal stuff like t test, correlation, plot, hist, barplot, matrix, etc. transfers nicely. CSV files created on one system can be read on the other. But to give a definitive answer for a particular case, we need to see the code. -- View this message in context: http://www.nabble.com/windows-vs.-linux-code-tp22215667p22215903.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.
Re: [R] array manipulation simple questions
Λεωνίδας Μπαντής wrote: 1. Suppose I have a-c(1:10) (or a-array(c(1:10),dim=c(1,10))) and I want to end up with vector b=[0 0 0 0 0 1 1 1 1 1]. i.e. I want to substitute alla elements that are 5 with 0 and 5 with 1. I think you mean =5, not 5. In that case, try this: b - ifelse( a 5, 0, 1) Λεωνίδας Μπαντής wrote: 2. Suppose I have a-c(1,1,2,2,3,3) (or array again) And I want to place a 4,5 before every 2 and end up with a new bigger vector b: b=[1 1 4 5 2 4 5 2 3 3] Also I want to find where the 2's in array a (if it was an array) are located i.e. positions 3,4. Not sure how to insert the 4,5, but this will find the where the 2's are: which(a == 2) -- View this message in context: http://www.nabble.com/array-manipulation-simple-questions-tp22173710p22175864.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.
Re: [R] how to make the figure more clearly
Abelian-2 wrote: Dear all when i draw some figures by R, i try to use jpeg to display my result. However, it isn't still clear as i want. Especially when i compare the origial figures by R and the .jpeg file JPEG is designed for photos (the P stands for Photographic). The compression method is good for images with smooth color variation. (In fact, the method is lossy -- it loses some of the original pixels, but in a photo, your eye can't detect the difference) On a line drawing, that same loss of information is readily apparent; the edges of text look fuzzy. Try PNG instead. Its compression method is designed to be very efficient with line art, and it doesn't lose any information. It will give a much more crisp image that usually takes up less disk space. A sample plot( ) that I did, saved both ways, gave a .jpg file of size 11014 bytes with a fuzzy look. The .png file was only 1957 bytes, and the lines and text had sharp edges. Try this: png(filename=something.png,width=600,height=400, pointsize=14) plot(x,y) dev.off() -- View this message in context: http://www.nabble.com/how-to-make-the-figure-more-clearly-tp22094071p22095089.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.
Re: [R] error bars
Nicole Hackman wrote: Hello, I have a very simple data set i imported from excel including 96 averages in a column along with 96 standard errors associated with those averages (calculated in excel). I plotted the 95 averages using r and I am wondering if it is possible to plot the second column of standard errors while applying error bars to each value so they represent the error corresponding to each average? You might also find http://users.fmg.uva.nl/rgrasman/rpages/2005/09/error-bars-in-plots.html this page to be useful; it doesn't require you to load any new packages. -- View this message in context: http://www.nabble.com/error-bars-tp22092367p22095134.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.
Re: [R] How to split a character vector into 3 vectors
kayj wrote: Hi , Does any one know how to split a character vector , I have a vector X that looks like this and each row has 3 characters X ASK DGH ASG AUJ FRT I would like to split the vector into 3 vectors that look like this X1X2 X3 A S K D G H A S G A U J U R T thanks If I understand you correctly, you have this vector: x - c(ASK, DGH, ASG, AUJ, URT) This code seems to do what you want: x1 - substr(x, 1, 1) x2 - substr(x, 2, 2) x3 - substr(x, 3, 3) There's probably a much simpler and more elegant way to do it, though. -- View this message in context: http://www.nabble.com/How-to-split-a-character-vector-into-3-vectors-tp21939492p21939521.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.
Re: [R] Loop on characters
megh wrote: suppose I have three vectors like : l1 = 1:4 l2 = 4:9 l3 = 16:67 now I want to construct a loop like : for (i in 1:3) { count1[i] = length(li) # i.e. it will take l1, l2, l3 according to value of i } Try this. There's probably a more elegant way to do it, but this works. l1 - 1:4; l2 - 4:9; l3 - 16:67 count1 - c( ) # start with an empty vector for (i in 1:3) count1[i] - length(get(paste(l,i,sep=))) count1 The call to paste( ) concatenates the letter l and value of i; they are separated by the null string (otherwise they would have the default blank separator between them). The call to get( ) fetches the object with that name. -- View this message in context: http://www.nabble.com/Loop-on-characters-tp21949173p21949383.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.
Re: [R] Choosing a random number between x and y
Vie wrote: Hi, Ive been trying to find a function that will allow me to pull out a number between a minimum and maximum threshold. I want a random decimal number between, for example, 0 and 0.5 or 0 and 0.7. I'm no R expert, but this should give you n uniformly distributed random numbers scaled down to the range 0..max where max 1 (and yes, I know, this makes it not-so-uniform): rrange - function(n, max) { result - runif(n) * max; result } Use it as follows: rrange(12, 0.7) # generate 12 numbers between 0 and 0.7 If you are looking for integer values from a minimum to a maximum (inclusive), this should work: irange - function(n, min,max) { result - min + trunc(runif(n) * (max - min + 1)); result } Used as follows: irange(12, 5, 20) # generate 12 integers in the range 5..20 inclusive -- View this message in context: http://www.nabble.com/Choosing-a-random-number-between-x-and-y-tp21914106p21918718.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.