r

[R] running R in batch-mode - print to screen?

When running R CMD BATCH [options] filename.r I want to control where the output is printed. I can suppress the creation of the .Rout file with R CMD BATCH [options] filename.r /dev/null but is it possible to direct the output to the screen? Like when I run it by R [options] < filename.r ? ...

Given a vector a=[1,2, 3.2, 4, 5] and an element x=3 In vector a, how to find the exact entry which is bigger than x?

Given a vector a=[1,2, 3.2, 4, 5] and an element x=3 In vector a, how to find the exact entry which is bigger than x? In R, is there any function to do that? ...

vector binding in R

I would like to implement a simulation program, which requires the following structure: It has a for loop, the program will generate an vector in each iteration. I need each generated vector is appended to the existing vector. I do not how how to do this in R. Thanks for the help. ...

Saving plot to tiff, with high resolution for publication (in R)

A Journal we are sending an article to is asking for the following: To ensure the best reproduction quality of your figures we would appreciate high resolution files. All figures should preferably be in TIFF or EPS format... and should have the following resolution: Graph: 800 - 1200 DPI Photo: 400 - 800 DPI Color (only CMY...

R: Count number of entries in a row based on external criteria

Dear R-wizards, I have the following data frame: Date1 Date2 Date3 Date4 Date5 1 25 April 2005 10 May 2006 28 March 2007 14 November 2007 1 April 2008 2 25 April 2005 10 May 2006 28 March 2007 14 November 2007 1 April 2008 3 29 January 2008...

How to transform a dataframe of characters to the respective dates?

Dear all, I noticed already a couple of times that working with dates doesn't allow for using the usual tricks in R. Say I have a dataframe Data with Dates (see below), and I want to convert the complete dataframe to a date class. The only solution I could come up with until now is : for (i in 1:ncol(Data)){ Data[,i] <- as.Date(Dat...

Is it possible to write a table to a file in JSON format in R?

I'm making word frequency tables with R and the preferred output format would be a JSON file. sth like { "word" : "dog", "frequency" : 12 } Is there any way to save the table directly into this format? I've been using the write.csv() function and convert the output into JSON but this is very complicated and time consumin...

Print long strings of text LaTeX / Sweave

At the end of a survey I've conducted, we give respondents an open ended box to tell us anything we didn't cover in the survey.These comments will often span several pages. I am familiar with the longtable package for LaTeX and here is the solution I have mocked up: <<results = tex>>= cat("\\begin{longtable}{p{14cm}}\n") cat("\\hline\n"...

When does it pay off to use S4 methods in R programming

Hi all, I program regularly in R in a professional context, and I write packages for clients or co-workers as well. Some of the programmers here have a Java background and insist on doing everything the object-oriented way, using S4 methods. My experience on the other hand is that S4 implementations often perform worse and cause a lot m...

MySQL stored procedure fails when called from R

This procedure works from the MySQL commandline both remotely and on localhost and it works when called from PHP. In all cases the grants are adequate: CREATE PROCEDURE `myDB`.`lee_expout` (IN e int, IN g int) BEGIN select lm.groupname, lee.location, starttime, dark, inadist,smldist,lardist,emptydur,inadur,smldur,lardur,emptyct,entct...

How to define next line in a list for label.

Hello all, I've been put in charge of automating the graph production at work and need a specific way of displaying the scale on our bar plots. currently what we have is i.imgur.com/bWjk9.png and they would like the value of each of the scales listed below. When i change the scale it comes out like this. i.imgur.com/1D9up.png An...

how to pick up a set of numbers from the end of lines with irregular length in R?

I need to pick up some numbers from lines with irregular length, like this: AAAAAAAAA 250.00 BBB 240.00 CCCCCCC 13.00 I need to capture 250.00, 240.00 and 13.00, but since both the numeric and character strings are irregular, I can't use "substr" for that, I think regex maybe the solution, but I dunno much about it. Can anyone help? ...

how to convert numbers with comma inside from character to numeric in R?

I have some numbers with comma inside, like this: 1,200.00 R cannot convert this into numeric using as.numeric, I guess it is due to the comma, how can I fix it? Thanks! ...

how to set x-axis limits in ggplot2 R plots?

Say I plot the following in R: library(ggplot2) carrots <- data.frame(length = rnorm(500000, 10000, 10000)) cukes <- data.frame(length = rnorm(50000, 10000, 20000)) carrots$veg <- 'carrot' cukes$veg <- 'cuke' vegLengths <- rbind(carrots, cukes) ggplot(vegLengths, aes(length, fill = veg)) + geom_density(alpha = 0.2) Now say I only ...

How to make ggplot2 plots prettier?

I have generated the following plot using the R code that follows it: ggplot(lengths, aes(length, fill = library)) + geom_density(alpha = 0.2) + coord_cartesian(xlim = c(0, 60000)) Now I would like to make the plot a bit prettier: Make the x-axis show length every 5000 units (instead of every 20000) Add x-values on top of the thre...

One hour increment in R, zoo

Hello How can I add one hour to all the elements of the index of a zoo series? I've tried newseries <- myzooseries index(newseries) <- index(myzooseries)+times("1:00:00") but I get the message Incompatible methods ("Ops.dates", "Ops.times") for "+" thanks My index is a chron object with date and time but I've tried w...

Highlighting regions of interest in ggplot2

In vanilla plotting, it is possible to use a polygon call in the panel.first argument to plot to highlight a background region. Is it possible to do the same in ggplot2? Can it be done while preserving the gridlines? eg: # plot hp and wt for mtcars data, highlighting region where hp/wt ratio < 35 with(mtcars,plot(hp,wt, panel.firs...

Calculating Ages in R

Hello everyone. I have two data frames in R. One frame has a persons year of birth: YEAR /1931 /1924 and then another column shows a more recent time. RECENT 09/08/2005 11/08/2005 What I want to do is subtract the years so that I can calculate their age in number of years, however I am not sure how to approach this. Any help please...

R workspaces i.e. .R files

How do I start a new .R file default in a new session for new objects in that session? ...

Extracting lapply or mclapply results

I am currently running numerous apply lines that look like this: test=data.frame(t=seq(1,5,1),e=seq(6,10,1)) mean(apply(test,2,mean)) I want to convert the second line to mclapply which produces the same result as lapply. I realize that I could extract each item from the lapply statement using a for loop then use mean on that vector b...