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
415votes
32answers
196kviews

Elegant way to check for missing packages and install them?

I seem to be sharing a lot of code with coauthors these days. Many of them are novice/intermediate R users and don't realize that they have to install packages they don't already have. Is there an ...
137votes
4answers
276kviews

Controlling number of decimal digits in print output in R

There is an option in R to get control over digit display. For example: options(digits=10) is supposed to give the calculation results in 10 digits till the end of R session. In the help file of R, ...
285votes
30answers
142kviews

Determine path of the executing script

I have a script called foo.R that includes another script other.R, which is in the same directory: #!/usr/bin/env Rscript message("Hello") source("other.R") But I want R to find that other.R no ...
159votes
10answers
150kviews

Dynamically select data frame columns using $ and a character value

I have a vector of different column names and I want to be able to loop over each of them to extract that column from a data.frame. For example, consider the data set mtcars and some variable names ...
289votes
6answers
58kviews

Why are these numbers not equal?

The following code is obviously wrong. What's the problem? i <- 0.1 i <- i + 0.05 i ## [1] 0.15 if(i==0.15) cat("i equals 0.15") else cat("i does not equal 0.15") ## i does not equal 0.15
125votes
7answers
93kviews

Scheduling R Script

I have written an R script that pulls some data from a database, performs several operations on it and post the output to a new database. I would like this script to run every day at a specific time ...
655votes
16answers
1.7mviews

Plot two graphs in same plot in R

I would like to plot y1 and y2 in the same plot. x <- seq(-2, 2, 0.05) y1 <- pnorm(x) y2 <- pnorm(x, 1, 1) plot(x, y1, type = "l", col = "red") plot(x, y2, type = "l", col = "green") But ...
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 ...
254votes
16answers
571kviews

Change R default library path using .libPaths in Rprofile.site fails to work

I am running R on Windows, not as an administrator. When I install a package, the following command doesn't work: > install.packages("zoo") Installing package(s) into ‘C:/Program Files/R/R-2.15.2/...
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 <- ...
105votes
7answers
174kviews

Converting year and month ("yyyy-mm" format) to a date?

I have a dataset that looks like this: Month count 2009-01 12 2009-02 310 2009-03 2379 2009-04 234 2009-05 14 2009-08 1 2009-09 34 2009-10 2386 I want to plot the data (months as x values ...
236votes
10answers
312kviews

How do I make a list of data frames?

How do I make a list of data frames and how do I access each of those data frames from the list? For example, how can I put these data frames in a list ? d1 <- data.frame(y1 = c(1, 2, 3), ...
37votes
6answers
31kviews

Why can't I get a p-value smaller than 2.2e-16?

I've found this issue with t-tests and chi-squared in R but I assume this issue applies generally to other tests. If I do: a <- 1:10 b <- 100:110 t.test(a,b) I get: t = -64.6472, df = 18.998,...
57votes
7answers
57kviews

How to subtract years?

I have a date in R, e.g.: dt = as.Date('2010/03/17') I would like to subtract 2 years from this date, without worrying about leap years and such issues, getting as.Date('2010-03-17'). How would I ...
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 ...
138votes
11answers
141kviews

How to read data when some numbers contain commas as thousand separator?

I have a csv file where some of the numerical values are expressed as strings with commas as thousand separator, e.g. "1,513" instead of 1513. What is the simplest way to read the data into R? I can ...
405votes
15answers
548kviews

Side-by-side plots with ggplot2

I would like to place two plots side by side using the ggplot2 package, i.e. do the equivalent of par(mfrow=c(1,2)). For example, I would like to have the following two plots show side-by-side with ...
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))...
47votes
5answers
12kviews

A comprehensive survey of the types of things in R; 'mode' and 'class' and 'typeof' are insufficient

The language R confuses me. Entities have modes and classes, but even this is insufficient to fully describe the entity. This answer says In R every 'object' has a mode and a class. So I did ...
302votes
16answers
514kviews

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 ...
994votes
18answers
2.1mviews

Remove rows with all or some NAs (missing values) in data.frame

I'd like to remove the lines in this data frame that: a) contain NAs across all columns. Below is my example data frame. gene hsap mmul mmus rnor cfam 1 ENSG00000208234 0 NA NA ...
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") #...
276votes
15answers
448kviews

How to import multiple .csv files at once?

Suppose we have a folder containing multiple data.csv files, each containing the same number of variables but each from different times. Is there a way in R to import them all simultaneously rather ...
123votes
13answers
46kviews

General suggestions for debugging in R

I get an error when using an R function that I wrote: Warning messages: 1: glm.fit: algorithm did not converge 2: glm.fit: algorithm did not converge What I have done: Step through the function ...
45votes
4answers
266kviews

