Questions tagged [r]

R is a free, open-source programming language & software environment for statistical computing, bioinformatics, visualization & general computing. Please use minimal reproducible examples others can run using copy & paste. Show desired output entirely. Use dput() for data & specify all non-base packages with library(). Don't embed pictures for data or code, use indented code blocks instead. For statistics questions, use https://stats.stackexchange.com.

R Programming Language

R is a free, open-source programming language and software environment for statistical computing, bioinformatics, information graphics, and general computing. It is a multi-paradigm language and dynamically typed. R is an implementation of the S programming language combined with lexical scoping semantics inspired by Scheme. R was created by Ross Ihaka and Robert Gentleman and is now developed by the R Development Core Team. The R environment is easily extended through a packaging system on CRAN, the Comprehensive R Archive Network.

Scope of questions

This tag should be used for programming-related questions about R. Including a minimal reproducible example in your question will increase your chances of getting a timely, useful answer. Questions should not use the tag unless they relate specifically to the RStudio interface and not just the R language.

If your question is more focused on statistics or data science, use Cross Validated or Data Science, respectively. Bioinformatics-specific questions may be better received on Bioconductor Support or Biostars. General questions about R (such as requests for off-site resources or discussion questions) are unsuitable for Stack Overflow and may be appropriate for one of the general, or special-interest, R mailing lists.

Please do not cross-post across multiple venues. Do research (read tag wikis, look at existing questions, or search online) to determine the most appropriate venue so that you have a better chance of receiving solutions to your question. Your question may be automatically migrated to a more appropriate Stack Exchange site. If you receive no response to your questions after a few days, or if your question is put on hold for being off-topic, it is then OK to post to another venue, giving a link to your Stack Overflow question - but don't cross-post just because your question is down-voted or put on hold for being unclear. Instead, work on improving your question.

Stack Overflow resources

Official CRAN Documentation

Other CRAN resources

Free Resources

Interactive R learning

  • Coursera - Learn how to use R for effective data analysis
  • DataCamp - Many interactive R and data science courses
  • Dataquest - Interactive R courses for data science
  • edX - Basic Statistics and R (basic course, not just for life sciences)
  • edX - Introduction to R Programming
  • R-exercises - 1000+ R exercises and solutions
  • RPubs - Easy web publishing from R
  • Swirl - R-package to learn R interactively

Free books on R:

Programming Chrestomathy (problems written in many languages)

Other free resource materials

IDEs and editors for R

Web application framework for R

  • Shiny - Turn your analyses into interactive web applications. No HTML, CSS, or JavaScript knowledge required.
  • FastRWeb - Fast Interactive Web Framework for Data Mining Using R

Graphical User Interfaces (GUI) in R

Code style guides

Other Resources

Recommended additional R resources include:

Alternative R engines

All alternative R engines have the goal of increasing R's performance and memory management.

Downstream distributions with complete compatibility

Forks of R with near 100% code compatibility

  • pqR by Radford Neal (C-based).
  • Rho by Karl Millar, based upon CXXR by Andrew Runnalls (C++-based). The development on Rho has been suspended indefinitely.

Rewrites with high code compatibility

  • Renjin by BeDataDriven (Java-based).
  • TERR by Tibco (C++-based).

Experimental and early-stage rewrites

  • Riposte by Justin Talbot (C++-based).
  • FastR by Jan Vitek and Tomas Kalibera (Java-based).

Unrelated tags

Due to R's simple name, questions sometimes get tagged with the tag when a different topic is meant. Here is a list of tags that mistagged R questions might be re-tagged to

  • for questions related to the file R.java on
  • "A command line tool for running JavaScript scripts that use the Asynchronous Module Definition API (AMD) for declaring and using JavaScript modules and regular JavaScript script files. It is part of the RequireJS project, and works with the RequireJS implementation of AMD." (from the wiki summary)
  • for questions related to RStudio use the rstudio tag. Don't use this tag just because you are working with RStudio.
496613 questions
57
votes
4 answers

R Markdown - variable output name

With one R markdown file, I would like to create different possible output pdf documents, where the output file name should be defined within the document. Is there any way to convince markdown to manipulate the output filename in such a way?…
Sosel
  • 1,678
  • 1
  • 16
  • 31
57
votes
3 answers

dplyr: lead() and lag() wrong when used with group_by()

I want to find the lead() and lag() element in each group, but had some wrong results. For example, data is like this: library(dplyr) df = data.frame(name=rep(c('Al','Jen'),3), score=rep(c(100, 80, 60),2)) df Data: name score 1 …
YJZ
  • 3,934
  • 11
  • 43
  • 67
57
votes
5 answers

