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
2votes
1answer
88views

dplyr groups not working with dollar sign data$column syntax

I'm looking to find the min and max values of a column for each group: mtcars %>% group_by(mtcars$cyl) %>% summarize( min_mpg = min(mtcars$mpg), max_mpg = max(mtcars$mpg) ) # # A ...
user avatar
  • 49
0votes
1answer
481views

Why am I losing categorical data in my regression summary?

box <- read.csv("BlackBoxtrainApril22.csv") #Change the 2 categorical variables into factors box$SOUND <- as.factor(box$SOUND) box$SWITCH <- as.factor(box$SWITCH) #divide training ...
user avatar
11votes
1answer
8kviews

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?
user avatar
  • 45.5k
1vote
1answer
208views

How can I ask a great ggplot / lattice / base plot question?

I have read the How to make a great R reproducible example famous thread, but there is no specific advice regarding how to ask a great ggplot, lattice or base-plots great R question. What are your ...
13votes
1answer
3kviews

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 ...
user avatar
  • 3,701
27votes
1answer
3kviews

How to convert a Shiny app consisting of multiple files into an easily shareable and reproducible Shiny example?

There are resources on how to create a Minimal, Complete, and Verifiable example in general on Stack Overflow, and on how to make a great R reproducible example. However, there are no similar ...
user avatar
  • 23k
50votes
3answers
125kviews

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 : Factor w/...
user avatar
  • 563
20votes
1answer
2kviews

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?
user avatar
4votes
1answer
1kviews

env argument does not work in system2?

I am trying to run a command using system2 and to set an environment variable using the env argument, but it appears not to be set. I'm on Linux so this should work [in the helpfile it mentions that ...
user avatar
92votes
2answers
192kviews

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) ...
user avatar
3votes
3answers
547views

Remove groups with less than three unique observations

I would like to subset my data frame to keep only groups that have 3 or more observations on DIFFERENT days. I want to get rid of groups that have less than 3 observations, or the observations they ...
user avatar
34votes
4answers
31kviews

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 ...
user avatar
  • 1,463
21votes
7answers
65kviews

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 ...
user avatar
51votes
4answers
40kviews

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?
user avatar
  • 1,044
10votes
1answer
163views

What is the rationale for as.logical double coercion?

I'm trying to understand the coercion rationale for doubles: as.logical(c(-1, -0.01, 0, 0.01, 0.1,1:10)) #> [1] TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE #> [12] TRUE ...
user avatar
  • 4,038
17votes
1answer
3kviews

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 ...
user avatar
  • 545
40votes
4answers
84kviews

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 ...
97votes
8answers
70kviews

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?
user avatar
44votes
7answers
33kviews

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 ...
user avatar
  • 1,726
57votes
1answer
9kviews

How and when should I use on.exit?

on.exit calls code when a function exits, but how and when should I use it?
user avatar
5votes
2answers
7kviews

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 ...
user avatar
  • 7,968
5votes
4answers
47kviews

What is the difference between = and ==?

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 =?
user avatar
29votes
2answers
9kviews

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") #...
user avatar
42votes
5answers
296kviews

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': ...
user avatar
89votes
2answers
148kviews

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::...
user avatar
38votes
1answer
63kviews

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 ...
user avatar
14votes
1answer
40kviews

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 ...
user avatar
174votes
6answers
460kviews

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?
user avatar
  • 4,295
231votes
6answers
326kviews

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))...
user avatar
  • 8,025
18votes
2answers
10kviews

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 ...
user avatar
  • 7,056
83votes
6answers
213kviews

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 ...
user avatar
  • 3,465
236votes
10answers
169kviews

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 <- ...
user avatar
  • 4,421
56votes
3answers
321kviews

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: ...
user avatar
648votes
18answers
839kviews

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 ...
user avatar
44votes
9answers
34kviews

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, ...
user avatar
  • 965
133votes
17answers
152kviews

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,...
user avatar
  • 1,655
38votes
4answers
31kviews

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....
user avatar
  • 1,061
12votes
1answer
752views

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 ...
user avatar
  • 769
40votes
2answers
66kviews

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?
user avatar
  • 1,666
20votes
3answers
12kviews

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, ...
user avatar
  • 704
34votes
2answers
24kviews

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). ...
user avatar
  • 2,974
159votes
6answers
89kviews

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 ...
user avatar
258votes
2answers
142kviews

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?
user avatar
171votes
10answers
110kviews

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( ...
user avatar
  • 5,853
104votes
20answers
239kviews

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 ...
user avatar
  • 2,049
36votes
5answers
4kviews

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 ...
user avatar
  • 1,249
33votes
4answers
25kviews

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 ...
user avatar
  • 535
51votes
6answers
298kviews

Export data from R to Excel

I am writing codes to export database from R into Excel, I have been trying others codes including: write.table(ALBERTA1, "D:/ALBERTA1.txt", sep="\t") write.csv(ALBERTA1,":\ALBERTA1.csv") ...
user avatar
63votes
10answers
44kviews

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'...
user avatar
639votes
11answers
367kviews

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> &...
user avatar

15 30 50 per page
1
2 3 4 5