Skip to main content
The 2024 Developer Survey results are live! See the results

R Language Collective

Questions

Browse questions with relevant R Language tags

514,067 questions

2464 votes
23 answers
469k views

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 ...
1500 votes
22 answers
1.4m views

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
977 votes
30 answers
2.1m views

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
1043 votes
25 answers
1.9m views

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
  • 39.8k
1547 votes
14 answers
1.8m views

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
1083 votes
20 answers
2.4m views

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
  • 12k
643 votes
27 answers
1.2m views

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
  • 39.8k
1154 votes
12 answers
441k views

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
  • 30.3k
504 votes
36 answers
409k views

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.9k
452 votes
35 answers
257k views

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.8k
547 votes
28 answers
140k views

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 is no longer here's user avatar
690 votes
17 answers
1.9m views

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
716 votes
19 answers
1.0m views

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
472 votes
21 answers
1.4m views

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.9k
912 votes
8 answers
1.6m views

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
604 votes
17 answers
1.2m views

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.5k
517 votes
19 answers
998k views

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.3k
490 votes
21 answers
1.6m views

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,903
487 votes
19 answers
1.9m views

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,395
606 votes
16 answers
454k views

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
  • 27.3k
708 votes
14 answers
1.1m views

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,931
957 votes
9 answers
378k views

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
  • 60.2k
695 votes
14 answers
445k views

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
575 votes
13 answers
690k views

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.5k
533 votes
14 answers
1.4m views

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
  • 63.2k
675 votes
12 answers
578k views

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
570 votes
12 answers
281k views

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
  • 6,035
423 votes
15 answers
392k views

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.7k
680 votes
11 answers
337k views

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.6k
311 votes
24 answers
433k views

How to change facet labels?

I have used the following ggplot command: ggplot(survey, aes(x = age)) + stat_bin(aes(n = nrow(h3), y = ..count.. / n), binwidth = 10) + scale_y_continuous(formatter = "percent", breaks = c(0, 0.1, ...
wishihadabettername's user avatar
450 votes
14 answers
694k views

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
303 votes
31 answers
157k views

Determine path of the executing script

I have a script called foo.R that includes another script other.R, which is in the same directory: #!/usr/bin/env Rscript message("Hello") source("other.R") But I want R to find that other.R no ...
Frank's user avatar
  • 65.6k
403 votes
18 answers
765k views

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
  • 11.1k
393 votes
16 answers
584k views

Order Bars in ggplot2 bar graph

I am trying to make a bar graph where the largest bar would be nearest to the y axis and the shortest bar would be furthest. So this is kind of like the Table I have Name Position 1 James ...
Julio Diaz's user avatar
  • 9,297
344 votes
18 answers
610k views

Split data frame string column into multiple columns

I'd like to take data of the form before = data.frame(attr = c(1,30,4,6), type=c('foo_and_bar','foo_and_bar_2')) attr type 1 1 foo_and_bar 2 30 foo_and_bar_2 3 4 foo_and_bar 4 ...
jkebinger's user avatar
  • 4,084
454 votes
13 answers
471k views

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.8k
367 votes
20 answers
697k views

How can I remove an element from a list?

I have a list and I want to remove a single element from it. How can I do this? I've tried looking up what I think the obvious names for this function would be in the reference manual and I haven't ...
David Locke's user avatar
444 votes
13 answers
691k views

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,263
350 votes
19 answers
572k views

ggplot with 2 y axes on each side and different scales

I need to plot a bar chart showing counts and a line chart showing rate all in one chart, I can do both of them separately, but when I put them together, I scale of the first layer (i.e. the geom_bar) ...
lokheart's user avatar
  • 24.4k
768 votes
7 answers
93k views

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,222
456 votes
12 answers
1.7m views

Extracting specific columns from a data frame

I have an R data frame with 6 columns, and I want to create a new data frame 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,640
302 votes
23 answers
239k views

Split a vector into chunks

I have to split a vector into n chunks of equal size in R. I couldn't find any base function to do that. Also Google didn't get me anywhere. Here is what I came up with so far; x <- 1:10 n <- 3 ...
Sebastian's user avatar
  • 3,797
914 votes
5 answers
167k views

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
  • 52.4k
478 votes
12 answers
939k views

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,455
638 votes
8 answers
960k views

Test if a vector contains a given element

How to check if a vector contains a given value?
medriscoll's user avatar
  • 27.3k
672 votes
8 answers
175k views

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

What is the difference between require() and library()?
Marco's user avatar
  • 9,564
660 votes
7 answers
1.1m views

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.5k
380 votes
16 answers
547k views

Extracting the last n characters from a string in R

How can I get the last n characters from a string in R? Is there a function like SQL's RIGHT?
Brani's user avatar
  • 6,714
525 votes
11 answers
419k views

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
  • 68.6k
383 votes
15 answers
494k views

How to reshape data from long to wide format

I'm having trouble rearranging the following data frame: set.seed(45) dat1 <- data.frame( name = rep(c("firstName", "secondName"), each=4), numbers = rep(1:4, 2), value = rnorm(8) )...
Steve's user avatar
  • 5,937


15 30 50 per page
1
2 3 4 5
10282