Install a local R package with dependencies from CRAN mirror

I have built an R package, i.e. I have the mypackage.tar.gz file. This package depends on several other packages, all downloadable and installable from any CRAN mirror. Now I want to install this package on a system where the dependencies are not…
WoDoSc
  • 2,598
  • 1
  • 13
  • 26
57
votes
5 answers

How to add whitespace to an RMarkdown document?

Is there a way to do it by adding latex code in the text or does the solution lie in (R)Markdown? No sign of a solution here: http://rmarkdown.rstudio.com/pdf_document_format.html At present I'm bodging a solution by adding my monospace signature to…
RobinLovelace
  • 4,799
  • 6
  • 29
  • 40
57
votes
4 answers

How do I open a script file in RStudio using an R command?

I have a simple script file called test.R. It's saved in the working directory. When I go to File > Open > test.R, it opens the file in the editor (without executing the code, which is what I want). How do I do this by typing a command in the…
user3654852
  • 753
  • 1
  • 5
  • 10
57
votes
3 answers

How can I take pairwise parallel maximum or minimum between two vectors?

Suppose I have two vectors in R, defined as follows. a = c(3,3,5) b = c(2,4,6) Is there a function that will give me the pairwise maximum between the elements of a and the elements of b, which can be run inside a formula? I tried to do, max(a, b)…
merlin2011
  • 71,677
  • 44
  • 195
  • 329
57
votes
2 answers

Grouped bar plot in ggplot

I have a survey file in which row are observation and column question. Here are some fake data they look like: People,Food,Music,People P1,Very Bad,Bad,Good P2,Good,Good,Very Bad P3,Good,Bad,Good P4,Good,Very Bad,Very Good P5,Bad,Good,Very…
S12000
  • 3,345
  • 12
  • 35
  • 51
57
votes
6 answers

Put stars on ggplot barplots and boxplots - to indicate the level of significance (p-value)

It's common to put stars on barplots or boxplots to show the level of significance (p-value) of one or between two groups, below are several examples: The number of stars are defined by p-value, for example one can put 3 stars for p-value < 0.001,…
Ali
  • 9,440
  • 12
  • 62
  • 92
57
votes
4 answers

Cut() error - 'breaks' are not unique

I have following dataframe: a ID a.1 b.1 a.2 b.2 1 1 40.00 100.00 NA 88.89 2 2 100.00 100.00 100 100.00 3 3 50.00 100.00 75 100.00 4 4 66.67 59.38 NA 59.38 5 5 37.50 100.00 NA …
DSSS
  • 1,923
  • 4
  • 16
  • 15
57
votes
7 answers

as.numeric with comma decimal separators?

I have a large vector of strings of the form: Input = c("1,223", "12,232", "23,0") etc. That's to say, decimals separated by commas, instead of periods. I want to convert this vector into a numeric vector. Unfortunately, as.numeric(Input) just…
Fhnuzoag
  • 3,810
  • 2
  • 20
  • 16
57
votes
3 answers

Why TRUE == "TRUE" is TRUE in R?

Why TRUE == "TRUE" is TRUE in R? Is there any equivalent for === in R? Update: These are all returning FALSE: TRUE == "True" TRUE == "true" TRUE == "T" The only TRUE value is TRUE == "TRUE". In case of checking with identical() everything works…
Mahdi
  • 9,247
  • 9
  • 53
  • 74
57
votes
5 answers

How does one change the levels of a factor column in a data.table

What is the correct way to change the levels of a factor column in a data.table (note: not data frame) library(data.table) mydt <- data.table(id=1:6, value=as.factor(c("A", "A", "B", "B", "B", "C")), key="id") mydt[, levels(value)] [1]…
Ricardo Saporta
  • 54,400
  • 17
  • 144
  • 178
57
votes
2 answers

Create heatmap with values from matrix in ggplot2

I've seen heatmaps with values made in various R graphics systems including lattice and base like this: I tend to use ggplot2 a bit and would like to be able to make a heatmap with the corresponding cell values plotted. Here's the heat map and an…
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
57
votes
3 answers

How to print R graphics to multiple pages of a PDF and multiple PDFs?

I know that pdf("myOut.pdf") will print to a PDF in R. What if I want to Make a loop that prints subsequent graphs on new pages of a PDF file (appending to the end)? Make a loop that prints subsequent graphs to new PDF files (one graph per…
Dan Goldstein
  • 24,229
  • 18
  • 37
  • 41
57
votes
3 answers

Put whisker ends on boxplot

I would like to put perpendicular lines at the ends of the whiskers like the boxplot function automatically gives.
user1762299
  • 579
  • 1
  • 4
  • 3