Comments (2)
aha! thank you for a very helpful response. i misunderstood that valuesFillMap
applies pre-pivot.
valuesFill
worked fine until i realized i need to set defaults for keys that are not always in the data set initially.
replaceNully()
is exactly what i'm looking for.
from tidy.
Hi there, glad to hear tidy has been helpful for you. The map for valuesFillMap should use the column names pre-pivot – in your case n
(they match what is passed to the valuesFrom argument).
tidy(
[
{ n: 2, c: 'a', e: 1001 },
{ n: 3, c: 'a', e: 1002 },
{ n: 7, c: 'x', e: 1003 },
{ n: 4, c: 'b', e: 1001 },
{ n: 2, c: 'r', e: 1002 },
{ n: 9, c: 'y', e: 1003 },
{ n: 6, c: 'c', e: 1001 },
{ n: 1, c: 'z', e: 1002 },
{ n: 1, c: 'z', e: 1003 },
],
pivotWider({
namesFrom: 'c',
valuesFrom: 'n',
valuesFillMap: { n: 0 },
})
);
[
{ a: 2, x: 0, b: 4, r: 0, y: 0, c: 6, z: 0, e: 1001 },
{ a: 3, x: 0, b: 0, r: 2, y: 0, c: 0, z: 1, e: 1002 },
{ a: 0, x: 7, b: 0, r: 0, y: 9, c: 0, z: 1, e: 1003 }
]
Or more simply in this case, you could just use valuesFill: 0
tidy(
[
{ n: 2, c: 'a', e: 1001 },
{ n: 3, c: 'a', e: 1002 },
{ n: 7, c: 'x', e: 1003 },
{ n: 4, c: 'b', e: 1001 },
{ n: 2, c: 'r', e: 1002 },
{ n: 9, c: 'y', e: 1003 },
{ n: 6, c: 'c', e: 1001 },
{ n: 1, c: 'z', e: 1002 },
{ n: 1, c: 'z', e: 1003 },
],
pivotWider({
namesFrom: 'c',
valuesFrom: 'n',
valuesFill: 0,
})
);
Notably, there's no 's' or 't' in your input data, so there's no 's' or 't' in the output. If you want to explicitly make sure those also get set to zero, you might have better luck using replaceNully explicitly:
tidy(
[
{ n: 2, c: 'a', e: 1001 },
{ n: 3, c: 'a', e: 1002 },
{ n: 7, c: 'x', e: 1003 },
{ n: 4, c: 'b', e: 1001 },
{ n: 2, c: 'r', e: 1002 },
{ n: 9, c: 'y', e: 1003 },
{ n: 6, c: 'c', e: 1001 },
{ n: 1, c: 'z', e: 1002 },
{ n: 1, c: 'z', e: 1003 },
],
pivotWider({
namesFrom: 'c',
valuesFrom: 'n',
valuesFill: 0,
}),
replaceNully({ s: 0, t: 0 }),
);
[
{ a: 2, x: 0, b: 4, r: 0, y: 0, c: 6, z: 0, e: 1001, s: 0, t: 0 },
{ a: 3, x: 0, b: 0, r: 2, y: 0, c: 0, z: 1, e: 1002, s: 0, t: 0 },
{ a: 0, x: 7, b: 0, r: 0, y: 9, c: 0, z: 1, e: 1003, s: 0, t: 0 }
]
Or if you want to use replaceNully on all fields explicitly, you could avoid valuesFill:
tidy(
[
{ n: 2, c: 'a', e: 1001 },
{ n: 3, c: 'a', e: 1002 },
{ n: 7, c: 'x', e: 1003 },
{ n: 4, c: 'b', e: 1001 },
{ n: 2, c: 'r', e: 1002 },
{ n: 9, c: 'y', e: 1003 },
{ n: 6, c: 'c', e: 1001 },
{ n: 1, c: 'z', e: 1002 },
{ n: 1, c: 'z', e: 1003 },
],
pivotWider({
namesFrom: 'c',
valuesFrom: 'n',
}),
replaceNully({ a: 0, b: 0, c: 0, r: 0, s: 0, t: 0, x: 0, y: 0, z: 0 }),
);
[
{ a: 2, x: 0, b: 4, r: 0, y: 0, c: 6, z: 0, e: 1001, s: 0, t: 0 },
{ a: 3, x: 0, b: 0, r: 2, y: 0, c: 0, z: 1, e: 1002, s: 0, t: 0 },
{ a: 0, x: 7, b: 0, r: 0, y: 9, c: 0, z: 1, e: 1003, s: 0, t: 0 }
]
from tidy.
Related Issues (20)
- Bug: groupBy exports do not respect addGroupKeys
- Bug: mapEntry not called on entriesObject
- Feedback on unexpected behavior in groupBy HOT 9
- Should groupBy's addGroupKeys be on by default?
- Allow type guards for filter HOT 2
- leftJoin followed by select everything has unexpected dropped columns
- Typescript type error? HOT 1
- Mutate should pass the index
- GroupBy fails with Dates HOT 1
- arrange should accept accessors
- Update docs to reflect keys being prepended now HOT 1
- Feature Request: add a boolean flag to turn on/off debug HOT 1
- New Architecture
- Type error when using groupBy after mutate HOT 2
- New summarizer: nWhere? HOT 3
- Set colum index for mutations HOT 7
- complete, is not parsing the original data HOT 1
- import fail HOT 5
- nested objects? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tidy.