Questions tagged [reshape2]

The reshape2 package in R contains functions that allow data to be transformed into more convenient forms.

reshape2 is an package for flexibly restructuring and aggregating data. Its primary use is ing data between wide format with repeated measurements in separate columns of the same record and long format with the repeated measurements in separate records.

reshape2 succeeds the reshape package, and is succeeded by the package.

Repositories

Other resources

Related tags

1177 questions
6
votes
1 answer

R: Collapse multiple boolean columns into single attribute column with new rows for each combination

Trying to melt or collapse a dataframe with multiple boolean columns into a two column database with an id column and a column for the collapsed values BUT each value results in a new row. Example beginning: A S1 S2 S3 S4 1 ex1 1 0 0 0 2…
Sean Kelso
  • 77
  • 3
6
votes
2 answers

Combining multiple rows into one row with multiple columns of data R

I have a dataset where the same effect was measured in different ways and I want to compare those measurements. My dataset looks like this: Study MType ID Insect Mean Sd N Alla Fecundity 1 Aphid .62 .7628…
birdoptera
  • 117
  • 1
  • 2
  • 6
6
votes
1 answer

Correlation Matrix - tidyr gather v. reshape2 melt

I would like to use ggplot2 to make an upper triangle correlation matrix like this one. I can replicate that one just fine, but for some reason I'm stuck on really wanting to convert the reshape2 functions to tidyr ones. I would think that I could…
Nick Criswell
  • 1,733
  • 2
  • 16
  • 32
6
votes
1 answer

How to use tidyr (or similar data wrangling) on "big" data in postgreSQL (Redshift) database

I have 4 billion rows of data in a 12 node redshift cluster. I am successfully able to connect to it with the Rpostgreqsql package and use dplyr to do basic data wrangling. However, I'd like to do some data reshaping which I'd normally use reshape2…
leaRningR909
  • 235
  • 3
  • 10
6
votes
1 answer

How do I subset datetimes and pivot the measurement column in R

I have a dataframe like this Datetime <- c("2015-12-31 08:30:13", "2015-12-31 12:45:00", "2016-01-01 02:53:20", "2016-01-01 03:22:18", "2016-01-01 09:42:10", "2016-01-01 20:55:50", "2016-01-01 21:14:10", "2016-01-02 05:42:16", …
Sharath
  • 2,225
  • 3
  • 24
  • 37
6
votes
4 answers

less clunky reshaping of anscombe data

I was trying to use ggplot2 to plot the built-in anscombe data set in R (which contains four different small data sets with identical correlations but radically different relationships between X and Y). My attempts to reshape the data properly were…
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
6
votes
3 answers

Pivot rows into columns with values of counts for each measurement R

I have a sample dataframe that I am working with ID <- c("A","A","A","A","A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B","B","B") TARG_AVG <-…
Sharath
  • 2,225
  • 3
  • 24
  • 37
6
votes
3 answers

Unlisting columns by groups

I have a dataframe in the following format: id | name | logs ---+--------------------+----------------------------------------- 84 | "zibaroo" | "C47931038" 12 |…
retrography
  • 6,302
  • 3
  • 22
  • 32
6
votes
3 answers

Use put two value columns in spread() function in R

I just posted a question recently asking how to reshape data from a long table to a wide table. Then I found spread() is a quite handy function for doing this. So now I need some further development on my previous post. Let's suppose we have a table…
Lambo
  • 857
  • 3
  • 14
  • 39
6
votes
4 answers

Complex data reshaping in R

I have a data frame with 3 columns (extract below): df <- data.frame( id = c(1,1,1,2,2,2), Year = c(2007, 2008, 2009, 2007, 2008, 2009), A = c(5, 2, 3, 7, 5, 6), B = c(10, 0, 50, 13, 17, 17) ) df I'd like to have this: df_needed <-…
user3904098
6
votes
1 answer

Transposing a data.table with reshape2:::dcast

I am transposing a data.table and have chosen to use reshape2:::dcast, however I am plagued by a strange handling of the data.table... here is a toy data set that replicates the behavior: > library(data.table) > library(reshape2) > DT <-…
mlegge
  • 6,763
  • 3
  • 40
  • 67
6
votes
2 answers

Reshaping data table to make column names into row names

I have a data.table in R > dt SAMPLE junction count 1: R1 a 1 2: R2 a 1 3: R3 b 1 4: R3 a 1 5: R1 c 2 Now I want to "reshape" the data table to form a data frame m (basically…
hi15
  • 2,113
  • 6
  • 28
  • 51
6
votes
4 answers

R get rows based on multiple conditions - use dplyr and reshape2

df <- data.frame( exp=c(1,1,2,2), name=c("gene1", "gene2", "gene1", "gene2"), value=c(1,1,3,-1) ) In trying to get customed to the dplyr and reshape2I stumbled over a "simple" way to select rows based on several conditions. If I want…
user3375672
  • 3,728
  • 9
  • 41
  • 70
6
votes
4 answers

How to strsplit data frame column and replicate rows accordingly?

I have a data frame like this: > df <- data.frame(Column1=c("id1", "id2", "id3"), Column2=c("text1,text2,text3", "text4", "text5,text6"), Column3=c("text7", "text8,text9,text10,text11", "text12,text13")) > df Column1 Column2 …
enricoferrero
  • 2,249
  • 1
  • 23
  • 28
6
votes
3 answers

Melt a dataframe by pattern in colnames

I have several dataframes, each with more than 250 variables. A partial dput from the first dataframe: df <- structure(list(id = structure(1:6, .Label = c("00", "01", "02", "03", "04", "05", "06", "08", "09", "10", "11", "12", "13", "14", "15", "All…
Jaap
  • 81,064
  • 34
  • 182
  • 193