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

How to split a data frame?

I want to split a data frame into several smaller ones. This looks like a very trivial question, however I cannot find a solution from web search.
Leo5188
  • 1,967
  • 2
  • 17
  • 21
85
votes
3 answers

Convert a dataframe to a vector (by rows)

I have a dataframe with numeric entries like this one test <- data.frame(x = c(26, 21, 20), y = c(34, 29, 28)) How can I get the following vector? > 26, 34, 21, 29, 20, 28 I was able to get it using the following, but I guess there should be a…
Brani
  • 6,454
  • 15
  • 46
  • 49
84
votes
8 answers

Round up from .5

Yes I know why we always round to the nearest even number if we are in the exact middle (i.e. 2.5 becomes 2) of two numbers. But when I want to evaluate data for some people they don't want this behaviour. What is the simplest method to get this: x…
jakob-r
  • 6,824
  • 3
  • 29
  • 47
79
votes
5 answers

ending "+" prompt in R

I'm fairly new to R and I made a type-o while entering some code and now instead of getting the ">" prompt I get a "+" prompt. I appear to be stuck in some kind of function that is looking for input, but I can't seem to get out of it. I figure that…
TMin
  • 2,280
  • 1
  • 25
  • 34
75
votes
8 answers

duplicate 'row.names' are not allowed error

I am trying to load a csv file that has 14 columns like this: StartDate, var1, var2, var3, ..., var14 when I issue this command: systems <- read.table("http://getfile.pl?test.csv", header = TRUE, sep = ",") I get an error message. duplicate…
george willy
  • 1,693
  • 8
  • 22
  • 26
75
votes
4 answers

do-while loop in R

I was wondering about how to write do-while-style loop? I found this post: you can use repeat{} and check conditions whereever using if() and exit the loop with the "break" control word. I am not sure what it exactly means. Can someone please…
Tim
  • 1
  • 141
  • 372
  • 590
74
votes
6 answers

How to sort a character vector where elements contain letters and numbers?

I have a character array cf <- c("V440","V457","V116","V327","V446","V108", "V155","V217","V120","V51","V477") I would like to sort it in descending order so that I will have an output like…
rinzy kutex
  • 1,195
  • 1
  • 12
  • 13
73
votes
4 answers

Access variable value where the name of variable is stored in a string

Similar questions have been raised for other languages: C, sql, java, etc. But I'm trying to do this in R. I have: ret_series <- c(1, 2, 3) x <- "ret_series" How do I get (1, 2, 3) by calling some function / manipulation on x, without direct…
Zhang18
  • 4,800
  • 10
  • 50
  • 67
70
votes
4 answers

R round to nearest .5 or .1

I have a data set of stock prices that have already been rounded to 2 decimal places (1234.56). I am now trying to round to a specific value which is different for each stock. Here are some examples: Current Stock Price Minimum Tick…
screechOwl
  • 27,310
  • 61
  • 158
  • 267
70
votes
4 answers

How to load packages in R automatically?

Could you suggest me a way for loading packages in R automatically? I mean, I want to start a session in R without needing to use library('package name') several times. Suppose I downloaded all packages I'll want to use the next time I start R.
nhern121
  • 3,831
  • 6
  • 27
  • 40
69
votes
4 answers

Split data.frame based on levels of a factor into new data.frames

I'm trying to create separate data.frame objects based on levels of a factor. So if I have: df <- data.frame( x=rnorm(25), y=rnorm(25), g=rep(factor(LETTERS[1:5]), 5) ) How can I split df into separate data.frames for each level of g…
smillig
  • 5,073
  • 6
  • 36
  • 46
68
votes
1 answer

ggplot's qplot does not execute on sourcing

Let's assume I have 2 source files, the first one named example1.r and the second one example2.r (given below). example1.r plot(1:10,1:10) example2.r qplot(1:10,1:10) When I source example1.r, the graph is drawn. It does not, however, when I…
Grega Kešpret
  • 11,827
  • 6
  • 39
  • 44
68
votes
3 answers

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 call to select select(df, 'a a') gives Error in…
Flux
  • 815
  • 1
  • 6
  • 6
67
votes
11 answers

Count number of rows per group and add result to original data frame

Say I have a data.frame object: df <- data.frame(name=c('black','black','black','red','red'), type=c('chair','chair','sofa','sofa','plate'), num=c(4,5,12,4,3)) Now I want to count the number of rows (observations)…
Uri Laserson
  • 2,391
  • 5
  • 30
  • 39
67
votes
10 answers

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's an example where the two levels "Yes" and "Y"…
Ricardo Saporta
  • 54,400
  • 17
  • 144
  • 178