R data.table list output vs vector output











up vote
2
down vote

favorite












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









share|improve this question
























  • 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 at 22:40






  • 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 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 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 at 0:06















up vote
2
down vote

favorite












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









share|improve this question
























  • 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 at 22:40






  • 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 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 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 at 0:06













up vote
2
down vote

favorite









up vote
2
down vote

favorite











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









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 at 22:45









Henrik

40.5k991107




40.5k991107










asked Nov 14 at 22:12









rosepark222

73




73












  • 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 at 22:40






  • 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 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 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 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 at 22:40






  • 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 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 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 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 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 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 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 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 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 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 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 at 0:06

















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',
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
});


}
});














draft saved

draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents