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
30
votes
9 answers

Transpose / reshape dataframe without "timevar" from long to wide format

I have a data frame that follows the below long Pattern: Name MedName Name1 atenolol 25mg Name1 aspirin 81mg Name1 sildenafil 100mg Name2 atenolol 50mg Name2 enalapril 20mg And would like to get below (I do not…
Hotamd6
  • 315
  • 3
  • 8
28
votes
4 answers

Index values from a matrix using row, col indices

I have a 2D matrix mat with 500 rows × 335 columns, and a data.frame dat with 120425 rows. The data.frame dat has two columns I and J, which are integers to index the row, column from mat. I would like to add the values from mat to the rows of…
Mike T
  • 41,085
  • 18
  • 152
  • 203
28
votes
3 answers

How to measure similarity between strings?

I have a bunch of names, and I want to obtain the unique names. However, due to spelling errors and inconsistencies in the data the names might be written down wrong. I am looking for a way to check in a vector of strings if two of them are…
Sacha Epskamp
  • 46,463
  • 20
  • 113
  • 131
28
votes
7 answers

R: How to split a data frame into training, validation, and test sets?

I'm using R to do machine learning. Following standard machine learning methodology, I would like to randomly split my data into training, validation, and test data sets. How do I do that in R? I know there are some related questions on how to split…
stackoverflowuser2010
  • 38,621
  • 48
  • 169
  • 217
28
votes
2 answers

Convert continuous numeric values to discrete categories defined by intervals

I have a data frame with a continuous numeric variable, age in months (age_mnths). I want to make a new discrete variable, with age categories based on age intervals. # Some example data rota2 <- data.frame(age_mnth = 1:170) I've created ifelse…
Aybek Khodiev
  • 596
  • 1
  • 4
  • 10
27
votes
2 answers

Logical operators (AND, OR) with NA, TRUE and FALSE

I cannot understand the properties of logical (boolean) values TRUE, FALSE and NA when used with logical OR (|) and logical AND (&). Here are some examples: NA | TRUE # [1] TRUE NA | FALSE # [1] NA NA & TRUE # [1] NA NA & FALSE # [1] FALSE Can…
Remi.b
  • 17,389
  • 28
  • 87
  • 168
25
votes
1 answer

Is there anything wrong with using T & F instead of TRUE & FALSE?

I noticed that using T and F instead of TRUE and FALSE in functions in R gives me the same results. Of course, T and F are more concise, yet, I see TRUE and FALSE being used more often. I was wondering whether there is any difference between the…
Rob
  • 1,460
  • 2
  • 16
  • 23
25
votes
7 answers

Finding All Positions for Multiple Elements in a Vector

Suppose I have the following vector: x <- c(8, 6, 9, 9, 7, 3, 2, 5, 5, 1, 6, 8, 5, 2, 9, 3, 5, 10, 8, 2) How can I find which elements are either 8 or 9?
Christopher DuBois
  • 42,350
  • 23
  • 71
  • 93
24
votes
4 answers

View the source of an R package

Is there an easy way to view the source of an R package (or a method in a package), from within the interactive environment?
pufferfish
  • 16,651
  • 15
  • 56
  • 65
22
votes
3 answers

R: How to handle times without dates?

I have data which includes Date as well as Time enter and Time exit. These latter two contain data like this: 08:02, 12:02, 23:45 etc. I would like to manipulate the Time eXXX data - for example, substract Time enter from Time exit to work out…
gisol
  • 754
  • 3
  • 8
  • 20
21
votes
1 answer

What are primitive, internal, builtin, and special functions?

I have seen that some functions that call C-code are described as primitive, internal, builtin, or special. What are these functions?
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
20
votes
5 answers

How can I avoid having my R script printed every time I run it?

Suppose I have an R script: library('nnet') something <- runif(50); print(something) When I run this script from the command line, it prints: > library('nnet') > something <- runif(5); > print(something) [1] 0.04665518 0.93574275 0.96387299…
John Doucette
  • 4,370
  • 5
  • 37
  • 61
20
votes
4 answers

ERROR: a 'NAMESPACE' file is required

I am trying to install some R packages on a Linux machine using R CMD INSTALL -l and I see an error message: ERROR: a 'NAMESPACE' file is required I am using R 3.0.1. Please…
user2448881
  • 311
  • 1
  • 3
  • 7
19
votes
1 answer

Issue when passing variable with dollar sign notation ($) to aes() in combination with facet_grid() or facet_wrap()

I am doing some analysis in ggplot2 at the moment for a project and by chance I stumbled across some (for me) weird behavior that I cannot explain. When I write aes(x = cyl, ...) the plot looks different to what it does if I pass the same variable…
Christoph
  • 575
  • 4
  • 15
18
votes
2 answers

Why does summarize or mutate not work with group_by when I load `plyr` after `dplyr`?

Note: The title of this question has been edited to make it the canonical question for issues when plyr functions mask their dplyr counterparts. The rest of the question remains unchanged. Suppose I have the following data: dfx <- data.frame( …
Ignacio
  • 7,646
  • 16
  • 60
  • 113