All Questions

808
votes
12answers
612671 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))) ...
97
votes
6answers
95695 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? I can't find a simple example of this anywhere. SO is telling me my post is too short, so I'll get th...
72
votes
4answers
43953 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 ...
1004
votes
15answers
953447 views

How to sort a dataframe by column(s)?

I want to sort a data.frame by multiple columns. For example, with the data.frame below I would like to sort by column z (descending) then by column b (ascending): dd <- data.frame(b = factor(c("...
54
votes
5answers
45400 views

Dynamically select data frame columns using $ and a vector of column names

I wish to order a data frame based on different columns, one at a turn. I have a character vector with the relevant column names on which the order should be based: parameter <- c("market_value_LO...
407
votes
8answers
142019 views

Quickly reading very large tables as dataframes in R

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 implementati...
148
votes
7answers
98605 views

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 call...
92
votes
4answers
109428 views

Aggregate / summarize multiple variables per group (i.e. sum, mean, etc)

From a data frame, is there a easy way to aggregate (i.e. sum) multiple variables simultaneously? Below are some sample data: library(lubridate) days = 365*2 date = seq(as.Date("2000-01-01"), length...
119
votes
14answers
155789 views

Split a column of a data frame to 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 ...
90
votes
2answers
42038 views

Reshape three column data frame to matrix ("long" to "wide" format)

I have a data.frame that looks like this. x a 1 x b 2 x c 3 y a 3 y b 3 y c 2 I want this in matrix form so I can feed it to heatmap to make a plot. The result should look something like: ...

Previous Next