Thanks to visit codestin.com
Credit goes to github.com

Skip to content

initial_rowno in expand_cat_json() is wrong if by=F, JSON empty, and na.rm = T #9

@berg-michael

Description

@berg-michael

Hi,

Not sure if this is a bug or intended behavior. The JSON in the second row of the input datatable is empty. If I expand the JSON with by = F and na.rm = T, the initial_rowno variable for rows 3 and 4 of the output is 2, when it should be 3. If I set na.rm = F, it becomes 3.

Obviously this issue can be avoided entirely by setting na.rm = F. Maybe it should be obvious to me why this behavior occurs, but it confused me so I thought I'd bring it up.

Thanks for all your work on this package, by the way!

patterns <- data.table::data.table(state_fips = c(1,2,3),
                                   cat_origin = c('{"a": "2", "b": "3"}',
                                                  '{}',
                                                  '{"a": "4", "b": "5"}'))
> patterns
   state_fips           cat_origin
1:          1 {"a": "2", "b": "3"}
2:          2                   {}
3:          3 {"a": "4", "b": "5"}
> 
expand_cat_json(
  patterns,
  'cat_origin',
  'index',
  by = F,
  na.rm = T
)
   initial_rowno cat_origin index
1:             1          2     a
2:             1          3     b
3:             2          4     a
4:             2          5     b
expand_cat_json(
  patterns,
  'cat_origin',
  'index',
  by = F,
  na.rm = F
)
   initial_rowno cat_origin index
1:             1          2     a
2:             1          3     b
3:             3          4     a
4:             3          5     b

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions