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
55
votes
8 answers

Figure position in markdown when converting to PDF with knitr and pandoc

I'm trying to control the position of a plot when converting to PDF using knitr and pandoc. My .Rmd file looks this: # My report Some text some text some text some text some text some text some text some text some text ```{r myplot, echo=FALSE,…
luciano
  • 13,158
  • 36
  • 90
  • 130
55
votes
6 answers

How do you order the fill-colours within ggplot2 geom_bar

I am calling the ggplot function ggplot(data,aes(x,y,fill=category)+geom_bar(stat="identity") The result is a barplot with bars filled by various colours corresponding to category. However the ordering of the colours is not consistent from bar to…
Dave31415
  • 2,846
  • 4
  • 26
  • 34
55
votes
4 answers

Subset rows in a data frame based on a vector of values

I have two data sets that are supposed to be the same size but aren't. I need to trim the values from A that are not in B and vice versa in order to eliminate noise from a graph that's going into a report. (Don't worry, this data isn't being…
Zelbinian
  • 3,221
  • 5
  • 20
  • 23
55
votes
9 answers

Insert elements into a vector at given indexes

I have a logical vector, for which I wish to insert new elements at particular indexes. I've come up with a clumsy solution below, but is there a neater way? probes <- rep(TRUE, 15) ind <- c(5, 10) probes.2 <-…
Aaron Statham
  • 2,048
  • 1
  • 15
  • 16
55
votes
6 answers

Listing R Package Dependencies Without Installing Packages

Is there a simple way to get a list of R package dependencies (all recursive dependencies) for a given package, without installing the package and it's dependencies? Something similar to a fake install in portupgrade or apt.
Jonathan Lisic
  • 1,710
  • 1
  • 13
  • 21
55
votes
4 answers

Merging multiple data.tables

I am aware that one can merge (join) two data.table with the merge function or the [.data.table function. However, if I have say 10, data.tables and want to use do.call to merge them all together, is there a function that would do this? Currently I…
Alex
  • 19,533
  • 37
  • 126
  • 195
55
votes
2 answers

How to add different lines for facets

I have data where I look at the difference in growth between a monoculture and a mixed culture for two different species. Additionally, I made a graph to make my data clear. I want a barplot with error bars, the whole dataset is of course bigger,…
Marinka
  • 1,189
  • 2
  • 11
  • 24
55
votes
4 answers

Use superscripts in R axis labels

Using base graphics in R, how can I add superscripts to axis labels, as one might want to when plotting latitude and longitude axes on a map. Consider this example: plot(-100:-50, 50:100, type="n", xlab="", ylab="", axes=FALSE) axis(1, seq(-100,…
digitalmaps
  • 2,885
  • 3
  • 22
  • 28
54
votes
3 answers

Grouping 2 levels of a factor in R

I have a column of data that is a factor with levels A, B and C, I am interested in combining two of these levels into one factor, so it would become A and B, with B = B and C, or maybe a new variable A and D, with D = B and C. I can come up with…
asjohnson
  • 1,057
  • 4
  • 17
  • 25
54
votes
4 answers

Plot multiple columns on the same graph in R

I have the following data frame: A B C D Xax 0.451 0.333 0.034 0.173 0.22 0.491 0.270 0.033 0.207 0.34 0.389 0.249 0.084 0.271 0.54 0.425 0.819 0.077 0.281 0.34 0.457 0.429 …
ifreak
  • 1,726
  • 4
  • 27
  • 45
54
votes
7 answers

How to flatten a list to a list without coercion?

I am trying to achieve the functionality similar to unlist, with the exception that types are not coerced to a vector, but the list with preserved types is returned instead. For instance: flatten(list(NA, list("TRUE", list(FALSE), 0L)) should…
eold
  • 5,972
  • 11
  • 56
  • 75
54
votes
7 answers

Find duplicated rows (based on 2 columns) in Data Frame in R

I have a data frame in R which looks like: | RIC | Date | Open | |--------|---------------------|--------| | S1A.PA | 2011-06-30 20:00:00 | 23.7 | | ABC.PA | 2011-07-03 20:00:00 | 24.31 | | EFG.PA | 2011-07-04 20:00:00 |…
user802231
  • 853
  • 2
  • 8
  • 10
54
votes
6 answers

Find the index position of the first non-NA value in an R vector?

I have a problem where a vector has a bunch of NAs at the beginning, and data thereafter. However the peculiarity of my data is that the first n values that are non NA, are probably unreliable, so I would like to remove them and replace them with…
Thomas Browne
  • 23,824
  • 32
  • 78
  • 121
54
votes
7 answers

State name to abbreviation

I have a large file with a variable state that has full state names. I would like to replace it with the state abbreviations (that is "NY" for "New York"). Is there an easy way to do this (apart from using several if-else commands)? May be using…
user227290
  • 1,509
  • 3
  • 18
  • 26
54
votes
5 answers

Looping over variables in ggplot

I want to use ggplot to loop over several columns to create multiple plots, but using the placeholder in the for loop changes the behavior of ggplot. If I have this: t <- data.frame(w = c(1, 2, 3, 4), x = c(23,45,23, 34), y = c(23,34,54, 23), z =…
Tom
  • 4,860
  • 7
  • 43
  • 55
1 2 3
99
100