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
431
votes
13 answers

Sample random rows in dataframe

I am struggling to find the appropriate function that would return a specified number of rows picked up randomly without replacement from a data frame in R language? Can anyone help me out?
nikhil
  • 9,023
  • 22
  • 55
  • 81
414
votes
15 answers

How can I trim leading and trailing white space?

I am having some trouble with leading and trailing white space in a data.frame. For example, I look at a specific row in a data.frame based on a certain condition: > myDummy[myDummy$country == c("Austria"),c(1,2,3:7,19)] [1] codeHelper …
mropa
  • 11,562
  • 10
  • 33
  • 29
389
votes
12 answers

How does one reorder columns in a data frame?

How would one change this input (with the sequence: time, in, out, files): Time In Out Files 1 2 3 4 2 3 4 5 To this output (with the sequence: time, out, in, files)? Time Out In Files 1 3 2 4 2 4…
Catherine
  • 5,345
  • 11
  • 30
  • 28
389
votes
9 answers

Test if characters are in a string

I'm trying to determine if a string is a subset of another string. For example: chars <- "test" value <- "es" I want to return TRUE if "value" appears as part of the string "chars". In the following scenario, I would want to return false: chars…
mike
  • 22,931
  • 31
  • 77
  • 100
381
votes
16 answers

Order Bars in ggplot2 bar graph

I am trying to make a bar graph where the largest bar would be nearest to the y axis and the shortest bar would be furthest. So this is kind of like the Table I have Name Position 1 James Goalkeeper 2 Frank Goalkeeper 3 Jean …
Julio Diaz
  • 9,067
  • 19
  • 55
  • 70
366
votes
14 answers

How to reshape data from long to wide format

I'm having trouble rearranging the following data frame: set.seed(45) dat1 <- data.frame( name = rep(c("firstName", "secondName"), each=4), numbers = rep(1:4, 2), value = rnorm(8) ) dat1 name numbers value 1 firstName …
Steve
  • 5,727
  • 10
  • 32
  • 30
364
votes
5 answers

Plotting two variables as lines using ggplot2 on the same graph

A very newbish question, but say I have data like this: test_data <- data.frame( var0 = 100 + c(0, cumsum(runif(49, -20, 20))), var1 = 150 + c(0, cumsum(runif(49, -10, 10))), date = seq(as.Date("2002-01-01"), by="1 month",…
fmark
  • 57,259
  • 27
  • 100
  • 107
363
votes
15 answers

Formatting Decimal places in R

I have a number, for example 1.128347132904321674821 that I would like to show as only two decimal places when output to screen (or written to a file). How does one do that? x <- 1.128347132904321674821 EDIT: The use of: options(digits=2) Has…
Brandon Bertelsen
  • 43,807
  • 34
  • 160
  • 255
362
votes
19 answers

How can I remove an element from a list?

I have a list and I want to remove a single element from it. How can I do this? I've tried looking up what I think the obvious names for this function would be in the reference manual and I haven't found anything appropriate.
David Locke
  • 17,926
  • 9
  • 33
  • 53
359
votes
8 answers

Evaluate expression given as a string

I'm curious to know if R can use its eval() function to perform calculations provided by e.g. a string. This is a common case: eval("5+5") However, instead of 10 I get: [1] "5+5" Any solution?
Federico Giorgi
  • 10,495
  • 9
  • 42
  • 56
348
votes
8 answers

Concatenate a vector of strings/character

If I have a vector of type character, how can I concatenate the values into string? Here's how I would do it with paste(): sdata = c('a', 'b', 'c') paste(sdata[1], sdata[2], sdata[3], sep ='') yielding "abc". But of course, that only works if I…
Nick
  • 21,555
  • 18
  • 47
  • 50
346
votes
11 answers

How to save a plot as image on the disk?

I plot a simple linear regression using R. I would like to save that image as PNG or JPEG, is it possible to do it automatically? (via code) There are two different questions: First, I am already looking at the plot on my monitor and I would like to…
blakc05
  • 3,629
  • 4
  • 16
  • 6
338
votes
9 answers

Simultaneously merge multiple data.frames in a list

I have a list of many data.frames that I want to merge. The issue here is that each data.frame differs in terms of the number of rows and columns, but they all share the key variables (which I've called "var1" and "var2" in the code below). If the…
bshor
  • 4,859
  • 8
  • 24
  • 33
331
votes
18 answers

Split data frame string column into multiple columns

I'd like to take data of the form before = data.frame(attr = c(1,30,4,6), type=c('foo_and_bar','foo_and_bar_2')) attr type 1 1 foo_and_bar 2 30 foo_and_bar_2 3 4 foo_and_bar 4 6 foo_and_bar_2 and use split() on the column…
jkebinger
  • 3,944
  • 4
  • 19
  • 14
331
votes
18 answers

ggplot with 2 y axes on each side and different scales

I need to plot a bar chart showing counts and a line chart showing rate all in one chart, I can do both of them separately, but when I put them together, I scale of the first layer (i.e. the geom_bar) is overlapped by the second layer (i.e. the…
lokheart
  • 23,743
  • 39
  • 98
  • 169
1 2
3
16 17