Convert date-time string to class Date

I have a data frame with a character column of date-times. When I use as.Date, most of my strings are parsed correctly, except for a few instances. The example below will hopefully show you what is ...
32votes
3answers
20kviews

How can I remove all duplicates so that NONE are left in a data frame?

There is a similar question for PHP, but I'm working with R and am unable to translate the solution to my problem. I have this data frame with 10 rows and 50 columns, where some of the rows are ...
180votes
19answers
118kviews

Replacing NAs with latest non-NA value

In a data.frame (or data.table), I would like to "fill forward" NAs with the closest previous non-NA value. A simple example, using vectors (instead of a data.frame) is the following: > y ...
326votes
18answers
563kviews

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 ...
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?
86votes
8answers
185kviews

Calculate the mean by group

I have a large data frame that looks similar to this: df <- data.frame(dive = factor(sample(c("dive1","dive2"), 10, replace=TRUE)), speed = runif(10) ...
182votes
8answers
435kviews

Convert data.frame column format from character to factor

I would like to change the format (class) of some columns of my data.frame object (mydf) from charactor to factor. I don't want to do this when I'm reading the text file by read.table() function. ...
309votes
9answers
237kviews

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 ...
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': ...
615votes
12answers
287kviews

The difference between bracket [ ] and double bracket [[ ]] for accessing the elements of a list or dataframe

R provides two different methods for accessing the elements of a list or data.frame: [] and [[]]. What is the difference between the two, and when should I use one over the other?
52votes
8answers
21kviews

Reshaping multiple sets of measurement columns (wide format) into single columns (long format)

I have a dataframe in a wide format, with repeated measurements taken within different date ranges. In my example there are three different periods, all with their corresponding values. E.g. the first ...
1442votes
13answers
1.6mviews

How to join (merge) data frames (inner, outer, left, right)

Given two data frames: df1 = data.frame(CustomerId = c(1:6), Product = c(rep("Toaster", 3), rep("Radio", 3))) df2 = data.frame(CustomerId = c(2, 4, 6), State = c(rep("Alabama", 2), rep("Ohio", 1))) ...
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?
92votes
17answers
344kviews

Generate a dummy-variable

I have had trouble generating the following dummy-variables in R: I'm analyzing yearly time series data (time period 1948-2009). I have two questions: How do I generate a dummy variable for ...
390votes
15answers
343kviews

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(&...
29votes
3answers
25kviews

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". ...
215votes
8answers
208kviews

Reshaping data.frame from wide to long format

I have some trouble to convert my data.frame from a wide table to a long table. At the moment it looks like this: Code Country 1950 1951 1952 1953 1954 AFG Afghanistan 20,249 ...
597votes
7answers
844kviews

Test if a vector contains a given element

How to check if a vector contains a given value?
48votes
4answers
179kviews

Plot multiple columns on the same graph in R [duplicate]

I have the following data frame: A B C D Xax 0.451 0.333 0.034 0.173 0.22 0.491 0.270 0.033 0.207 0.34 0.389 0.249 0.084 0.271 0.54 0....
87votes
11answers
213kviews

How to plot all the columns of a data frame in R

The data frame has n columns and I would like to get n plots, one plot for each column. I'm a newbie and I am not fluent in R, anyway I found two solutions. The first one works but it does not print ...
581votes
16answers
433kviews

Drop unused factor levels in a subsetted data frame

I have a data frame containing a factor. When I create a subset of this dataframe using subset or another indexing function, a new data frame is created. However, the factor variable retains all of ...
149votes
7answers
265kviews

Order discrete x scale by frequency/value

I am making a dodged bar chart using ggplot with discrete x scale, the x axis are now arranged in alphabetical order, but I need to rearrange it so that it is ordered by the value of the y-axis (i.e., ...
2468votes
23answers
388kviews

How to make a great R reproducible example

When discussing performance with colleagues, teaching, sending a bug report or searching for guidance on mailing lists and here on Stack Overflow, a reproducible example is often asked and always ...
100votes
7answers
290kviews

Fitting a density curve to a histogram in R

Is there a function in R that fits a curve to a histogram? Let's say you had the following histogram hist(c(rep(65, times=5), rep(25, times=5), rep(35, times=10), rep(45, times=4))) It looks normal,...
549votes
12answers
255kviews

Quickly reading very large tables as dataframes

I have very large tables (30 million rows) that I would like to load as a dataframes in R. read.table() has a lot of convenient features, but it seems like there is a lot of logic in the ...
325votes
14answers
726kviews

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....

15 30 50 per page
1
2 3 4 5