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
36
votes
3 answers

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
  • 23,484
  • 14
  • 36
  • 57
36
votes
1 answer

Generate multiple graphics from within an R function

I'd like to spawn several graphics windows from within a function in R using ggplot graphics... testf <- function(a, b) { devAskNewPage(TRUE) qplot(a, b); # grid.newpage(recording = TRUE) dev.new() qplot(a, a+a); # grid.newpage(recording…
William Doane
  • 1,416
  • 12
  • 20
35
votes
3 answers

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). This is related to twelvefold way. The "distinct"…
Randy Lai
  • 3,084
  • 2
  • 22
  • 23
35
votes
3 answers

Error in if/while (condition) { : argument is of length zero

I received the error Error in if (condition) { : argument is of length zero or Error in while (condition) { : argument is of length zero What causes this error message, and what does it mean? On further inspection it seems that the value is…
Craig Wright
  • 1,575
  • 1
  • 11
  • 19
34
votes
5 answers

Reshape multiple value columns to wide format

I have the following data frame and i want to use cast to create a "pivot table" with columns for two values (value and percent). Here is the data frame: expensesByMonth <- structure(list(month = c("2012-02-01", "2012-02-01", "2012-02-01",…
Alex Burdusel
  • 3,015
  • 5
  • 38
  • 49
33
votes
2 answers

How to filter a data frame

I have a data frame and tried to select only the observations I'm interested in by this: data[data["Var1"]>10] Unfortunately, this command destroys the data.frame structure and returns a long vector. What I want to get is the data.frame shortened…
Mike
  • 455
  • 2
  • 7
  • 9
32
votes
2 answers

Migrating R libraries

I'd like to move several R libraries (*) from one drive to another, on Linux, and would like to know whether a simple move is feasible and safe or if I should uninstall and reinstall the packages. I realize that the locations of libraries are…
Iterator
  • 20,250
  • 12
  • 75
  • 111
32
votes
3 answers

Add column which contains binned values of a numeric column

I have a dataframe with a few columns, one of those columns is ranks, an integer between 1 and 20. I want to create another column that contains a bin value like "1-4", "5-10", "11-15", "16-20". What is the most effective way to do this? the data…
wespiserA
  • 3,131
  • 5
  • 28
  • 36
32
votes
2 answers

Memory profiling in R - tools for summarizing

R has some tools for memory profiling, like Rprofmem(), Rprof() with option "memory.profiling=TRUE" and tracemem(). The last one can only be used on objects, and hence is useful to follow how many times an object is copied, but doesn't give an…
Joris Meys
  • 106,551
  • 31
  • 221
  • 263
32
votes
2 answers

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") ## [1] TRUE Is there a preference for which one I…
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
31
votes
8 answers

Conditional merge/replacement in R

I have two data frames: df1 x1 x2 1 a 2 b 3 c 4 d and df2 x1 x2 2 zz 3 qq I want to replace some of the values in df1$x2 with values in df2$x2 based on the conditional match between df1$x1 and df2$x2 to produce: df1 x1 x2 1 a 2 …
Mike
  • 761
  • 2
  • 7
  • 5
31
votes
2 answers

How to perform natural (lexicographic) sorting in R?

Is there a natural sort for R? Say I had a character vector like so: seq.names <- c('abc21', 'abc2', 'abc1', 'abc01', 'abc4', 'abc201', '1b', '1a') I'd like to sort it aphanumerically, so I get back this: c('1a', '1b', 'abc1', 'abc01', 'abc2',…
cbare
  • 12,060
  • 8
  • 56
  • 63
30
votes
3 answers

as.Date returning NA while converting from 'ddmmmyyyy'

I am trying to convert the string "2013-JAN-14" into a Date as follow : sdate1 <- "2013-JAN-14" ddate1 <- as.Date(sdate1,format="%Y-%b-%d") ddate1 but I get : [1] NA What am I doing wrong ? should I install a package for this purpose (I tried…
Ricky Bobby
  • 7,490
  • 7
  • 46
  • 63
30
votes
3 answers

How can I paste 100000 without it being shortened to 1e+05?

Question: How can I use paste without 100000 becoming 1e+05? Sorry in advance if this question seems frivolous (but it has resulted in a bug in my code). I use R to call an external script, so when I say e.g. paste("abc",100000) I want it to output…
Douglas S. Stones
  • 541
  • 1
  • 4
  • 14
30
votes
3 answers

Dollar sign before a variable

I have this sample code to create a new data frame 'new_data' from the existing data frame 'my_data'. new_data = NULL n = 10 #this number correspond to the number of rows in my_data conditions = c("Bas_A", "Bas_T", "Oper_A", "Oper_T") # the vector…
this.is.not.a.nick
  • 2,631
  • 3
  • 21
  • 26