R data.table list output vs vector output
d <- structure(list(Name = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L), .Label = c("Aira", "Ben", "Cat"), class = "factor"), Month = c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), Rate1 = c(12L, 18L, 19L, 53L, 22L, 19L, 22L, 67L, 45L), Rate2 = c(23L, 73L, 45L, 19L, 87L, 45L, 87L, 43L, 32L)), .Names = c("Name", "Month", "Rate1", "Rate2"), class = c("data.table", "data.frame"), row.names = c(NA, -9L))
> d
Name Month Rate1 Rate2
1: Aira 1 12 23
2: Aira 2 18 73
3: Aira 3 19 45
4: Ben 1 53 19
5: Ben 2 22 87
6: Ben 3 19 45
7: Cat 1 22 87
8: Cat 2 67 43
9: Cat 3 45 32
I have the above data.table and want to know the difference between the following two operations. I know .() is short for list and c() is a vector. What does data.table do with these two different syntax? All I can observe is that the first case resulted in two variables and the second case resulted in a single variable.
> d[, .(mean(Rate1), mean(Rate2)), by = Name]
Name V1 V2
1: Aira 16.33 47.00
2: Ben 31.33 50.33
3: Cat 44.67 54.00
and
> d[, c(mean(Rate1), mean(Rate2)), by = Name]
Name V1
1: Aira 16.33
2: Aira 47.00
3: Ben 31.33
4: Ben 50.33
5: Cat 44.67
6: Cat 54.00
r list data.table grouping aggregation
add a comment |
d <- structure(list(Name = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L), .Label = c("Aira", "Ben", "Cat"), class = "factor"), Month = c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), Rate1 = c(12L, 18L, 19L, 53L, 22L, 19L, 22L, 67L, 45L), Rate2 = c(23L, 73L, 45L, 19L, 87L, 45L, 87L, 43L, 32L)), .Names = c("Name", "Month", "Rate1", "Rate2"), class = c("data.table", "data.frame"), row.names = c(NA, -9L))
> d
Name Month Rate1 Rate2
1: Aira 1 12 23
2: Aira 2 18 73
3: Aira 3 19 45
4: Ben 1 53 19
5: Ben 2 22 87
6: Ben 3 19 45
7: Cat 1 22 87
8: Cat 2 67 43
9: Cat 3 45 32
I have the above data.table and want to know the difference between the following two operations. I know .() is short for list and c() is a vector. What does data.table do with these two different syntax? All I can observe is that the first case resulted in two variables and the second case resulted in a single variable.
> d[, .(mean(Rate1), mean(Rate2)), by = Name]
Name V1 V2
1: Aira 16.33 47.00
2: Ben 31.33 50.33
3: Cat 44.67 54.00
and
> d[, c(mean(Rate1), mean(Rate2)), by = Name]
Name V1
1: Aira 16.33
2: Aira 47.00
3: Ben 31.33
4: Ben 50.33
5: Cat 44.67
6: Cat 54.00
r list data.table grouping aggregation
I'm still learningdata.table, but I had never thought to usec(...). Interesting to see that a summarizing function would return more than one row per group, I can see that as very useful in some circumstances. I'm not certain where your question is, though: your code is good at demonstrating the difference: in the.(...)case you get one row per group. I suspect the equivalent of your second command isd[, .(c(mean(Rate1), mean(Rate2))), by = Name], which might clear up why it returns a single column, each group with two rows.
– r2evans
Nov 14 '18 at 22:40
2
Please have a look at the nice vignette Introduction todata.table: "As long asjreturns a list, each element of the list becomes a column in the resultingdata.table"; "When there’s only one column or expression to refer to inj[...], we can drop the.()notation. This is purely for convenience." In your case, thec()concatenates the two means to one column.
– Henrik
Nov 14 '18 at 22:42
1
@r2evans - I supposej+by=are not strictly a summarizing function, but rather a flexible grouping capability. You can even return more rows than are in the original data.
– thelatemail
Nov 15 '18 at 0:04
Yes, @thelatemail, I'm gathering that. This is one key difference betweendata.tablesummarizing anddplyr::summarize, where it fails if the returned summary is other than length 1. Indplyr-land, I would have summarized with ado(...)block so that I can return more than 1, it's nice to not have to do something similar here.
– r2evans
Nov 15 '18 at 0:06
add a comment |
d <- structure(list(Name = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L), .Label = c("Aira", "Ben", "Cat"), class = "factor"), Month = c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), Rate1 = c(12L, 18L, 19L, 53L, 22L, 19L, 22L, 67L, 45L), Rate2 = c(23L, 73L, 45L, 19L, 87L, 45L, 87L, 43L, 32L)), .Names = c("Name", "Month", "Rate1", "Rate2"), class = c("data.table", "data.frame"), row.names = c(NA, -9L))
> d
Name Month Rate1 Rate2
1: Aira 1 12 23
2: Aira 2 18 73
3: Aira 3 19 45
4: Ben 1 53 19
5: Ben 2 22 87
6: Ben 3 19 45
7: Cat 1 22 87
8: Cat 2 67 43
9: Cat 3 45 32
I have the above data.table and want to know the difference between the following two operations. I know .() is short for list and c() is a vector. What does data.table do with these two different syntax? All I can observe is that the first case resulted in two variables and the second case resulted in a single variable.
> d[, .(mean(Rate1), mean(Rate2)), by = Name]
Name V1 V2
1: Aira 16.33 47.00
2: Ben 31.33 50.33
3: Cat 44.67 54.00
and
> d[, c(mean(Rate1), mean(Rate2)), by = Name]
Name V1
1: Aira 16.33
2: Aira 47.00
3: Ben 31.33
4: Ben 50.33
5: Cat 44.67
6: Cat 54.00
r list data.table grouping aggregation
d <- structure(list(Name = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L), .Label = c("Aira", "Ben", "Cat"), class = "factor"), Month = c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), Rate1 = c(12L, 18L, 19L, 53L, 22L, 19L, 22L, 67L, 45L), Rate2 = c(23L, 73L, 45L, 19L, 87L, 45L, 87L, 43L, 32L)), .Names = c("Name", "Month", "Rate1", "Rate2"), class = c("data.table", "data.frame"), row.names = c(NA, -9L))
> d
Name Month Rate1 Rate2
1: Aira 1 12 23
2: Aira 2 18 73
3: Aira 3 19 45
4: Ben 1 53 19
5: Ben 2 22 87
6: Ben 3 19 45
7: Cat 1 22 87
8: Cat 2 67 43
9: Cat 3 45 32
I have the above data.table and want to know the difference between the following two operations. I know .() is short for list and c() is a vector. What does data.table do with these two different syntax? All I can observe is that the first case resulted in two variables and the second case resulted in a single variable.
> d[, .(mean(Rate1), mean(Rate2)), by = Name]
Name V1 V2
1: Aira 16.33 47.00
2: Ben 31.33 50.33
3: Cat 44.67 54.00
and
> d[, c(mean(Rate1), mean(Rate2)), by = Name]
Name V1
1: Aira 16.33
2: Aira 47.00
3: Ben 31.33
4: Ben 50.33
5: Cat 44.67
6: Cat 54.00
r list data.table grouping aggregation
r list data.table grouping aggregation
edited Nov 14 '18 at 22:45
Henrik
42.3k994110
42.3k994110
asked Nov 14 '18 at 22:12
rosepark222rosepark222
1916
1916
I'm still learningdata.table, but I had never thought to usec(...). Interesting to see that a summarizing function would return more than one row per group, I can see that as very useful in some circumstances. I'm not certain where your question is, though: your code is good at demonstrating the difference: in the.(...)case you get one row per group. I suspect the equivalent of your second command isd[, .(c(mean(Rate1), mean(Rate2))), by = Name], which might clear up why it returns a single column, each group with two rows.
– r2evans
Nov 14 '18 at 22:40
2
Please have a look at the nice vignette Introduction todata.table: "As long asjreturns a list, each element of the list becomes a column in the resultingdata.table"; "When there’s only one column or expression to refer to inj[...], we can drop the.()notation. This is purely for convenience." In your case, thec()concatenates the two means to one column.
– Henrik
Nov 14 '18 at 22:42
1
@r2evans - I supposej+by=are not strictly a summarizing function, but rather a flexible grouping capability. You can even return more rows than are in the original data.
– thelatemail
Nov 15 '18 at 0:04
Yes, @thelatemail, I'm gathering that. This is one key difference betweendata.tablesummarizing anddplyr::summarize, where it fails if the returned summary is other than length 1. Indplyr-land, I would have summarized with ado(...)block so that I can return more than 1, it's nice to not have to do something similar here.
– r2evans
Nov 15 '18 at 0:06
add a comment |
I'm still learningdata.table, but I had never thought to usec(...). Interesting to see that a summarizing function would return more than one row per group, I can see that as very useful in some circumstances. I'm not certain where your question is, though: your code is good at demonstrating the difference: in the.(...)case you get one row per group. I suspect the equivalent of your second command isd[, .(c(mean(Rate1), mean(Rate2))), by = Name], which might clear up why it returns a single column, each group with two rows.
– r2evans
Nov 14 '18 at 22:40
2
Please have a look at the nice vignette Introduction todata.table: "As long asjreturns a list, each element of the list becomes a column in the resultingdata.table"; "When there’s only one column or expression to refer to inj[...], we can drop the.()notation. This is purely for convenience." In your case, thec()concatenates the two means to one column.
– Henrik
Nov 14 '18 at 22:42
1
@r2evans - I supposej+by=are not strictly a summarizing function, but rather a flexible grouping capability. You can even return more rows than are in the original data.
– thelatemail
Nov 15 '18 at 0:04
Yes, @thelatemail, I'm gathering that. This is one key difference betweendata.tablesummarizing anddplyr::summarize, where it fails if the returned summary is other than length 1. Indplyr-land, I would have summarized with ado(...)block so that I can return more than 1, it's nice to not have to do something similar here.
– r2evans
Nov 15 '18 at 0:06
I'm still learning
data.table, but I had never thought to use c(...). Interesting to see that a summarizing function would return more than one row per group, I can see that as very useful in some circumstances. I'm not certain where your question is, though: your code is good at demonstrating the difference: in the .(...) case you get one row per group. I suspect the equivalent of your second command is d[, .(c(mean(Rate1), mean(Rate2))), by = Name], which might clear up why it returns a single column, each group with two rows.– r2evans
Nov 14 '18 at 22:40
I'm still learning
data.table, but I had never thought to use c(...). Interesting to see that a summarizing function would return more than one row per group, I can see that as very useful in some circumstances. I'm not certain where your question is, though: your code is good at demonstrating the difference: in the .(...) case you get one row per group. I suspect the equivalent of your second command is d[, .(c(mean(Rate1), mean(Rate2))), by = Name], which might clear up why it returns a single column, each group with two rows.– r2evans
Nov 14 '18 at 22:40
2
2
Please have a look at the nice vignette Introduction to
data.table: "As long as j returns a list, each element of the list becomes a column in the resulting data.table"; "When there’s only one column or expression to refer to in j [...], we can drop the .() notation. This is purely for convenience." In your case, the c() concatenates the two means to one column.– Henrik
Nov 14 '18 at 22:42
Please have a look at the nice vignette Introduction to
data.table: "As long as j returns a list, each element of the list becomes a column in the resulting data.table"; "When there’s only one column or expression to refer to in j [...], we can drop the .() notation. This is purely for convenience." In your case, the c() concatenates the two means to one column.– Henrik
Nov 14 '18 at 22:42
1
1
@r2evans - I suppose
j + by= are not strictly a summarizing function, but rather a flexible grouping capability. You can even return more rows than are in the original data.– thelatemail
Nov 15 '18 at 0:04
@r2evans - I suppose
j + by= are not strictly a summarizing function, but rather a flexible grouping capability. You can even return more rows than are in the original data.– thelatemail
Nov 15 '18 at 0:04
Yes, @thelatemail, I'm gathering that. This is one key difference between
data.table summarizing and dplyr::summarize, where it fails if the returned summary is other than length 1. In dplyr-land, I would have summarized with a do(...) block so that I can return more than 1, it's nice to not have to do something similar here.– r2evans
Nov 15 '18 at 0:06
Yes, @thelatemail, I'm gathering that. This is one key difference between
data.table summarizing and dplyr::summarize, where it fails if the returned summary is other than length 1. In dplyr-land, I would have summarized with a do(...) block so that I can return more than 1, it's nice to not have to do something similar here.– r2evans
Nov 15 '18 at 0:06
add a comment |
0
active
oldest
votes
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%2f53309515%2fr-data-table-list-output-vs-vector-output%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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%2f53309515%2fr-data-table-list-output-vs-vector-output%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
I'm still learning
data.table, but I had never thought to usec(...). Interesting to see that a summarizing function would return more than one row per group, I can see that as very useful in some circumstances. I'm not certain where your question is, though: your code is good at demonstrating the difference: in the.(...)case you get one row per group. I suspect the equivalent of your second command isd[, .(c(mean(Rate1), mean(Rate2))), by = Name], which might clear up why it returns a single column, each group with two rows.– r2evans
Nov 14 '18 at 22:40
2
Please have a look at the nice vignette Introduction to
data.table: "As long asjreturns a list, each element of the list becomes a column in the resultingdata.table"; "When there’s only one column or expression to refer to inj[...], we can drop the.()notation. This is purely for convenience." In your case, thec()concatenates the two means to one column.– Henrik
Nov 14 '18 at 22:42
1
@r2evans - I suppose
j+by=are not strictly a summarizing function, but rather a flexible grouping capability. You can even return more rows than are in the original data.– thelatemail
Nov 15 '18 at 0:04
Yes, @thelatemail, I'm gathering that. This is one key difference between
data.tablesummarizing anddplyr::summarize, where it fails if the returned summary is other than length 1. Indplyr-land, I would have summarized with ado(...)block so that I can return more than 1, it's nice to not have to do something similar here.– r2evans
Nov 15 '18 at 0:06