R data.table create list column by group
I have a data.table
below, and I would like to apply a function to column v2
group by v1
and order
, the resulting column v3
should be a list of vectors below.
How do I write this function such that it will return a vector of 0s for each group's first row (order == 1 & v1 %in% c(1, 2))
. For each subsequent row in the group, the vector will append previous row's v2
value to the right of the vector while bumping off one 0
from the left.
Initial data.table
t3 <- data.table(
v1 = rep(1:2, each = 5),
order = rep(1:5, 2),
v2 = c(6, 9, 6, 8, 6, 2, 5, 7, 8, 2)
)
v1 order v2
1: 1 1 6
2: 1 2 9
3: 1 3 6
4: 1 4 8
5: 1 5 6
6: 2 1 2
7: 2 2 5
8: 2 3 7
9: 2 4 8
10: 2 5 2
applying the function...
output:
t3[, v3 := list(c(0, 0, 0, 0, 0),
c(0, 0, 0, 0, 6),
c(0, 0, 0, 6, 9),
c(0, 0, 6, 9, 6),
c(0, 6, 9, 6, 8),
c(0, 0, 0, 0, 0),
c(0, 0, 0, 0, 2),
c(0, 0, 0, 2, 5),
c(0, 0, 2, 5, 7),
c(0, 2, 5, 7, 8))]
v1 order v2 v3
1: 1 1 6 0,0,0,0,0
2: 1 2 9 0,0,0,0,6
3: 1 3 6 0,0,0,6,9
4: 1 4 8 0,0,6,9,6
5: 1 5 6 0,6,9,6,8
6: 2 1 2 0,0,0,0,0
7: 2 2 5 0,0,0,0,2
8: 2 3 7 0,0,0,2,5
9: 2 4 8 0,0,2,5,7
10: 2 5 2 0,2,5,7,8
r data.table
add a comment |
I have a data.table
below, and I would like to apply a function to column v2
group by v1
and order
, the resulting column v3
should be a list of vectors below.
How do I write this function such that it will return a vector of 0s for each group's first row (order == 1 & v1 %in% c(1, 2))
. For each subsequent row in the group, the vector will append previous row's v2
value to the right of the vector while bumping off one 0
from the left.
Initial data.table
t3 <- data.table(
v1 = rep(1:2, each = 5),
order = rep(1:5, 2),
v2 = c(6, 9, 6, 8, 6, 2, 5, 7, 8, 2)
)
v1 order v2
1: 1 1 6
2: 1 2 9
3: 1 3 6
4: 1 4 8
5: 1 5 6
6: 2 1 2
7: 2 2 5
8: 2 3 7
9: 2 4 8
10: 2 5 2
applying the function...
output:
t3[, v3 := list(c(0, 0, 0, 0, 0),
c(0, 0, 0, 0, 6),
c(0, 0, 0, 6, 9),
c(0, 0, 6, 9, 6),
c(0, 6, 9, 6, 8),
c(0, 0, 0, 0, 0),
c(0, 0, 0, 0, 2),
c(0, 0, 0, 2, 5),
c(0, 0, 2, 5, 7),
c(0, 2, 5, 7, 8))]
v1 order v2 v3
1: 1 1 6 0,0,0,0,0
2: 1 2 9 0,0,0,0,6
3: 1 3 6 0,0,0,6,9
4: 1 4 8 0,0,6,9,6
5: 1 5 6 0,6,9,6,8
6: 2 1 2 0,0,0,0,0
7: 2 2 5 0,0,0,0,2
8: 2 3 7 0,0,0,2,5
9: 2 4 8 0,0,2,5,7
10: 2 5 2 0,2,5,7,8
r data.table
add a comment |
I have a data.table
below, and I would like to apply a function to column v2
group by v1
and order
, the resulting column v3
should be a list of vectors below.
How do I write this function such that it will return a vector of 0s for each group's first row (order == 1 & v1 %in% c(1, 2))
. For each subsequent row in the group, the vector will append previous row's v2
value to the right of the vector while bumping off one 0
from the left.
Initial data.table
t3 <- data.table(
v1 = rep(1:2, each = 5),
order = rep(1:5, 2),
v2 = c(6, 9, 6, 8, 6, 2, 5, 7, 8, 2)
)
v1 order v2
1: 1 1 6
2: 1 2 9
3: 1 3 6
4: 1 4 8
5: 1 5 6
6: 2 1 2
7: 2 2 5
8: 2 3 7
9: 2 4 8
10: 2 5 2
applying the function...
output:
t3[, v3 := list(c(0, 0, 0, 0, 0),
c(0, 0, 0, 0, 6),
c(0, 0, 0, 6, 9),
c(0, 0, 6, 9, 6),
c(0, 6, 9, 6, 8),
c(0, 0, 0, 0, 0),
c(0, 0, 0, 0, 2),
c(0, 0, 0, 2, 5),
c(0, 0, 2, 5, 7),
c(0, 2, 5, 7, 8))]
v1 order v2 v3
1: 1 1 6 0,0,0,0,0
2: 1 2 9 0,0,0,0,6
3: 1 3 6 0,0,0,6,9
4: 1 4 8 0,0,6,9,6
5: 1 5 6 0,6,9,6,8
6: 2 1 2 0,0,0,0,0
7: 2 2 5 0,0,0,0,2
8: 2 3 7 0,0,0,2,5
9: 2 4 8 0,0,2,5,7
10: 2 5 2 0,2,5,7,8
r data.table
I have a data.table
below, and I would like to apply a function to column v2
group by v1
and order
, the resulting column v3
should be a list of vectors below.
How do I write this function such that it will return a vector of 0s for each group's first row (order == 1 & v1 %in% c(1, 2))
. For each subsequent row in the group, the vector will append previous row's v2
value to the right of the vector while bumping off one 0
from the left.
Initial data.table
t3 <- data.table(
v1 = rep(1:2, each = 5),
order = rep(1:5, 2),
v2 = c(6, 9, 6, 8, 6, 2, 5, 7, 8, 2)
)
v1 order v2
1: 1 1 6
2: 1 2 9
3: 1 3 6
4: 1 4 8
5: 1 5 6
6: 2 1 2
7: 2 2 5
8: 2 3 7
9: 2 4 8
10: 2 5 2
applying the function...
output:
t3[, v3 := list(c(0, 0, 0, 0, 0),
c(0, 0, 0, 0, 6),
c(0, 0, 0, 6, 9),
c(0, 0, 6, 9, 6),
c(0, 6, 9, 6, 8),
c(0, 0, 0, 0, 0),
c(0, 0, 0, 0, 2),
c(0, 0, 0, 2, 5),
c(0, 0, 2, 5, 7),
c(0, 2, 5, 7, 8))]
v1 order v2 v3
1: 1 1 6 0,0,0,0,0
2: 1 2 9 0,0,0,0,6
3: 1 3 6 0,0,0,6,9
4: 1 4 8 0,0,6,9,6
5: 1 5 6 0,6,9,6,8
6: 2 1 2 0,0,0,0,0
7: 2 2 5 0,0,0,0,2
8: 2 3 7 0,0,0,2,5
9: 2 4 8 0,0,2,5,7
10: 2 5 2 0,2,5,7,8
r data.table
r data.table
asked Nov 21 '18 at 15:55
EKtheSageEKtheSage
796
796
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
We could try
t3[order(order), .(order, v2, shift(v2, 5:1, fill = 0)), by = v1]
Output:
v1 order v2 V3
1: 1 1 6 0,0,0,0,0
2: 1 2 9 0,0,0,0,6
3: 1 3 6 0,0,0,6,9
4: 1 4 8 0,0,6,9,6
5: 1 5 6 0,6,9,6,8
6: 2 1 2 0,0,0,0,0
7: 2 2 5 0,0,0,0,2
8: 2 3 7 0,0,0,2,5
9: 2 4 8 0,0,2,5,7
10: 2 5 2 0,2,5,7,8
This is excellent answer! I didn't know you could create a list usingshift
. Thank you, thank you, thank you!!
– EKtheSage
Nov 22 '18 at 1:14
You are welcome. Matt & Company have done an excellent job, ´data.table´ is awesome!
– J.R.
Nov 22 '18 at 9:56
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415886%2fr-data-table-create-list-column-by-group%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
We could try
t3[order(order), .(order, v2, shift(v2, 5:1, fill = 0)), by = v1]
Output:
v1 order v2 V3
1: 1 1 6 0,0,0,0,0
2: 1 2 9 0,0,0,0,6
3: 1 3 6 0,0,0,6,9
4: 1 4 8 0,0,6,9,6
5: 1 5 6 0,6,9,6,8
6: 2 1 2 0,0,0,0,0
7: 2 2 5 0,0,0,0,2
8: 2 3 7 0,0,0,2,5
9: 2 4 8 0,0,2,5,7
10: 2 5 2 0,2,5,7,8
This is excellent answer! I didn't know you could create a list usingshift
. Thank you, thank you, thank you!!
– EKtheSage
Nov 22 '18 at 1:14
You are welcome. Matt & Company have done an excellent job, ´data.table´ is awesome!
– J.R.
Nov 22 '18 at 9:56
add a comment |
We could try
t3[order(order), .(order, v2, shift(v2, 5:1, fill = 0)), by = v1]
Output:
v1 order v2 V3
1: 1 1 6 0,0,0,0,0
2: 1 2 9 0,0,0,0,6
3: 1 3 6 0,0,0,6,9
4: 1 4 8 0,0,6,9,6
5: 1 5 6 0,6,9,6,8
6: 2 1 2 0,0,0,0,0
7: 2 2 5 0,0,0,0,2
8: 2 3 7 0,0,0,2,5
9: 2 4 8 0,0,2,5,7
10: 2 5 2 0,2,5,7,8
This is excellent answer! I didn't know you could create a list usingshift
. Thank you, thank you, thank you!!
– EKtheSage
Nov 22 '18 at 1:14
You are welcome. Matt & Company have done an excellent job, ´data.table´ is awesome!
– J.R.
Nov 22 '18 at 9:56
add a comment |
We could try
t3[order(order), .(order, v2, shift(v2, 5:1, fill = 0)), by = v1]
Output:
v1 order v2 V3
1: 1 1 6 0,0,0,0,0
2: 1 2 9 0,0,0,0,6
3: 1 3 6 0,0,0,6,9
4: 1 4 8 0,0,6,9,6
5: 1 5 6 0,6,9,6,8
6: 2 1 2 0,0,0,0,0
7: 2 2 5 0,0,0,0,2
8: 2 3 7 0,0,0,2,5
9: 2 4 8 0,0,2,5,7
10: 2 5 2 0,2,5,7,8
We could try
t3[order(order), .(order, v2, shift(v2, 5:1, fill = 0)), by = v1]
Output:
v1 order v2 V3
1: 1 1 6 0,0,0,0,0
2: 1 2 9 0,0,0,0,6
3: 1 3 6 0,0,0,6,9
4: 1 4 8 0,0,6,9,6
5: 1 5 6 0,6,9,6,8
6: 2 1 2 0,0,0,0,0
7: 2 2 5 0,0,0,0,2
8: 2 3 7 0,0,0,2,5
9: 2 4 8 0,0,2,5,7
10: 2 5 2 0,2,5,7,8
answered Nov 21 '18 at 16:59
J.R.J.R.
3,29811219
3,29811219
This is excellent answer! I didn't know you could create a list usingshift
. Thank you, thank you, thank you!!
– EKtheSage
Nov 22 '18 at 1:14
You are welcome. Matt & Company have done an excellent job, ´data.table´ is awesome!
– J.R.
Nov 22 '18 at 9:56
add a comment |
This is excellent answer! I didn't know you could create a list usingshift
. Thank you, thank you, thank you!!
– EKtheSage
Nov 22 '18 at 1:14
You are welcome. Matt & Company have done an excellent job, ´data.table´ is awesome!
– J.R.
Nov 22 '18 at 9:56
This is excellent answer! I didn't know you could create a list using
shift
. Thank you, thank you, thank you!!– EKtheSage
Nov 22 '18 at 1:14
This is excellent answer! I didn't know you could create a list using
shift
. Thank you, thank you, thank you!!– EKtheSage
Nov 22 '18 at 1:14
You are welcome. Matt & Company have done an excellent job, ´data.table´ is awesome!
– J.R.
Nov 22 '18 at 9:56
You are welcome. Matt & Company have done an excellent job, ´data.table´ is awesome!
– J.R.
Nov 22 '18 at 9:56
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415886%2fr-data-table-create-list-column-by-group%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown