Re: [R] R plot split screen in uneven panels
Dear Luigi Marongiu, Re: > Dear all, > I would like to draw two plots in the same device so that there is a > single row and two columns, with the first column being 1/3 of the > device's width. > I am creating a PNG object with width = 30 and height = 20 cm. > I know that I should use split.screen or layout but I am lost with the > matrix to pass to the functions. > For istance, I tried: > # distance in arbitrary units (so let's say cm) from of corners > # left, right, bottom, and top counting from bottom left corner > # that is first panel has the bottom right corner 20 cm from the bottom left? >> m = matrix(c(0,20,40,0, 20,60,40,0), byrow=T, ncol=4) >> m > [,1] [,2] [,3] [,4] > [1,]0 20 400 > [2,] 20 60 400 >> split.screen(m) > Error in par(split.screens[[cur.screen]]) : > invalid value specified for graphical parameter "fig" >> m[1,] > [1] 0 20 40 0 >> split.screen(m[1,]) > Error in split.screen(m[1, ]) : 'figs' must specify at least one screen > > What should be the syntax for this task? > > -- > Best regards, > Luigi > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. Screen parrts shoeld be fractions (0 ...1), not percentages. And bottom first, so ... m = matrix(c(0,20,0,40, 20,60,0,40)/100, byrow=T, ncol=4) ... will work. Success and Best regards, Franklin --- Franklin Bretschneider Dept of Biology Utrecht University brets...@xs4all.nl __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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] Identify does sort the locations
Hi Samuel GRANJEAUD, Isn't this loop more simple to retain the click-order: result = numeric(n) for (i in 1:n) { id = identify(x, y, n=1) result[i] = id } Hope this helps, Best regards, Franklin Bretschneider Dept of Biology Utrecht University brets...@xs4all.nl __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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] Override/Insert (Change) a value (default value) inside a function
Dear Mohammad Tanvir Ahamed, Re: > Hi!, > > Lets I have a function form a package. > The function is, as an example, > > myplot <- function(x,y) { plot(x,y) } > > > Now I can use the function according to function's defined argument. > > x<- sort(runif(200)) > y<- 1:200 > myplot(x,y) > > Now I want to input extra argument or override default value of plot inside > the function myplot. > > If I use myplot (x,y, col = "red", cex = 0.1 ) it does not work . I clearly > understand , why it does not work . > But in this situation how can i solve the problem ? > That's where the three-dot argument is for. See "Introduction to R", paragraph 10.4 The ‘…’ argument. Succes and best regards, Frank --- Franklin Bretschneider Dept of Biology Utrecht University brets...@xs4all.nl __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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] Euler & Runge-Kutta
Hello Tom Mosca, Re: > Can someone help me with R code to perform approximations to second order > differential equations and systems of first order differential equations > using Euler's method and Runge-Kutta? I am not a student and this is not for > a test or graded assignment. > > Examples (unrelated to each other): > > h = 0.1 > > 1. 3(t^2)y'' - 5ty' + 5y = 0 > y(1) = 0, y'(1) = 2/3 > > 2. Lotka-Volterra > x' = x(3-y) > y' = y(x-3) > For solving differential equations, the famous "lsoda" solver is best, far better than the simple Euler method, and also better than Runge & Kutta. Angels, or very nice people, implemented solving methods using the lsoda into R. Download the package "deSolve" and the world of numerical solutions is at your feet. Succes, Franklin - Franklin Bretschneider Dept of Biology Utrecht University brets...@xs4all.nl __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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] Same code on Mac?
Dear Tom Mosca, Re: > Using a PC I have written the R code for my elementary statistics students. > One of the students has a Mac. Should the same lines of code work on a Mac? > > > > Where can the student find support for R on her Mac? I don't know anything > about them, and have never used one. > Some commands are platform-dependent though, such as opening the standard graphics window [quartz() vs windows()]. This needn't be a problem, since in R a script can sense on which platform it is running. Some years ago I wrote a platform-sensing graphics routine, reproduced below. This might help to prevent problems with simple graphics demos, and can no doubt be extended to other commands. Success, and Best regards, Franklin Bretschneider Dept of Biology Utrecht University brets...@xs4all.nl # function PIgraph.r # Platform-Independent graphics init # determines platform (Windows or Mac) then chooses device type # F. Bretschneider; 04-08-2009 # PIgraph <- function(w,h) if(.Platform$OS.type == "windows") windows(w=w, h=h) else quartz(w=w, h=h) # example application x=-3:3 y=x^2 PIgraph(8,6) plot(x,y, type = 'o') __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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] as(, "numeric") vs as.numeric()
Dear Erik Wright, Re: > Could someone please explain this R behavior to me: > >> typeof(as.numeric(1:10)) > [1] "double" >> typeof(as(1:10, "numeric")) > [1] "integer" > > I expected "double" in both cases. In the help for the "as" function it says: > > "Methods are pre-defined for coercing any object to one of the basic > datatypes. For example, as(x, "numeric") uses the existing as.numeric > function." This happens because 1:10 yields only integers, and so can be stored cheap, whereas as.numeric() actually means: as.double. The "numeric" in the second line is an unused argument. Best regards, Frank --- Franklin Bretschneider Dept of Biology Utrecht University brets...@xs4all.nl __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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 editor for Mac
Dear Christofer Bogaso, Re: > Could you please suggest a good R editor for Mac OS X (10.7.5) Indeed, as Roy Mendelssohn wrote, the editor built into "R.app", the GUI program which is part of the standard R for OS X, has a beautiful editor, complete with syntax colouring and bracket balancing. And one can run only one or a few lines from a script at wish. I couldn't wish myself more. Success and best wishes, Frank -- Franklin Bretschneider Dept of Biology Utrecht University brets...@xs4all.nl __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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 request from Ph.D. Students
Dear NECMETTİN ALPAY KOÇAK, Re: > Dear All, > I am Ph.D. student in Econometrics. My thesis is about "Linear Filtering on a > Time Series" which R has already a nice package, namely "Filter". This > package is really helpful for my study. But, I really help from you to create > two filter using with "filter" package. > I want to create two filter desribed in attachment (effects word file) using > "filter" package. But, I dont know how? > > I really need your reply, > Sincerely, > > Alpay KOCAK > (etc...) I don't know a package called "filter", but a function "filter" is in the "stats" package There are however more packages to filter time data. I use "signal", which also has a "filter" function in addition to functions to design filters yourself (from simple first-order to higher-order butterworth etc). This package is intended to filter (electrical) signals, but might be used for any time series. In addition, there are several packages for the analysis of seasonal data. You might search CRAN for the names. Success and Best Wishes, Frank -- Franklin Bretschneider Dept of Biology Utrecht University brets...@xs4all.nl __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Problems trying to generate a prime factor vector
Hi Kevin Wright, Re: > > I am very new to 'R' and am trying to write an R function which returns the > prime factors of a given number(n) > > Unfortunately, the function only works for very small numbers, if for > example I pass 18 to the function > a mysteriously long vector is returned. I have not been able to find where > or why this is happening. > I know I've done something wrong. I've tried using debugging statements. > Sometimes the > currentPrime variable seems to become some sort of array?! > > > can you help? > > > library(gmp) > But why writing one? Such a function is already in gmp: factorize(n), and it works with fairly large numbers: >library(gmp) >factorize(12345678987654321) Big Integer ('bigz') object of length 13: [1] 2 2 2 2 5 7 11 73 101 109 109 137 167 Success, Frank -- Franklin Bretschneider Dept of Biology Utrecht University brets...@xs4all.nl __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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] [FORGED] Re: How to correct documentation?
Dear all, As to stating units in graphs: IMHO it should be as follows: If an axis reads 0 ... 10 ... 20 ... etc and the unit is pounds (lb), the legend should read "weight/lb" (pronounced "weight in pound"). The logic is: 10 lb/lb = 10. In orther words, dividing a dimensioned number by the dimension leaves the bare number, which is what the axis shows. So in the case of the OP the legend should read: "weight/1000lb". Thus, 2 lb/1000lb = 20, which is what the axis shows. Best, Frank ---- Franklin Bretschneider Dept of Biology Utrecht University brets...@xs4all.nl __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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] Command to input a variable value in real time
Dear nicholas.wray, Re: > Hi I am sure that there is a command in R which tells the prog to wait until > you have input a value for a variable, but for the life of me I can't find it. > Searches on the net only seem to talk about inputting datasets etc, not about > real time single inputs. I'd be most grateful if anyone could point me in the > right direction > > Thanks, Nick Wray > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. Maybe you need "readline()" in base-R: > > readline {base} R Documentation > Read a Line from the Terminal > > Description > > readline reads a line from the terminal (in interactive use). > > Usage > > readline(prompt = "") > > Arguments > > prompt > the string printed when prompting the user for input. Should usually end with > a space " ". > Best wishes, Frank --- Franklin Bretschneider Dept of Biology Utrecht University brets...@xs4all.nl __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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] Opposite color in R
Dear Atte Tenkanen, Re: Hi, I have tried to find a way to find opposite or complementary colors in R. I would like to form a color circle with R like this one: http://nobetty.net/dandls/colorwheel/complementary_colors.jpg If you just make a basic color wheel in R, the colors do not form complementary color circle: palette(rainbow(24)) Colors=palette() pie(rep(1, 24), col = Colors) There is a package ”colortools” where you can find function opposite(), but it doesn’t work as is said. I tried library(colortools) opposite(violet) and got green instead of yellow and opposite(blue) and got yellow instead of orange. Do you know any solutions? Atte Tenkanen Actually, yellow and blue are complementary colours, but red and green aren't. The human visual system has three types of cones: red-sensitive, green-sensitive and blue-sensitive. (the labels are approximate, e.g. red-sensitive cones have their optimum sensitivity at a wavelength we might call orange, but for understanding colours, R-G-B is the useful standard designation). A certain combination of these three together, such as in sunlight, is seen as white. In the digital domain, the three colour channels of an image are usually scaled to 8-bit numbers, i.e. from zero up to and including 255. So, all three channels 255 makes white. Leaving one of the three colors out yields yellow (no blue), magenta (no green) and cyan (no red). The pairs yellow-blue, magenta-green and cyan-red are truly complementary colours. Colours are the result of the wavelength of the light, so one would expect colours to lie on a linear scale, from about 700 nm (red), through 550 (green) to about 440 nm (blue). There is a complication, however: the photosensitive pigment of our red cones has a second action peak past that of the blue cones, so past pure blue we see a sort of reddish blue, in other words violet or purple. Therefore, the colours can be plotted in a circle, where violet and purple fill the gap between blue and red. Using a combination of the three ground colors R, G and B, any desired colour shade can be composed. Orange, for example, consists of (approximately) all red and half green. - - - - - - - - - R has ample possibilities to compose colours or colour palettes, with which one can create (almost continuous) gradients or stepwise colour patches. Examples are col2rgb(): col2rgb(orange) [,1] red255 green 165 blue 0 col2rgb(violet) [,1] red238 green 130 blue 238 Cindy Brewer wrote a fine set of colour functions, adapted to R by Erich Neuwirth. See package RColorBrewer. And much can be done with the standard R distribution: The following code plots a some colours in a circle, with the complementary colours at opposite sides (so crudely what you're after): # define colour triplets reds = c( 255, 255, 255,0,0,0,0, 128) greens = c( 0, 127, 255, 255, 255, 127,0,0) blues= c( 0, 0,0,0, 255, 255, 255, 255) n = length(reds) # compute circle to plot in stp = 2*pi/n th = seq(0,2*pi-stp, length.out=n) x = cos(th); y=sin(th) # plot (on a Mac, for other OSses call the appropriate grahics window quartz(w=5, h=5) par(xpd=NA) plot(x,y,pch=15, cex=8, col=rgb(reds, greens, blues, maxColorValue = 255), asp=1, axes=FALSE, xlab='', ylab='') points(x,y,pch=0, cex=8, col=black) # arrows connect the complementary colours arrows(0,0, 0.7*x, 0.7*y, length = 0.25, col = grey) Hope this helps; Best wishes, Frank -- Franklin Bretschneider Dept of Biology Utrecht University brets...@xs4all.nl __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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] Cross correlation between two time series over nested time periods?
On 2015-05-14 , at 02:11, Tim via R-help r-help@r-project.org wrote: Hello Tim, Re: I have two time series Calculate and plot cross correlation between two time series over nested time periods. Each point in either time series is for a week (not exactly a calendar week, but the first week in a calendar year always starts from Jan 1, and the other weeks in the same year follow that, and the last week of the year may contain more than 7 days but no more than 13 days). The first time series A is stored in a compressed (.gz) text file, which looks like (each week and the corresponding time series value are separated by a comma in a line): week,value 20060101-20060107,0 20060108-20060114,5 ... 20061217-20061223,0 20061224-20061230,0 20070101-20070107,0 20070108-20070114,4 ... 20150903-20150909,0 20150910-20150916,1 The second time series B is similarly stored in a compressed (.gz) text file, but over a subset of period of A, which looks like: week,value 20130122-20130128,509 20130129-20130204,204 ... 20131217-20131223,150 20131224-20131231,148.0 20140101-20140107,365.0 20140108-20140114,45.0 ... 20150305-20150311,0 20150312-20150318,364 I wonder how to calculate the cross correlation between the two time series A and B (up to a specified maximum lag), and plot A and B in a single plot? The auto- and crosscorrelation functions are in the stats package: acf(x, lag.max = NULL, type = c(correlation, covariance, partial), plot = TRUE, na.action = na.fail, demean = TRUE, ...) ccf(x, y, lag.max = NULL, type = c(correlation, covariance), plot = TRUE, na.action = na.fail, ...) See further: ?ccf Succes and Best wishes, Frank --- Franklin Bretschneider Dept of Biology Utrecht University brets...@xs4all.nl __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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 problem with string handling to make a time duration
Hello gavinr, I have a character string that represents a time duration. It has an hours minutes seconds structure(ish) but with letters denoting units (H,M or S) no leading zeros and no placeholder at all where one or other of the units are not required. It looks like this: t-c(10H20M33S,1H1M,1M,21M9S,2H55S )) df-data.frame(t) df #ideally should look like: t2-c(10:20:33,01:00:01,00:01:00,00:21:09,02:00:55) df2-data.frame(t2) df2 I need to get it into hours minutes and seconds either in time format or as a string with leading zeros and all three time units represented in each one, as in df2. The data, part of a very large dataset, are for onward use and processing in a GIS application. I’ve messed about with string handling statements in SQL to no avail, but wondered if R would be a better bet? I’ve had a look at some of the commands in stringr, but am unsure how to operationalise a solution using this package. Any advice is welcome. This can be done easily with the substring function, e.g. # say: string=12H15M45S #then pick: h=substr(string,1,2) m=substr(string,4,5) # and join again: newstr = paste(h,m,sep=:) # etcetera Success and Best regards, Frank -- Franklin Bretschneider Dept of Biology Utrecht University brets...@xs4all.nl __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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-SIG-Mac] Unsubscribe from the list
Hi Silvia Solis, Re: Hi, I wanted to ask you to scratch this email address from the mailing list. This is a work address and I already receive your emails con my personal account. My mistake this one time I replied from a different email address. Thanks! Silvia -- [[alternative HTML version deleted]] ___ R-SIG-Mac mailing list r-sig-...@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-mac The instructions to unsubscribe are at the end of every message. It is: __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help Succes, Frank --- Franklin Bretschneider Dept of Biology Utrecht University brets...@xs4all.nl __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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] FFT Normalization Documentation
Dear Elke; Jeff, Re: Eike: Understanding Discrete Fourier Transform theory is not trivial... while a vignette added to the stat package has the potential help a lot of users, it is a bit ambitious to try to supplant the extensive published material on using and interpreting the DFT (particularly as there is more than one way to do it and the R fft() function is very typical of fft implementations). (Similar arguments could be applied to most of the stat package... note the absence of vignettes there.) It might be more practical to propose to R-devel some patches to the fft() help file references and examples sections. Alternatively, you could write YAB (Yet Another Blog) for people to search for. Frank: While folding is an important concept to know about when interpreting DFT results, I think something went rather wrong in your example with your mask variable since folding applies to f (for forward fft) or t (for inverse fft), not to the corresponding magnitudes. In addition to that, it is simply not necessary to pre-fold your data before applying the fft... the folding is assumed by the math to exist in the input outside the input window, and there is nothing you can do to the data to affect that assumption. Folding in the output is more visibly evident, but presenting it as a symmetric plot is entirely optional and is not done in most cases. Maybe I didn't use the proper terminology, but what I called 'folding' is a modification of the input signal used only to present the amplitude spectrum in a convenient way. The FFT (butterfly algorithm) yields a complex array where the highest frequencies (pos and neg) are in the middle, the lowest (and DC and fNyq) are at the ends. To display this same array with the DC value in the middle, the neg frequencies increasing to the left and the pos frequencies to the right, the trick with the +1/-1 mask is performed. This mask function is in fact a square wave at the Nyquist frequency. In Matlab, it is in a routine called fftshift, see here: Y = fftshift(X) rearranges the outputs of fft, fft2, and fftn by moving the zero-frequency component to the center of the array. It is useful for visualizing a Fourier transform with the zero-frequency component in the middle of the spectrum. This is from the MathWorks web site: http://nl.mathworks.com/help/matlab/ref/fftshift.html. In addition, in my example I forgot to scale the amplitude. This must indeed be divided by n (the number of data points). So, change my line YY - fft(yy) into YY - fft(yy)/n. Now the amplitudes of the spectral line are numerically the same as given in the composition of y. These values must indeed be regarded with caution, since with real-world signals the energy will most often be spread among several spectral lines. Windowing (Hann, Hanning, Blackman etc.) then improves the spectrum, but that's a different story. Best wishes, Frank --- Franklin Bretschneider Dept of Biology Utrecht University brets...@xs4all.nl __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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] FFT Normalization Documentation
Dear Eike Petersen, Re: Hello everyone, the docpage for the fft function states: “Description: Performs the Fast Fourier Transform of an array.” and “Arguments – inverse: if ‘TRUE’, the unnormalized inverse transform is computed (the inverse has a ‘+’ in the exponent of e, but here, we do _not_ divide by ‘1/length(x)’).” Judging from this, I would expect ‘fft(x)’ to yield the correct FFT of x, and ‘fft(X, inverse = TRUE) / length(X)’ to yield the correct inverse FFT of X. However, it seems to me that actually the result of ‘fft(x)’ should be scaled by ‘1/length(x)’, while ‘fft(X, inverse=TRUE)’ seems to yield a correct result by default: t - seq(0.001, 1, 0.001) y - 1 + sin(2*pi*20*t) + 1.5 * sin(2*pi*30*t) Y - fft(y) dev.new() plot(abs(Y)) ## Shows peaks at amplitudes 1000, 500 and 750, while they should be at amplitude 1, 0.5 and 0.75. y2 - Re(fft(Y / length(Y), inverse = TRUE)) max(abs(y-y2)) ## The IFFT yields a correctly scaled result by default, if applied to a correctly scaled FFT. Did I get something wrong? If not, having spent quite some time figuring this out, I would like to see the documentation clearly pointing this out. I find the current text rather confusing. On another note: I have spent some time working on demo files that showcase some of the properties of the FFT and their implementation in R. I have done this primarily for myself, as I keep forgetting how these things work, but I thought that it might be helpful to others as well. Any hints on where/how I should publish such a thing? Kind regards and many thanks in advance, Eike __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. As far as I know an FFT must be normalized and folded to obtain a spectrum in the form we like, so this would be my version: # your time signal: t - seq(0.001, 1, 0.001) y - 1 + sin(2*pi*20*t) + 1.5 * sin(2*pi*30*t) # find time and frequency calibration: n = length(y) dt = t[2]-t[1] fNyq = 1/(2*dt) tmax = max(t) df = 1/tmax # make frequency vector to display as x-values of the spectrum rather than the index. f = seq(-fNyq, fNyq-df, by=df) # make folding mask mask=rep(c(1, -1),length.out=n) # fold the spectrum around the Nyquist frequency; so the DC value (f=0) is in the middle; the - and + max frequency at the ends. yy = y * mask # # Then do the FFT YY - fft(yy) Plot the amplitude spectrum vector against the freq. vector plot(f,abs(YY), type='h') It would be a good idea to put such an example in the help pages indeed. The short example given in the manual isn't of much help for the usual (periodic!) time signals. Hoping this helps, I remain With best wishes, Frank Franklin Bretschneider Dept of Biology Utrecht University brets...@xs4all.nl __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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 looping a specific code
On 2015-01-07 , at 16:29, Haznadar, Majda (NIH/NCI) [E] majda.hazna...@nih.gov wrote: Hi, I would like to loop a specific code through multiple files ending in .CDF in the same directory, but I am unsure of how to do that (new to R). Can someone please help me with this-thank you. This is the code I would like to loop: xr-xcmsRaw(mh141210xg2_sccs_00101.CDF, profstep=0) write.mzdata(xr, file=mh141210xg2_sccs_00101.CDF) And there are 675 of these .CDF files in the directory that I would like to apply the code to. Also, when I save the file in the second line of code, I would like it to have the same name as it previously had, so just re-write the old file. Thanks so much for your time!! Majda I use this function to find the appropriate file names, as follows: getFolder - function(pat) { txt=file.choose() #cat(txt,'\n') pos=0 fname=basename(txt) #cat(paste(\nFilename found is: ,fname,'\n')) foln=dirname(txt) cat(paste(\nFolder name found is: ,foln,'\n')) drtext=dir(foln, pattern=pat, full.names = TRUE) #cat('\n\n\n\n') return(drtext) } # it can be called with the appropriate extension as a pattern, e.g. .wav follist=getFolder(\\.wav) # this variable now contains all filenames (complete path names) of the selected type # and can be used t read in a loop: # for (i in 1:length(follist) { # rad ypur file # do something with it # view or save the results # } Maybe you can adapt this to your problem. Success and Best wishes, Franklin Bretschneider Dept of Biology Utrecht University brets...@xs4all.nl __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/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] quartz() and windows()
Dear Megersa , Which version of R is used for quartz function please? Quartz is the Apple Mac graphics window (device). On Windows use ... windows() Best wishes, Franklin Bretschneider Dept of Biology Utrecht University brets...@xs4all.nl __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] par(mfrow)
Hi, re: I have some problems using the par function: I want to split the screen into 2 rows and 4 col and I type par(mfrow=c(2, 4)) but when I do that, instead of setting a graphical parameter, it creates a white Quarz. I'm currently using the R base version for Mac Os, 3.0.3 . Could you give me an help? Thank you very much, But that's correct, if you don't plot something afterwards. Try plotting 8 x-y graphs, then you'll see that 8 small plot will appear (unless the margins will prove to be too large). Good luck, Franklin Bretschneider Dept of Biology Utrecht University brets...@xs4all.nl __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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-SIG-Mac] upgrade to Mavericks
Dear Richard M. Heiberger, Re: Apple has been popping an offer for a free upgrade to Mavericks. (I currently have OS X Lion10.7.5 on an 8GB MacBook Air). Other than this offer from Apple, I have no dissatisfaction with the Mac for my use pattern. There has been much comment on this list about the difficulties of compiling R with Mac. I am not planning to compile R. Are there any remaining issues with using R on the Mac that should make we want to not upgrade? Or is Mavericks now stable enough that upgrading would make sense. Mavericks seems OK: I bought a MacBook Air last december, and immediately put Mavericks on it. Since then, I'm running R (3.0.x) without any problems (as far as I remember). So it seems safe to upgrade (as to R; other software, such as Filemaker pro, might be incompatible, so read the forums). Best regards, Frank -- Franklin Bretschneider Dept of Biology Utrecht University brets...@xs4all.nl __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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 of ! operator
Daer Massimiliano Tripoli, Re: Dear all, I wrote a function : idrecfun - function(x) { idi - 1 esn - x[order(x)] if !(all(esn==x)) stop(x not ordered) esn - as.character(esn) for (i in 2:length(esn)) if(esn[i]==esn[i-1]) idi[i] - idi[i-1] else idi[i] - idi[i-1] + 1 idi } for whichever I send the code R give to me this error: idrecfun - function(x) + { + + idi - 1 + esn - x[order(x)] + if !(all(esn==x)) stop(x not ordered) Error: unexpected '!' in: esn - x[order(x)] if ! esn - as.character(esn) Error: object 'esn' not found for (i in 2:length(esn)) if(esn[i]==esn[i-1]) + idi[i] - idi[i-1] else idi[i] - idi[i-1] + 1 Error: object 'esn' not found idi Error: object 'idi' not found } Error: unexpected '}' in } What's wrong with my code? Anyone may help me, please? Massimiliano This is the output of sessionInfo(): R version 3.0.3 (2014-03-06) Platform: x86_64-w64-mingw32/x64 (64-bit) locale: [1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C [5] LC_TIME=English_United States.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base loaded via a namespace (and not attached): [1] tools_3.0.3 -- Massimiliano Tripoli Collaboratore T.E.R. scado il 31/12/2014 ISTAT - DCCN - Direzione Centrale della Contabilità Nazionale U.O. Contabilità dei flussi di materia del sistema economico - CSA/C Via Depretis, 74/B 00184 Roma Tel. 06.4673.3132 E-mail: mtrip...@istat.it Precedentemente: Collaboratore T.E.R. scado il 31/12/2014 Dipartimento DICS Dipartimento per i conti nazionali e le statistiche economiche Direzione DCCN Direzione Centrale della contabilità nazionale Servizio FIP Servizio Statistiche della Finanza Pubblica Ufficio FIP/E U.O. Conti trimestrali delle amministrazioni pubbliche e conti della sanità Via Depretis, 74/B 00184 Roma Tel. 06.4673.3132 E-mail: mtrip...@istat.it __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. This line is wrong: if !(all(esn==x)) stop(x not ordered) Try again with ! after ( (so if (!something)... And if there prove to be more problems, scrutinize your code (check, re-check and double-check). Best regards, Frank -- Franklin Bretschneider Dept of Biology Utrecht University brets...@xs4all.nl __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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 fix the warning message the condition has length 1 and only the first element will be used?
Hi jcrosbie , Re: I'm trying to create a function to return the date x months in the past. With the code below I'm getting the warning message: Warning message: In if (MonthsBack = CurrentMonth) { : the condition has length 1 and only the first element will be used Use ifelse(), that's for vectors. If is intended for single elements only. Best wishes, Franklin - Franklin Bretschneider Dept of Biology Utrecht University brets...@xs4all.nl __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] tip: large plots
Dear Sarah, RE: Hi all, I'm working with a bunch of large graphs, and stumbled across something useful. Probably many of you know this, but I didn't and so others might benefit. Using pch=. speeds up plotting considerably over using symbols. x - runif(100) y - runif(100) system.time(plot(x, y, pch=.)) user system elapsed 1.042 0.030 1.077 system.time(plot(x, y)) user system elapsed 37.865 0.033 38.122 If you have enough points, the result is also more legible. Choice of which pch symbol makes a difference too, the default pch=1 being the slowest of what I tried, but . is by far the speediest. system.time(plot(x, y, pch=0)) user system elapsed 11.191 0.011 11.270 system.time(plot(x, y, pch=1)) user system elapsed 38.024 0.008 38.245 system.time(plot(x, y, pch=2)) user system elapsed 14.140 0.027 14.270 system.time(plot(x, y, pch=3)) user system elapsed 15.696 0.011 15.799 system.time(plot(x, y, pch=4)) user system elapsed 18.770 0.007 18.888 This is a vanilla R session, 2.13.1 for x86_64-redhat-linux-gnu. I haven't tried it on any other OS, but it's making my life a lot smoother right now. Sarah With Mac OS10 (Snow Leopard, MacBookPro, R version 2.14.0), it 's a bit different: 2.14) source(/var/folders/t6/t6uCNQmsHsSw6tS6HuHp4U+++TI/-Tmp-//Rtmp4ZzIWp/file6b861d06) [1] plot symbol . (dot) user system elapsed 16.620 0.087 16.731 [1] plot symbol default (circle) user system elapsed 53.132 0.134 52.959 [1] symbol# 0 user system elapsed 18.828 0.090 18.803 [1] symbol# 1 user system elapsed 53.046 0.134 52.877 [1] symbol# 2 user system elapsed 22.000 0.154 22.401 [1] symbol# 3 user system elapsed 20.794 0.096 20.763 [1] symbol# 4 user system elapsed 23.534 0.098 23.542 It seems that even-numbered symbols are plotted faster than the odd ones By the way, I learn a lot about R programming by just following r-help! Best regards, Franklin Bretschneider -- Dept Biologie Kruytgebouw W711 Padualaan 8 3584 CH Utrecht The Netherlands -- f.bretschnei...@uu.nl __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.