Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zip transform does not allow no key on with #27

Closed
timelyportfolio opened this issue Apr 9, 2013 · 3 comments
Closed

zip transform does not allow no key on with #27

timelyportfolio opened this issue Apr 9, 2013 · 3 comments

Comments

@timelyportfolio
Copy link
Contributor

As far as I can tell, there is not a transform available that allows copying from something like a total stats transform into all the values of the dataset. For instance on the barley.json example, if we calculate stats on an aggregate level into "totalstats" through

    {
       "name" : "totalstats",
       "source" : "barley",
       "transform" : [
           {"type" : "stats", "value" : "data.yield"},
           {"type" : "zip", "with" : "barley"}
       ]
    }

and then we would like to use a formula transform using the totalstats fields such as mean on the data faceted by site, then there is no way to access this data. I thought I could use

        {"type": "zip", "with":"totalstats"},    

but that only zips with the Waseca or key 0. Is there a way to accomplish this other than a potentially modified zip transform? I'm sure I am introducing a new bug, but maybe something like this:

  function zip(data, db) {
    var zdata = db[z], d, i, len, map;

    if (withKey) {
      map = {};
      zdata.forEach(function(s) { map[withKey(s)] = s; });
    }

    for (i=0, len=data.length; i<len; ++i) {
      d = data[i];
      //if there is no key on zdata and length is 0 we know there is no real match
     //can we assume then that user wants every value in the target to receive the zip object
      if(key(zdata) & zdata.length > 0) {
          d[as] = map ? map[key(d)] : zdata[i];
      } else {
          d[as] = zdata[0];
      }
    }

    return data;
  }

Thanks.

@jheer
Copy link
Member

jheer commented Apr 9, 2013

If keys are provided both the source key and target withKey must be properly defined. By default the data object is used as the key for the source data. This default is helpful if your input data is simple strings or numbers rather than objects.

If no withKey is provided, then zip simply combines elements based on their position in their respective arrays. Source item 0 is zipped with target item 0, and so on, in a one-to-one fashion.

In the use case above, do you want to map a single global stats object with every element in the barley data? If so, I believe the following modification to zip should do the trick:

var zlen = zdata.length;
for (i=0, len=data.length; i<len; ++i) {
  d = data[i];
  d[as] = map ? map[key(d)] : zdata[i % zlen];
}

In the case that the target data is shorter than the source data, the target data indices simply cycle. For stats data with only one entry, zlen is 1 and so the modulo operation (i % zlen) will always return 0. For better or worse, these semantics are more similar to how R handles arrays... Does the above address your need?

@jheer
Copy link
Member

jheer commented Apr 10, 2013

As of commit 2d5f025, "zipping" two data sets by index now cycles the indices if the secondary data set is shorter than the primary data set.

@jheer jheer closed this as completed Apr 10, 2013
@timelyportfolio
Copy link
Contributor Author

Very nice, thanks. Your solution is far more elegant than my proposal (no surprise there).

jheer added a commit that referenced this issue Dec 8, 2018
Fix the Relay Transform visit order
jheer added a commit that referenced this issue Dec 14, 2018
Fix the Relay Transform visit order
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants