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.

Filter by
Sorted by
Tagged with
0 votes
0 answers
24 views

Understanding Compiler Version Impact on Building R and R Packages

I am currently performing local testing based on rhub images, which requires to install R packages (and other dependencies) from source. I’ve noticed that the compiler versions used in the rhub images ...
Mat D.'s user avatar
  • 483
0 votes
0 answers
29 views

Order of discrete x scale is incorrect for a single value [duplicate]

I'm trying to populate a bar chart with R. Everything is working as expected, but one of the X values is placed in the wrong order. Here's my code: data<- read.csv("figure.csv", header = ...
Falling Into Infinity's user avatar
44 votes
3 answers
24k views

What does |> (pipe greater than) mean in R?

I have recently come across the code |> in R. It is a vertical line character (pipe) followed by a greater than symbol. Here is an example: mtcars |> head() What is the |> code doing?
Ian Campbell's user avatar
  • 23.9k
109 votes
5 answers
13k views

What are the differences between R's native pipe `|>` and the magrittr pipe `%>%`?

In R 4.1 (May 2021) a native pipe operator was introduced that is "more streamlined" than previous implementations. I already noticed one difference between the native |> and the magrittr ...
sieste's user avatar
  • 8,526
12 votes
1 answer
13k views

How should I deal with "'someFunction' is not an exported object from 'namespace:somePackage'" error? [closed]

I have this error: 'someFunction' is not an exported object from 'namespace:somePackage' Does anyone know how to solve it?
zx8754's user avatar
  • 53.5k
15 votes
1 answer
4k views

How to order data by value within ggplot facets

I have the following data frame: library(tidyverse) tdat <- structure(list(term = c("Hepatic Fibrosis / Hepatic Stellate Cell Activation", "Cellular Effects of Sildenafil (Viagra)", "Epithelial ...
littleworth's user avatar
  • 4,919
16 votes
1 answer
989 views

Create a group index for values connected directly and indirectly

I would like to generate indices to group observations based on two columns. But I want groups to be made of observation that share, at least one observation in commons. In the data below, I want to ...
Malta's user avatar
  • 1,893
62 votes
3 answers
163k views

How to debug "contrasts can be applied only to factors with 2 or more levels" error?

Here are all the variables I'm working with: str(ad.train) $ Date : Factor w/ 427 levels "2012-03-24","2012-03-29",..: 4 7 12 14 19 21 24 29 31 34 ... $ Team ...
Troy's user avatar
  • 683
9 votes
1 answer
18k views

`lm` summary not display all factor levels

I am running a linear regression on a number of attributes including two categorical attributes, B and F, and I don't get a coefficient value for every factor level I have. B has 9 levels and F has ...
Karen Roberts's user avatar
21 votes
1 answer
3k views

What are primitive, internal, builtin, and special functions? [closed]

I have seen that some functions that call C-code are described as primitive, internal, builtin, or special. What are these functions?
Richie Cotton's user avatar
105 votes
2 answers
237k views

What does "The following object is masked from 'package:xxx'" mean?

When I load a package, I get a message stating that: "The following object is masked from 'package:xxx' For example, if I load testthat then assertive, I get the following: library(testthat) ...
Richie Cotton's user avatar
38 votes
5 answers
40k views

dplyr left_join by less than, greater than condition

This question is somewhat related to issues Efficiently merging two data frames on a non-trivial criteria and Checking if date is between two dates in r. And the one I have posted here requesting if ...
rajvijay's user avatar
  • 1,661
28 votes
7 answers
75k views

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 ...
stackoverflowuser2010's user avatar
55 votes
4 answers
45k views

How do I get a list of built-in data sets in R?

Can someone please help how to get the list of built-in data sets and their dependency packages?
mockash's user avatar
  • 1,258
19 votes
1 answer
3k views

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 ...
Christoph's user avatar
  • 575
46 votes
4 answers
101k views

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 ...
117 votes
10 answers
92k views

Get filename without extension in R

I have a file: ABCD.csv The length before the .csv is not fixed and vary in any length. How can I extract the portion before the .csv?
Matrix.cursor's user avatar
50 votes
7 answers
39k views

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 ...
oercim's user avatar
  • 1,838
64 votes
1 answer
11k views

How and when should I use on.exit?

on.exit calls code when a function exits, but how and when should I use it?
Richie Cotton's user avatar
5 votes
2 answers
8k views

Remove backslashes from character string

I am reading text in from a txt file and pass the contents to SQL. The SQL text contains double quotes and is causing problems. I would like to remove the "\" in the string below so I can send it to ...
user3022875's user avatar
  • 8,688
6 votes
4 answers
60k views

What is the difference between = and == in R?

What is the difference between = and ==? I have found cases where the double equal sign will allow my script to run while one equal sign produces an error message. When should I use == instead of =?
bubbalouie's user avatar
33 votes
2 answers
12k views

What's the difference between is and inherits?

If I want to check whether a variable inherits from some class, I can either use is or inherits. class(letters) ## [1] "character" is(letters, "character") ## [1] TRUE inherits(letters, "character") #...
Richie Cotton's user avatar
45 votes
5 answers
330k views

What does "Error: object '<myvariable>' not found" mean?

I got the error message: Error: object 'x' not found Or a more complex version like Error in mean(x) : error in evaluating the argument 'x' in selecting a method for function 'mean': ...
Richie Cotton's user avatar
102 votes
3 answers
180k views

How do I deal with special characters like \^$.?*|+()[{ in my regex?

I want to match a regular expression special character, \^$.?*|+()[{. I tried: x <- "a[b" grepl("[", x) ## Error: invalid regular expression '[', reason 'Missing ']'' (Equivalently stringr::...
Richie Cotton's user avatar
44 votes
1 answer
79k views

Error in <my code> : target of assignment expands to non-language object

I received the error Error in <my code> : target of assignment expands to non-language object or Error in <my code> : invalid (do_set) left-hand side to assignment or Error in <my ...
Richie Cotton's user avatar
14 votes
1 answer
49k views

Error in if/while (condition) : argument is not interpretable as logical

I received the error Error in if (condition) { : argument is not interpretable as logical or Error in while (condition) { : argument is not interpretable as logical What does it mean, and how do ...
Richie Cotton's user avatar
205 votes
7 answers
612k views

What does %>% function mean in R?

I have seen the use of %>% (percent greater than percent) function in some packages like dplyr and rvest. What does it mean? Is it a way to write closure blocks in R?
alfakini's user avatar
  • 4,665
255 votes
6 answers
415k views

ggplot2 line chart gives "geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?"

With this data frame ("df"): year pollution 1 1999 346.82000 2 2002 134.30882 3 2005 130.43038 4 2008 88.27546 I try to create a line chart like this: plot5 <- ggplot(df, aes(year, pollution))...
megashigger's user avatar
  • 8,745
18 votes
2 answers
11k views

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 ...
Ignacio's user avatar
  • 7,676
93 votes
7 answers
247k views

How to plot a function curve in R

What are the alternatives for drawing a simple curve for a function like eq = function(x){x*x} in R? It sounds such an obvious question, but I could only find these related questions on ...
sjdh's user avatar
  • 3,917
290 votes
10 answers
214k views

Use dynamic name for new column/variable in `dplyr`

I want to use dplyr::mutate() to create multiple new columns in a data frame. The column names and their contents should be dynamically generated. Example data from iris: library(dplyr) iris <- ...
Timm S.'s user avatar
  • 5,245
60 votes
3 answers
386k views

Error: unexpected symbol/input/string constant/numeric constant/SPECIAL in my code

I received one of these errors. Error: unexpected symbol in "<my code>" Error: unexpected input in "<my code>" Error: unexpected string constant in "<my code>" Error: ...
Richie Cotton's user avatar
702 votes
19 answers
976k 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
47 votes
9 answers
40k views

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, ...
kuba's user avatar
  • 1,005
157 votes
19 answers
191k views

Select the row with the maximum value in each group

In a dataset with multiple observations for each subject. For each subject I want to select the row which have the maximum value of 'pt'. For example, with a following dataset: ID <- c(1,1,1,2,2,...
Xinting WANG's user avatar
  • 1,925
49 votes
4 answers
48k views

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....
Rainmaker's user avatar
  • 1,191
12 votes
1 answer
1k views

Order of operator precedence when using ":" (the colon)

I am trying to extract values from a vector using numeric vectors expressed in two seemingly equivalent ways: x <- c(1,2,3) x[2:3] # [1] 2 3 x[1+1:3] # [1] 2 3 NA I am confused why the ...
Marc's user avatar
  • 776
41 votes
2 answers
76k views

How to end a 'debug' mode? [duplicate]

In RStudio, I started a debug mode by debug(ls) ls() Then I do not know how to end this mode. The prompt changed to Browse[2]> How can I end this debugging mode?
user67275's user avatar
  • 2,010
69 votes
3 answers
60k views

How to deal with nonstandard column names (white space, punctuation, starts with numbers)

df <- structure(list(`a a` = 1:3, `a b` = 2:4), .Names = c("a a", "a b" ), row.names = c(NA, -3L), class = "data.frame") and the data looks like a a a b 1 1 2 2 2 3 3 3 4 Following ...
Flux's user avatar
  • 825
22 votes
3 answers
16k views

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, ...
gisol's user avatar
  • 754
35 votes
3 answers
28k views

How to generate permutations or combinations of object in R?

How to generate sequences of r objects from n objects? I'm looking for a way to do either permutations or combinations, with/without replacement, with distinct and non-distinct items (aka multisets). ...
Randy Lai's user avatar
  • 3,114
202 votes
6 answers
119k views

How to use a variable to specify column name in ggplot

I have a ggplot command ggplot( rates.by.groups, aes(x=name, y=rate, colour=majr, group=majr) ) inside a function. But I would like to be able to use a parameter of the function to pick out the ...
Theodore Norvell's user avatar
276 votes
2 answers
152k views

What are the main differences between R data files?

What are the main differences between .RData, .Rda and .Rds files? Are there differences in compression, etc.? When should each type be used? How can one type be converted to another?
Carlos Cinelli's user avatar
183 votes
10 answers
121k views

Group by multiple columns in dplyr, using string vector input

I'm trying to transfer my understanding of plyr into dplyr, but I can't figure out how to group by multiple columns. # make data with weird column names that can't be hard coded data = data.frame( ...
sharoz's user avatar
  • 6,207
119 votes
21 answers
282k views

Rename multiple columns by names

Someone should have asked this already, but I couldn't find an answer. Say I have: x = data.frame(q=1,w=2,e=3, ...and many many columns...) what is the most elegant way to rename an arbitrary ...
qoheleth's user avatar
  • 2,229
41 votes
6 answers
7k views

How to find all functions in an R package?

What is the best way to find all the functions associated in a package?? I am currently going through the caTools package. If I do ?caTools or ??caTools I am simply going to get search for functions ...
jessica's user avatar
  • 1,335
39 votes
4 answers
28k views

Subset data frame based on number of rows per group

I have data like this, where some "name" occurs more than three times: df <- data.frame(name = c("a", "a", "a", "b", "b", "c", "c", "c", "c"), x = 1:9) name x 1 a 1 2 a 2 3 a 3 4 b ...
SJSU2013's user avatar
  • 595
62 votes
8 answers
359k views

Export data from R to Excel

I am writing code to export database from R into Excel, I have been trying others code including: write.table(ALBERTA1, "D:/ALBERTA1.txt", sep="\t") write.csv(ALBERTA1,":\...
ntamjo achille's user avatar
68 votes
10 answers
50k views

Cleaning up factor levels (collapsing multiple levels/labels)

What is the most effective (ie efficient / appropriate) way to clean up a factor containing multiple levels that need to be collapsed? That is, how to combine two or more factor levels into one. Here'...
Ricardo Saporta's user avatar
687 votes
13 answers
428k 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

1
2 3 4 5 6