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

Filter by
Sorted by
Tagged with
2467 votes
23 answers

How to make a great R reproducible example

When discussing performance with colleagues, teaching, sending a bug report or searching for guidance on mailing lists and here on Stack Overflow, a reproducible example is often asked and always ...
1522 votes
14 answers

How to join (merge) data frames (inner, outer, left, right)

Given two data frames: df1 = data.frame(CustomerId = c(1:6), Product = c(rep("Toaster", 3), rep("Radio", 3))) df2 = data.frame(CustomerId = c(2, 4, 6), State = c(rep("Alabama", 2), rep("Ohio", 1))) ...
Dan Goldstein's user avatar
1486 votes
19 answers

Sort (order) data frame rows by multiple columns

I want to sort a data frame by multiple columns. For example, with the data frame below I would like to sort by column 'z' (descending) then by column 'b' (ascending): dd <- data.frame(b = factor(c(...
Christopher DuBois's user avatar
1142 votes
12 answers

Grouping functions (tapply, by, aggregate) and the *apply family

Whenever I want to do something "map"py in R, I usually try to use a function in the apply family. However, I've never quite understood the differences between them -- how {sapply, lapply, etc.} ...
grautur's user avatar
  • 30k
1069 votes
20 answers

Remove rows with all or some NAs (missing values) in data.frame

I'd like to remove the lines in this data frame that: a) contain NAs across all columns. Below is my example data frame. gene hsap mmul mmus rnor cfam 1 ENSG00000208234 0 NA NA ...
Benoit B.'s user avatar
  • 11.9k
1031 votes
25 answers

Drop data frame columns by name

I have a number of columns that I would like to remove from a data frame. I know that we can delete them individually using something like: df$x <- NULL But I was hoping to do this with fewer ...
Btibert3's user avatar
  • 39k
955 votes
30 answers

How do I replace NA values with zeros in an R dataframe?

I have a data frame and some columns have NA values. How do I replace these NA values with zeroes?
Renato Dinhani's user avatar
939 votes
9 answers

What are the differences between "=" and "<-" assignment operators?

What are the differences between the assignment operators = and <- in R? I know that operators are slightly different, as this example shows x <- y <- 5 x = y = 5 x = y <- 5 x <- y = ...
csgillespie's user avatar
  • 59.3k
906 votes
5 answers

data.table vs dplyr: can one do something well the other can't or does poorly?

Overview I'm relatively familiar with data.table, not so much with dplyr. I've read through some dplyr vignettes and examples that have popped up on SO, and so far my conclusions are that: data....
BrodieG's user avatar
  • 51.8k
887 votes
9 answers

Rotating and spacing axis labels in ggplot2

I have a plot where the x-axis is a factor whose labels are long. While probably not an ideal visualization, for now I'd like to simply rotate these labels to be vertical. I've figured this part out ...
Christopher DuBois's user avatar
762 votes
7 answers

How can we make xkcd style graphs?

Apparently, folk have figured out how to make xkcd style graphs in Mathematica and in LaTeX. Can we do it in R? Ggplot2-ers? A geom_xkcd and/or theme_xkcd? I guess in base graphics, par(xkcd=TRUE)? ...
jebyrnes's user avatar
  • 9,102
704 votes
14 answers

How to convert a factor to integer\numeric without loss of information?

When I convert a factor to a numeric or integer, I get the underlying level codes, not the values as numbers. f <- factor(sample(runif(5), 20, replace = TRUE)) ## [1] 0.0248644019011408 0....
Adam SO's user avatar
  • 9,851
696 votes
18 answers

How should I deal with "package 'xxx' is not available (for R version x.y.z)" warning?

I tried to install a package, using install.packages("foobarbaz") but received the warning Warning message: package 'foobarbaz' is not available (for R version x.y.z) Why doesn't R think that the ...
Richie Cotton's user avatar
683 votes
17 answers

Plot two graphs in a same plot

I would like to plot y1 and y2 in the same plot. x <- seq(-2, 2, 0.05) y1 <- pnorm(x) y2 <- pnorm(x, 1, 1) plot(x, y1, type = "l", col = "red") plot(x, y2, type = "l", col = "green") But ...
Sandra Schlichting's user avatar
682 votes
13 answers

How can I view the source code for a function?

I want to look at the source code for a function to see how it works. I know I can print a function by typing its name at the prompt: > t function (x) UseMethod("t") <bytecode: 0x2332948> &...
Joshua Ulrich's user avatar
666 votes
11 answers

The difference between bracket [ ] and double bracket [[ ]] for accessing the elements of a list or dataframe

R provides two different methods for accessing the elements of a list or data.frame: [] and [[]]. What is the difference between the two, and when should I use one over the other?
Sharpie's user avatar
  • 17.3k
661 votes
12 answers

How to unload a package without restarting R

I'd like to unload a package without having to restart R (mostly because restarting R as I try out different, conflicting packages is getting frustrating, but conceivably this could be used in a ...
Ari B. Friedman's user avatar
660 votes
8 answers

What is the difference between require() and library()?

What is the difference between require() and library()?
Marco's user avatar
  • 9,374
643 votes
7 answers

Run R script from command line

I have a file, called a.r, it has a chmod of 755, sayHello <- function(){ print('hello') } sayHello() How can I run this via command-line?
Sait's user avatar
  • 19.1k
637 votes
26 answers

Convert a list to a data frame

I have a nested list of data. Its length is 132 and each item is a list of length 20. Is there a quick way to convert this structure into a data frame that has 132 rows and 20 columns of data? Here is ...
Btibert3's user avatar
  • 39k
632 votes
8 answers

Test if a vector contains a given element

How to check if a vector contains a given value?
medriscoll's user avatar
601 votes
16 answers

Drop unused factor levels in a subsetted data frame

I have a data frame containing a factor. When I create a subset of this dataframe using subset or another indexing function, a new data frame is created. However, the factor variable retains all of ...
medriscoll's user avatar
598 votes
17 answers

Create an empty data.frame

I'm trying to initialize a data.frame without any rows. Basically, I want to specify the data types for each column and name them, but not have any rows created as a result. The best I've been able ...
Jeff Allen's user avatar
  • 17.3k
563 votes
12 answers

Quickly reading very large tables as dataframes

I have very large tables (30 million rows) that I would like to load as a dataframes in R. read.table() has a lot of convenient features, but it seems like there is a lot of logic in the ...
eytan's user avatar
  • 5,965
559 votes
13 answers

How to find out which package version is loaded in R?

I am in a process of figuring out how to use my university cluster. It has 2 versions of R installed. System wide R 2.11 (Debian 6.0) and R 2.14.2 in non-standard location. I am trying to use MPI ...
mpiktas's user avatar
  • 11.3k
542 votes
28 answers

Tricks to manage the available memory in an R session

What tricks do people use to manage the available memory of an interactive R session? I use the functions below [based on postings by Petr Pikal and David Hinds to the r-help list in 2004] to list (...
Dirk Eddelbuettel's user avatar
521 votes
6 answers

How to write trycatch in R

I want to write trycatch code to deal with error in downloading from the web. url <- c( "", "
Dd Pp's user avatar
  • 5,747
508 votes
10 answers

Folder management with r : Check existence of directory and create it if it doesn't exist

I often find myself writing R scripts that generate a lot of output. I find it cleaner to put this output into its own directory(s). What I've written below will check for the existence of a directory ...
Chase's user avatar
  • 67.8k
503 votes
18 answers

How to sum a variable by group

I have a data frame with two columns. First column contains categories such as "First", "Second", "Third", and the second column has numbers that represent the number of times I saw the specific ...
boo-urns's user avatar
  • 10.1k
501 votes
14 answers

How to change legend title in ggplot

I have the following plot like below. It was created with this command: library(ggplot2) df <- data.frame(cond = factor(rep(c("A", "B"), each = 200)), rating =...
neversaint's user avatar
  • 61.1k
493 votes
35 answers

How to find the statistical mode?

In R, mean() and median() are standard functions which do what you'd expect. mode() tells you the internal storage mode of the object, not the value that occurs the most in its argument. But is there ...
Nick's user avatar
  • 21.6k
487 votes
10 answers

Combine a list of data frames into one data frame by row

I have code that at one place ends up with a list of data frames which I really want to convert to a single big data frame. I got some pointers from an earlier question which was trying to do ...
JD Long's user avatar
  • 59.8k
484 votes
20 answers

Counting the number of elements with the values of x in a vector

I have a vector of numbers: numbers <- c(4,23,4,23,5,43,54,56,657,67,67,435, 453,435,324,34,456,56,567,65,34,435) How can I have R count the number of times a value x appears in the ...
RQuestions's user avatar
  • 4,843
480 votes
7 answers

How do I install an R package from source?

A friend sent me along this great tutorial on webscraping The New York Times with R. I would really love to try it. However, the first step is to install a package called [RJSONIO][2] from source. I ...
user avatar
479 votes
19 answers

Changing column names of a data frame

I have a data frame called "newprice" (see below) and I want to change the column names in my program in R. > newprice Chang. Chang. Chang. 1 100 36 136 2 120 -33 ...
Son's user avatar
  • 5,315
476 votes
8 answers

How to add leading zeros?

I have a set of data which looks something like this: anim <- c(25499,25500,25501,25502,25503,25504) sex <- c(1,2,2,1,2,1) wt <- c(0.8,1.2,1.0,2.0,1.8,1.4) data <- data.frame(anim,sex,...
baz's user avatar
  • 6,827
470 votes
12 answers

How can two strings be concatenated?

How can I concatenate (merge, combine) two values? For example I have: tmp = cbind("GAD", "AB") tmp # [,1] [,2] # [1,] "GAD" "AB" My goal is to concatenate the two values in "tmp" to one ...
Hans's user avatar
  • 5,355
462 votes
8 answers

Cluster analysis in R: determine the optimal number of clusters

How can I choose the best number of clusters to do a k-means analysis. After plotting a subset of below data, how many clusters will be appropriate? How can I perform cluster dendro analysis? n = 1000 ...
user2153893's user avatar
  • 4,657
461 votes
21 answers

How to rename a single column in a data.frame?

I know if I have a data frame with more than 1 column, then I can use colnames(x) <- c("col1","col2") to rename the columns. How to do this if it's just one column? Meaning a ...
screechOwl's user avatar
  • 27.3k
445 votes
10 answers

Extracting specific columns from a data frame

I have an R data frame with 6 columns, and I want to create a new dataframe that only has three of the columns. Assuming my data frame is df, and I want to extract columns A, B, and E, this is the ...
Aren Cambre's user avatar
  • 6,540
445 votes
2 answers

Why is `[` better than `subset`?

When I need to filter a data.frame, i.e., extract rows that meet certain conditions, I prefer to use the subset function: subset(airquality, Month == 8 & Temp > 90) Rather than the [ function: ...
flodel's user avatar
  • 87.7k
443 votes
14 answers

Side-by-side plots with ggplot2

I would like to place two plots side by side using the ggplot2 package, i.e. do the equivalent of par(mfrow=c(1,2)). For example, I would like to have the following two plots show side-by-side with ...
Christopher DuBois's user avatar
443 votes
13 answers

Write lines of text to a file in R

In the R scripting language, how do I write lines of text, e.g., the following two lines Hello World to a file named "output.txt"?
amarillion's user avatar
  • 24.5k
441 votes
35 answers

Elegant way to check for missing packages and install them?

I seem to be sharing a lot of code with coauthors these days. Many of them are novice/intermediate R users and don't realize that they have to install packages they don't already have. Is there an ...
Maiasaura's user avatar
  • 32.3k
433 votes
13 answers

Sample random rows in dataframe

I am struggling to find the appropriate function that would return a specified number of rows picked up randomly without replacement from a data frame in R language? Can anyone help me out?
nikhil's user avatar
  • 9,053
432 votes
2 answers

How to set limits for axes in ggplot2 R plots?

I plot the following: library(ggplot2) carrots <- data.frame(length = rnorm(500000, 10000, 10000)) cukes <- data.frame(length = rnorm(50000, 10000, 20000)) carrots$veg <- 'carrot' cukes$...
David B's user avatar
  • 29.3k
425 votes
4 answers

Is there an R function for finding the index of an element in a vector?

In R, I have an element x and a vector v. I want to find the first index of an element in v that is equal to x. I know that one way to do this is: which(x == v)[[1]], but that seems excessively ...
Ryan C. Thompson's user avatar
414 votes
15 answers

How can I trim leading and trailing white space?

I am having some trouble with leading and trailing white space in a data.frame. For example, I look at a specific row in a data.frame based on a certain condition: > myDummy[myDummy$country == c(&...
mropa's user avatar
  • 11.6k
407 votes
6 answers

How to find the length of a string in R

How to find the length of a string (i.e., number of characters in a string) without splitting it in R? I know how to find the length of a list but not of a string. And what about Unicode strings? How ...
Igor Chubin's user avatar
  • 61.9k
398 votes
18 answers

Convert data.frame columns from factors to characters

I have a data frame. Let's call him bob: > head(bob) phenotype exclusion GSM399350 3- 4- 8- 25- 44+ 11b- 11c- 19- NK1.1- Gr1- TER119- GSM399351 3- 4- 8- 25-...
Mike Dewar's user avatar
  • 10.9k

2 3 4 5