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
52
votes
8 answers

Numeric comparison difficulty in R

I'm trying to compare two numbers in R as a part of a if-statement condition: (a-b) >= 0.5 In this particular instance, a = 0.58 and b = 0.08... and yet (a-b) >= 0.5 is false. I'm aware of the dangers of using == for exact number comparisons, and…
Matt Parker
  • 26,709
  • 7
  • 54
  • 72
51
votes
1 answer

How to call an object with the character variable of the same name

I'm trying to write a function in R to batch-analyse a number of files in a similar manner. The files are of class ExpressionSetIllumina. I can make a character (string) vector with names of all files in the directory and load each of them: list =…
Ophiothrix
  • 791
  • 1
  • 8
  • 11
51
votes
5 answers

A comprehensive survey of the types of things in R; 'mode' and 'class' and 'typeof' are insufficient

The language R confuses me. Entities have modes and classes, but even this is insufficient to fully describe the entity. This answer says In R every 'object' has a mode and a class. So I did these experiments: > class(3) [1] "numeric" >…
Aaron McDaid
  • 26,501
  • 9
  • 66
  • 88
51
votes
2 answers

How to check the OS within R

Possible Duplicates: detecting operating system in R (e.g. for adaptive .Rprofile files) How can I determine in R what platform I'm running on? Is there a primitive function in R that will return information about the system on which R is…
gappy
  • 10,095
  • 14
  • 54
  • 73
51
votes
6 answers

Vectorized IF statement in R?

x <- seq(0.1,10,0.1) y <- if (x < 5) 1 else 2 This gives a warning (or error since R version 4.2.0) that the condition has length > 1. I would want the if to operate on every single case instead of operating on the whole vector. What do I have to…
Christian
  • 25,249
  • 40
  • 134
  • 225
50
votes
3 answers

Convert integer to class Date

I have an integer which I want to convert to class Date. I assume I first need to convert it to a string, but how? My attempt: v <- 20081101 date <- as.Date(v, format("%Y%m%d")) Error in charToDate(x) : character string is not in a…
Elzo Valugi
  • 27,240
  • 15
  • 95
  • 114
49
votes
4 answers

Variable name restrictions in R

What are the restrictions as to what characters (and maybe other restrictions) can be used for a variable name in R? (This screams of general reference, but I can't seem to find the answer)
Kyle Brandt
  • 26,938
  • 37
  • 124
  • 165
49
votes
1 answer

One function to detect NaN, NA, Inf, -Inf, etc.?

Is there a single function in R that determines if a value is NA, NaN, Inf, -Inf, or otherwise not a well-formed number?
Suraj
  • 35,905
  • 47
  • 139
  • 250
49
votes
7 answers

Find complement of a data frame (anti - join)

I have two data frames(df and df1). df1 is subset of df. I want to get a data frame which is complement of df1 in df, i.e. return rows of the first data set which are not matched in the second. For example let, data frame…
oercim
  • 1,808
  • 2
  • 17
  • 34
48
votes
4 answers

How do I install a package that has been archived from CRAN?

I typed the following in the R command line: install.packages("RecordLinkage") I got the following error: Warning in install.packages : package ‘RecordLinkage’ is not available (for R version 3.1.0) However, one of my coworkers did the exact…
Rainmaker
  • 1,181
  • 2
  • 11
  • 11
47
votes
6 answers

How to convert character of percent into numeric in R

I have data with percent signs (%) that I want to convert into numeric. I run into a problem when converting character of percentage to numeric. E.g. I want to convert "10%" into 10%, but as.numeric("10%") returns NA. Do you have any ideas?
Frank Wang
  • 1,462
  • 3
  • 17
  • 39
47
votes
13 answers

How to create a consecutive group number

I have a data frame (all_data) in which I have a list of sites (1... to n) and their scores e.g. site score 1 10 1 11 1 12 4 10 4 11 4 11 8 9 8 8 8 7 I want create a…
ThallyHo
  • 2,667
  • 5
  • 22
  • 19
47
votes
9 answers

Use variable names in functions of dplyr

I want to use variable names as strings in functions of dplyr. See the example below: df <- data.frame( color = c("blue", "black", "blue", "blue", "black"), value = 1:5) filter(df, color == "blue") It works perfectly, but I would like…
kuba
  • 1,005
  • 2
  • 11
  • 16
46
votes
4 answers

Read SAS sas7bdat data into R

What options does R have for reading files in the native SAS format, sas7bdat, into R? The NCES Common Core, for example, contains an extensive repository of data files saved in this format. For concreteness, let's focus on trying to read in this…
MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
46
votes
5 answers

What do the %op% operators in mean? For example "%in%"?

I tried to do this simple search but couldn't find anything on the percent (%) symbol in R. What does %in% mean in the following code? time(x) %in% time(y) where x and y are matrices. How do I look up help on %in% and similar functions that follow…
heavy rocker dude
  • 2,271
  • 8
  • 33
  • 47