Questions tagged [r]
R is a free, open-source programming language & software environment for statistical computing, bioinformatics, visualization & general computing. Please use minimal reproducible examples others can run using copy & paste. Show desired output entirely. Use dput() for data & specify all non-base packages with library(). Don't embed pictures for data or code, use indented code blocks instead. For statistics questions, use https://stats.stackexchange.com.
56,084
questions
2464
votes
23
answers
469k
views
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 ...
1547
votes
14
answers
1.8m
views
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)))
...
383
votes
15
answers
494k
views
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)
)...
251
votes
9
answers
335k
views
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 ...
311
votes
7
answers
69k
views
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
517
votes
19
answers
998k
views
How to sum a variable by group
I have a data frame with two columns. First column contains categories such as "First", "Second", "Third", and the second column has numbers that represent the number of times I saw the specific ...
271
votes
10
answers
384k
views
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),
...
158
votes
6
answers
54k
views
Split comma-separated strings in a column into separate rows
I have a data frame, like so:
data.frame(director = c("Aaron Blaise,Bob Walker", "Akira Kurosawa",
"Alan J. Pakula", "Alan Parker", "Alejandro Amenabar", "Alejandro Gonzalez ...
181
votes
10
answers
189k
views
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 ...
203
votes
10
answers
232k
views
Aggregate / summarize multiple variables per group (e.g. sum, mean)
From a data frame, is there a easy way to aggregate (sum, mean, max etc) multiple variables simultaneously?
Below are some sample data:
library(lubridate)
days = 365*2
date = seq(as.Date("2000-01-...
1154
votes
12
answers
441k
views
Grouping functions (tapply, by, aggregate) and the *apply family
Whenever I want to do something "map"py in R, I usually try to use a function in the apply family.
However, I've never quite understood the differences between them -- how {sapply, lapply, etc.} ...
113
votes
6
answers
59k
views
Collapse / concatenate / aggregate a column to a single comma separated string within each group
I want to aggregate one column in a data frame according to two grouping variables, and separate the individual values by a comma.
Here is some data:
data <- data.frame(A = c(rep(111, 3), rep(222,...
708
votes
14
answers
1.1m
views
How to convert a factor to integer\numeric without loss of information?
When I convert a factor to a numeric or integer, I get the underlying level codes, not the values as numbers.
f <- factor(sample(runif(5), 20, replace = TRUE))
## [1] 0.0248644019011408 0....
393
votes
16
answers
584k
views
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 ...
680
votes
11
answers
337k
views
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?