Questions tagged [r-faq]

The r-faq tag is created to group a limited number of questions discussing problems that come up regularly on the R tag. It is not the official FAQ on R for SO, but should serve as an interesting source of information on common problems.

The tag is created to group a limited number of questions discussing problems that come up regularly on the tag. It is not the official FAQ on R for SO, but should serve as an interesting source of information on common problems.

The "real" original R-FAQ is reliably found at: https://cran.r-project.org/doc/FAQ/R-FAQ.html and is typically the first google hit for "r-faq".

251 questions
632
votes
8 answers

Test if a vector contains a given element

How to check if a vector contains a given value?
medriscoll
  • 26,995
  • 17
  • 40
  • 36
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 its original levels, even when/if they do not…
medriscoll
  • 26,995
  • 17
  • 40
  • 36
596
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 to do so far is something like: df <-…
Jeff Allen
  • 17,277
  • 8
  • 49
  • 70
561
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 implementation that would slow things down. In my case, I am…
eytan
  • 5,945
  • 3
  • 20
  • 11
519
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( "http://stat.ethz.ch/R-manual/R-devel/library/base/html/connections.html", "http://en.wikipedia.org/wiki/Xz") y <- mapply(readLines, con=url) These two…
Dd Pp
  • 5,727
  • 4
  • 21
  • 19
502
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 groups from "Category". For example: Category …
boo-urns
  • 10,136
  • 26
  • 71
  • 107
492
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 is a standard library function that implements…
Nick
  • 21,555
  • 18
  • 47
  • 50
485
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 something similar but more complex. Here's an example…
JD Long
  • 59,675
  • 58
  • 202
  • 294
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 vector?
RQuestions
  • 4,843
  • 3
  • 16
  • 4
475
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,wt) data anim sex wt anim2 1 25499 1 0.8 2 2 25500 …
baz
  • 6,817
  • 11
  • 36
  • 37
469
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 string: tmp_new = "GAD,AB" Which function can do this…
Hans
  • 5,345
  • 4
  • 18
  • 10
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 only command I can figure out: …
Aren Cambre
  • 6,540
  • 9
  • 30
  • 36
443
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: airquality[airquality$Month == 8 & airquality$Temp >…
flodel
  • 87,577
  • 21
  • 185
  • 223
440
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 the same scale. x <- rnorm(100) eps <-…
Christopher DuBois
  • 42,350
  • 23
  • 71
  • 93
439
votes
34 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 elegant way to call installed.packages(), compare…
Maiasaura
  • 32,226
  • 27
  • 104
  • 108
1
2
3
16 17