... should exclude anything mentioned in value.var

Open
#1,833 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
38/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
r
Domain
data

Research direction

Start by reproducing the issue's melt and dcast examples, focusing on how dcast interprets ... and value.var. Trace the dcast entry point and its handling of formula variables. Done means dcast(DT_m, ... ~ ., value.var = "X", fun.aggregate = sum) excludes X from the grouping columns and produces the intended aggregated wide result.

Written by the indexing model from the issue text.

Description

reshape

Consider:

set.seed(120)
DT <- data.table(V1 = sample(10))
DT[ , paste0("V", 2:20) := replicate(19, sample(10), simplify = FALSE)]
DT[ , paste0("X", 1:50) := replicate(50, rnorm(10), simplify = FALSE)]

DT_m <- melt(DT, measure.vars = patterns("^X"), value.name = "X")

I want to reshape DT_m back to being "wide". I want V1:20 to be as originally in DT, but to aggregate all of X1:50 by summing into a single column.

It seems the way to do this is:

dcast(DT_m, V1 + V2 + V3 + [...] + V20 ~ ., fun.aggregate = sum)
#    V1 V2          .
#1   1  5   5.452721
#2   2  9   2.855705
#3   3  2  -1.775939
#4   4  6   7.033915
#5   5  1 -10.456389
#6   6  8   9.050576
#7   7  4   4.917982
#8   8  7   1.901975
#9   9 10   3.969899
#10 10  3  -1.367950

But obviously it's not optimal to need to type all the variables on the LHS. I thought that was the point of ..., but this doesn't work:

dcast(DT_m, ... ~ ., value.var = "X", fun.aggregate = sum)

... has included X, so the returned table is just DT_m (with X renamed to .).

Dominant language
R
Stars
3.9k
Forks
1.1k
Avg merge
14h 4m
Merged PRs (30d)
4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Rdatatable/data.table

All issues in Rdatatable/data.table

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.