# Reshape2

> R reshape2 package for data reshaping. Use for melting and casting data between wide and long formats.

- Skill: `leolin990405/reshape2` (Agent Skill)
- Install (CLI): `npx get-skill add leolin990405/reshape2`
- Raw SKILL.md: https://api.skillmd.com/api/skills/leolin990405/reshape2/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: LeoLin990405 (https://skillmd.com/u/leolin990405)
- Updated: 2026-11-14
- Page: https://skillmd.com/skills/leolin990405/reshape2

---


# reshape2

Flexibly reshape data.

## Melt (Wide to Long)

```r
library(reshape2)

# Basic melt
long_df <- melt(wide_df)

# Specify id variables
long_df <- melt(wide_df, id.vars = c("id", "name"))

# Specify measure variables
long_df <- melt(wide_df,
  id.vars = "id",
  measure.vars = c("var1", "var2", "var3")
)

# Custom column names
long_df <- melt(wide_df,
  id.vars = "id",
  variable.name = "metric",
  value.name = "score"
)
```

## Cast (Long to Wide)

```r
# dcast for data frames
wide_df <- dcast(long_df, id ~ variable)

# With aggregation
wide_df <- dcast(long_df, id ~ variable, fun.aggregate = mean)
wide_df <- dcast(long_df, id ~ variable, fun.aggregate = sum)

# Multiple id variables
wide_df <- dcast(long_df, id + name ~ variable)

# acast for arrays/matrices
arr <- acast(long_df, id ~ variable)
```

## Formula Syntax

```r
# row_var ~ col_var
dcast(df, id ~ variable)

# Multiple row variables
dcast(df, id + group ~ variable)

# Multiple column variables
dcast(df, id ~ var1 + var2)

# All variables on one side
dcast(df, ... ~ variable)  # All others as rows
dcast(df, id ~ ...)        # All others as columns
```

## Handling Missing Values

```r
# Fill missing with value
wide_df <- dcast(long_df, id ~ variable, fill = 0)
wide_df <- dcast(long_df, id ~ variable, fill = NA)

# Drop missing
wide_df <- dcast(long_df, id ~ variable, drop = TRUE)
```

## Multiple Value Columns

```r
# Melt multiple measure columns
long_df <- melt(wide_df,
  id.vars = "id",
  measure.vars = list(
    c("a1", "a2", "a3"),
    c("b1", "b2", "b3")
  ),
  variable.name = "time",
  value.name = c("metric_a", "metric_b")
)
```

## Comparison with tidyr

```r
# reshape2::melt = tidyr::pivot_longer
# reshape2::dcast = tidyr::pivot_wider

# reshape2 is faster for large datasets
# tidyr has cleaner syntax
# Both achieve same results
```

