Questions tagged [reshape2]

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

reshape2
Filter by
Sorted by
Tagged with
1 vote
1 answer
202 views

Reshaping block in data.frame to lists is columns uneven length

I would need your help with R to cast this data: STATUS SCORE JOB.ID 1 STATUS1 99 JOB1 2 STATUS1 99 JOB2 3 STATUS1 99 JOB3 4 STATUS1 99 JOB4 5 STATUS2 ...
Michael Robert's user avatar
6 votes
1 answer
4k views

How to use "cast" in reshape without aggregation

In many uses of cast I've seen, an aggregation function such as mean is used. How about if you simply want to reshape without information loss. For example, if I want to take this long format: ID ...
aleph4's user avatar
  • 708
2 votes
1 answer
276 views

reshape alternating columns in less time and using less memory

How can I do this reshape faster and so that it takes up less memory? My aim is to reshape a dataframe that is 500,000 rows by 500 columns with 4 Gb RAM. Here's a function that will make some ...
Ben's user avatar
  • 42.1k
0 votes
1 answer
104 views

how would I get the below answer using melt/cast from reshape2 package

I have two data frames, x and y. I rbind them to get z. Then I use reshape function (not package) to get the below answer. set.seed(1234) x <- data.frame(rp=c(1:5),dmg=1000*runif(5), loss=500*...
Farmer Bob's user avatar
32 votes
7 answers
177k views

Compute mean and standard deviation by group for multiple variables in a data.frame

Edit -- This question was originally titled << Long to wide data reshaping in R >> I'm just learning R and trying to find ways to apply it to help out others in my life. As a test case, I'm ...
user2348358's user avatar
12 votes
9 answers
40k views

split characters into two variables in data frame

Let's say I have a vector of variables like this: >variable [1] "A1" "A1" "A1" "A1" "A2" "A2" "A2" "A2" "B1" "B1" "B1" "B1" and I want to covert this into into a data frame like this: ...
Alby's user avatar
  • 5,602
0 votes
3 answers
403 views

Transforming a min() and a max() value into a range of values

I would like stretch some of my records that were flattened I have a table like this Store Min(Date) Max (Date) Status NYC1 1/1/2013 2/1/2013 Open NYC1 2/2/2013 ...
Green Demon's user avatar
  • 4,148
3 votes
2 answers
1k views

Convert long data frame into wide data frame

I am trying to convert long data form into wide data form in R. For instance, I have following data frame: a = rep(c("A","B","C","D"),4) b = rep(c("COL1","COL2","COL3","COL4"),4) val = 101:116 df = ...
M.Qasim's user avatar
  • 1,857
5 votes
1 answer
11k views

casting error - Error in .subset2(x, i, exact = exact)

It feels like I'm missing something obvious here, so apologies in advance. Anyways, here's some data a that I'm trying to cast: acct_num year_prem prem exc 001 2012 ...
alexwhitworth's user avatar
0 votes
1 answer
101 views

custom variable names in R reshape2

Is there a way to change the default way that the dcast function names variables? For example require(reshape2) x = data.frame(id=1:2, t=1:5, v=10:1) m = melt(x, id.vars = c("id", "t")) cx = dcast(m, ...
Alex's user avatar
  • 19.8k
5 votes
1 answer
6k views

Reversing a melting operation with reshape2 [duplicate]

Consider the following code. library (reshape2) x = rnorm (20) y = x + rnorm (rnorm (20, sd = .01)) dfr <- data.frame (x, y) mlt <- melt (dfr) When I try to reverse this operation with dcast, ...
Gil Tomás's user avatar
0 votes
3 answers
125 views

R reshape to comparable long

I have a matrix x A B C D 1 11 12 13 14 2 21 22 23 24 3 31 32 33 34 4 41 42 43 44 5 51 52 53 54 and two vetors [,1] [,2] [1,] "A" "B" [2,] "A" "C" [3,] "A" "D" [4,] "B" "C" [5,] "...
mffap's user avatar
  • 491
6 votes
1 answer
1k views

Simpler way to reconstitute a melted data frame back to the original

How do I recreate a data.frame that I melted with reshape2? Reproducible example library(reshape2) library(plyr) data(iris) df <- melt(iris, id.vars="Species") head(df) Species variable ...
Maiasaura's user avatar
  • 32.6k
1 vote
1 answer
996 views

How can I create a summary table from lists of matrices?

Data: k1 <- structure(list(`3280.2000` = structure(c(0, 1.363, 0, 4.198, 1.097, 1.669, 0, 1.098, 0), .Dim = c(3L, 3L), .Dimnames = list( c("3280.3500", "3280.400", "3280.4000"), c("3280.3500"...
Jack Ryan's user avatar
  • 2,134
1 vote
1 answer
216 views

Transposing a data frame [duplicate]

I have a question about re-shaping (if that's the right word) a data frame to a transposed version of it. I want to take something like: A B C 1 6 1 1 18 1 1 21 1 3 18 1 3 21 1 ...
AI52487963's user avatar
  • 1,273
17 votes
4 answers
3k views

Reshape wide format, to multi-column long format

I want to reshape a wide format dataset that has multiple tests which are measured at 3 time points: ID Test Year Fall Spring Winter 1 1 2008 15 16 19 1 1 2009 ...
Sam's user avatar
  • 381
3 votes
2 answers
407 views

melt with chron

I'm trying to melt a data frame with chron class library(chron) x = data.frame(Index = as.chron(c(15657.00,15657.17)), Var1 = c(1,2), Var2 = c(9,8)) x Index Var1 Var2 1 (11/13/12 00:...
fRed's user avatar
  • 75
2 votes
2 answers
434 views

R - Dramatic increase in file size after reshaping wide to long data [closed]

I have a medium size database (~400,000 rows, 27 columns) that I need to search across most of the columns (25 of them) with the same criteria for comparison. I figured it would be more efficient to ...
user avatar
0 votes
1 answer
257 views

which.min within reshape2's dcast()?

I would like to extract the value of var2 that corresponds to the minimum value of var1 in each building-month combination. Here's my (fake) data set: head(mydata) # building month var1 ...
baha-kev's user avatar
  • 3,059
2 votes
2 answers
618 views

ddply - spaces in variable name contained in an object

Is there a way to pass both named variables and an object containing a variable name with spaces into ddply? Here is an example of what I mean: > library(plyr) > dat <- data.frame(id1=rep(c("...
David's user avatar
  • 9,325
11 votes
3 answers
2k views

segfault in R using reshape2 package and dcast

RStudio was crashing when I tried to reshape a particular data frame using dcast (from the reshape2 package). I discovered that the crash was actually happening in R itself, so I ran my casting code ...
eipi10's user avatar
  • 92.8k
19 votes
8 answers
949 views

Complicated reshaping

I want to reshape my dataframe from long to wide format and I loose some data that I'd like to keep. For the following example: df <- data.frame(Par1 = unlist(strsplit("AABBCCC","")), ...
Vasily A's user avatar
  • 8,546
0 votes
1 answer
134 views

Reshaping a data.frame

I have the following table structure(list(Compound = structure(c(1L, 2L, 3L, 1L, 1L, 3L), .Label = c("Nap", "Phe", "tre"), class = "factor"), Area = c(...
user avatar
1 vote
1 answer
524 views

How do I construct "quoted expressions" for use in reshape2's dcast?

The documentation for reshape2 gives the following detail regarding the subset attribute to dcast: subset quoted expression used to subset data prior to reshaping, e.g. subset = .(variable=="...
saffsd's user avatar
  • 24k
7 votes
3 answers
235 views

Reorganizing data from 3 rows to 1

I need to reorganize data from a csv file that contains mostly repeating data. I have the data imported into R in a dataframe but I am having trouble with the following: ID Language Author ...
Ray's user avatar
  • 73
3 votes
3 answers
1k views

Rearrange a matrix in R using two factors

Here is the problem. There is a matrix with N rows and C columns, and two factors: ids and group, both of length N. For example: m <- matrix( 1:25, nrow= 5, byrow= T ) id <- factor( c( "A", "A",...
January's user avatar
  • 16.7k
2 votes
2 answers
1k views

How Do I Go About Transposing/Cutting Columns into Rows in R

I am working with a large dataset on country level data that is in country-year format and appears in the following form (abridged for ease): Country Variable of Interest Year Bolivia 5 ...
Bataman's user avatar
  • 23
9 votes
4 answers
818 views

Integer64 class doesn't survive reshape2 melt function

I don't know whether this is an integer64 (from bit64) problem, or a melt problem (from reshape2, but if I try to reshape a data.frame containing integer64 data then the class information is destroyed ...
Corvus's user avatar
  • 7,807
3 votes
1 answer
215 views

Aggregating multiple subtotals?

Is there a way to aggregate multiple sub-totals with reshape2? E.g. for the airquality dataset require(reshape2) require(plyr) names(airquality) <- tolower(names(airquality)) aqm <- melt(...
Zach's user avatar
  • 30.1k
5 votes
1 answer
8k views

Cast a data frame with a timevar in reshape2, as with the reshape base function?

Suppose I've got the following data frame : d <- data.frame(id=c(1,1,1,2,2,3,3,3), time=c(1,2,3,1,2,1,2,3), var=runif(8)) d id time var 1 1 1 0.3733586 2 1 2 0.5743769 3 1 3 ...
juba's user avatar
  • 48.5k
1 vote
1 answer
92 views

Reshape help needed [closed]

I am trying to reshape my data from this form in exhibit A to the form in exhibit B. I've tried reshape and looping over the data by each three columns and appending the datasets, but can't quite get ...
user1259823's user avatar
1 vote
2 answers
1k views

Use ddply() to aggregate relative histogram counts

Related to a previous question I asked (ggplot2 how to get 2 histograms with the y value = to count of one / sum of the count of both), I tried to write a function which would take a data.frame as ...
shora's user avatar
  • 141
0 votes
2 answers
84 views

reshaping data in R skipping certain measured variables

I would like to reshape a data.frame that looks like this: permno dte ttm var1 var2 var3 1 123 2012-01-01 20 1 10 100 2 123 2012-01-01 30 -1 10 100 3 124 2012-01-...
Alex's user avatar
  • 19.8k
4 votes
2 answers
833 views

Enumerate instances of a factor level

I have a data frame with 150000 lines in long format with multiple occurences of the same id variable. I'm using reshape (from stat, rather than package=reshape(2)) to convert this to wide format. I ...
Ewen's user avatar
  • 1,356
3 votes
2 answers
1k views

How to transpose/cast two variables to one line?

I need to turn this id | amount | day --------------------- A | 10 | 0 A | 54 | 8 A | 23 | 18 A | 43 | 28 A | 87 | 51 B | 34 | 0 B | 76 | 1 ...
jenswirf's user avatar
  • 7,177
6 votes
6 answers
771 views

"unpacking" a factor list from a data.frame

I'm new to R / having the option to easily re-organize data, and have hunted around for a solution but can't find exactly what I'd like to do. Reshape2's melt/cast doesn't quite seem to work and I ...
williaster's user avatar
0 votes
3 answers
216 views

paste values within categories defined by multiple columns

I want to pivot the result column in df horizontally creating a data set with a separate row for each region, state, county combination where the columns are ordered by year then city. I also want to ...
Mark Miller's user avatar
  • 12.8k
12 votes
2 answers
1k views

Generate graphs in R for certain correlations in a matrix

I want to generate graphs between variables (columns) that have a correlation above and below a certain point as well as having a pvalue < 0.01. The graphs would be ggplot2 (line or bar) graphs ...
themartinmcfly's user avatar
3 votes
1 answer
2k views

melt and merge several files into a single database using R

I have been trying to import several csv files, use the function "melt" and merge them into a single database in R. All the files have an "id", "date.time" and "tag" column; however, the rest of the ...
user1626688's user avatar
  • 1,623
8 votes
3 answers
2k views

Fast melted data.table operations

I am looking for patterns for manipulating data.table objects whose structure resembles that of dataframes created with melt from the reshape2 package. I am dealing with data tables with millions of ...
Sim's user avatar
  • 13.3k
5 votes
1 answer
325 views

High-performance big data manipulation in R

I am dealing with a collection of lists, which contain deeply nested lists with no fixed structure other than the fact that: The lists at level 1 have a single element called variations All leaf data ...
Sim's user avatar
  • 13.3k
0 votes
3 answers
168 views

Converting dataframe to partial "wide" and partial summary by group

I have the following data frame id datestamp hrofday val1 val2 val3 a 20120401 0 3.2 0 1 a 20120401 1 3.3 4 0 a 20120401 2 3.4 6 0 ... a 20120401 23 7.3 0 2 It represents a user-id followed by ...
broccoli's user avatar
  • 4,806
0 votes
1 answer
166 views

How to pass a premade list of functions to cast

cast, from the reshape2 package, can handle a bunch of functions if passed directly in the call. For example: library(reshape2) d <- data.frame(variable="variable",value=rnorm(100)) cast(d, ...
Brandon Bertelsen's user avatar
2 votes
1 answer
324 views

Finance - Random Portfolios using plyr and rportfolio - need to split columns

I am trying to compare an actual portfolio's performance to the performances of hypothetical random portfolios. Here is a sample of the data set I am working with. It shows two months worth of data, ...
jfreels's user avatar
  • 123
6 votes
1 answer
6k views

Reshaping a complex dataset from long to wide using recast()

I am working with a dataset that comes with lme4, and am trying to learn how to apply reshape2 to convert it from long to wide [full code at the end of the post]. library(lme4) data("VerbAgg") # ...
Twitch_City's user avatar
  • 1,246
3 votes
1 answer
484 views

reshape2: dcast tall into wide without aggregating

Consider ext <- data.frame(cond = rep(c('a', 'b'), each = 2), dat = runif(4) ) I want exw <- unstack(ext, dat ~ cond) But I would like to do it with dcast() in reshape2 (for pedagogical ...
mk9y's user avatar
  • 370
-1 votes
1 answer
716 views

How to get accumulated/cumulative row-wise sum with dcast in reshape2

Using dcast from the reshape2 library in R I aggregate data (sum) library('reshape2') DF <- data.frame(Cohort=rep("", 9), Weeks=rep("", 9), myvalue=rep(0, 9), stringsAsFactors=FALSE ) DF[1, ] <...
Cilvic's user avatar
  • 3,437
1 vote
1 answer
3k views

Cast not working on dataframe

I'm working with a large data frame and I'd like to perform a pivot table type of function on it. I've been trying to use the reshape2 package but for some reason my molten data frame is not reshaping....
jlatif's user avatar
  • 21
1 vote
2 answers
4k views

Reshape/melt a dataframe for scatter plot

I have a data file that looks like this: x ys ---------------------> 1 20 25 30 12 22 12 2 12 9 12 32 12 3 33 12 11 6 1 4 5 10 41 12 3 5 7 81 ...
user1728853's user avatar
  • 2,677
2 votes
2 answers
3k views

reshape function gives "subscript out of bounds" error

I have a ROC.Value data frame that looks like this: Years 1 2 3 4 5 2002 3.000000 NA 0.22 NA 0 2003 2.988000 NA 0.22 NA 0 2004 2.993976 NA 0....
bonna's user avatar
  • 1,575