Skip to main content

R Language Collective

Questions

Browse questions with relevant R Language tags

516,267 questions

2464 votes
23 answers
483k 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 ...
1511 votes
21 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
988 votes
30 answers
2.2m 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
1050 votes
25 answers
2.0m 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
  • 40.3k
1570 votes
14 answers
1.9m 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&...
Dan Goldstein's user avatar
1089 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
  • 12.1k
649 votes
27 answers
1.3m 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
  • 40.3k
1157 votes
12 answers
443k 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.5k
517 votes
37 answers
429k 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
  • 22.2k
457 votes
35 answers
270k 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
  • 33k
550 votes
28 answers
143k 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
692 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
721 votes
19 answers
1.1m 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
480 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
  • 28.2k
921 votes
8 answers
1.7m 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
607 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.6k
521 votes
19 answers
1.0m 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.4k
493 votes
21 answers
1.7m 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,933
493 votes
19 answers
2.0m 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,455
610 votes
16 answers
457k 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.5k
716 votes
14 answers
1.2m 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
  • 10k
969 votes
9 answers
391k 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.6k
698 votes
14 answers
456k 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
543 votes
14 answers
1.5m 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
  • 64.2k
578 votes
13 answers
729k 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
685 votes
12 answers
600k 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
573 votes
12 answers
285k 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,065
689 votes
11 answers
345k 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.7k
425 votes
15 answers
407k 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.9k
315 votes
24 answers
457k 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
456 votes
14 answers
719k 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
305 votes
31 answers
159k 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
  • 66.4k
405 votes
18 answers
772k 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
372 votes
21 answers
720k 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
  • 18.1k
403 votes
16 answers
600k 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,477
454 votes
14 answers
713k 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,403
351 votes
18 answers
626k 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,164
461 votes
13 answers
485k 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
360 votes
19 answers
596k 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.8k
463 votes
12 answers
1.8m 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,740
772 votes
7 answers
95k 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,352
306 votes
23 answers
244k 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,847
483 votes
12 answers
981k 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 string:...
Hans's user avatar
  • 5,535
917 votes
5 answers
170k 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.7k
674 votes
7 answers
1.2m 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.9k
641 votes
8 answers
973k views

Test if a vector contains a given element

How to check if a vector contains a given value?
medriscoll's user avatar
  • 27.5k
675 votes
8 answers
183k views

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

What is the difference between require() and library()?
Marco's user avatar
  • 9,634
383 votes
16 answers
567k 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,794
533 votes
11 answers
442k 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
  • 69.3k
366 votes
16 answers
319k views

Measuring function execution time in R

Is there a standardized way in R of measuring execution time of function? Obviously I can take system.time before and after execution and then take the difference of those, but I would like to know ...
dns's user avatar
  • 3,661


15 30 50 per page
1
2 3 4 5
10326