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

.I behavior in by #1544

Closed
maxmoro opened this issue Feb 23, 2016 · 5 comments
Closed

.I behavior in by #1544

maxmoro opened this issue Feb 23, 2016 · 5 comments

Comments

@maxmoro
Copy link

maxmoro commented Feb 23, 2016

This code

library(data.table)
t1=data.table(a=c(1,2,3),b=c(7,8,9))
t1[,':='(c=list(c(a,b))),by=t1[,.I]]
t1

Has the output I need. Basically column C is a list with vectors created from A and B within same Row

   a b   c
1: 1 7 1,7
2: 2 8 2,8
3: 3 9 3,9

But, if I write it in this way that is more clear to read:

library(data.table)
t1=data.table(a=c(1,2,3),b=c(7,8,9))
t1[,':='(c=list(c(a,b))),by=.I]
t1

It doesn't work:

   a b           c
1: 1 7 1,2,3,7,8,9
2: 2 8 1,2,3,7,8,9
3: 3 9 1,2,3,7,8,9

Why?
.I should help to group by Row number, but it is not.
Is this a bug or is intended to work in this way?

Thanks
Max

@franknarf1
Copy link
Contributor

In github posts, you can write blocks of codes (over multiple lines) by indenting each line four spaces. No need for backticks when doing that.

Currently, .I does not work in by; maybe it should throw an error there? I usually do by=seq(nrow(DT)), though it's not ideal.

@maxmoro
Copy link
Author

maxmoro commented Feb 23, 2016

Thank you for the block code tip :)
No error is returned, it just works like there is no by
I like the suggestion you are offering, I'll update my code, so is more readable.

Anyway, it would be great to have the by=.I implemented!
Thanks
Max

@arunsrinivasan
Copy link
Member

Thanks for following up, @franknarf1

@MichaelChirico
Copy link
Member

@franknarf1 I find whole section blocking easier on GitHub (precede and follow code blocks with a line of three backticks:

```
#some code without extra indentation
```

@jangorecki
Copy link
Member

jangorecki commented May 19, 2019

Looks like a duplicate of #1004 please re-open if not

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants