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
r list data.table grouping aggregation
add a comment |
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
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 at 22:40
2
Please have a look at the nice vignette Introduction todata.table
: "As long asj
returns 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 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 at 0:04
Yes, @thelatemail, I'm gathering that. This is one key difference betweendata.table
summarizing 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 at 0:06
add a comment |
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
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 at 22:45
Henrik
40.5k991107
40.5k991107
asked Nov 14 at 22:12
rosepark222
73
73
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 at 22:40
2
Please have a look at the nice vignette Introduction todata.table
: "As long asj
returns 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 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 at 0:04
Yes, @thelatemail, I'm gathering that. This is one key difference betweendata.table
summarizing 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 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 at 22:40
2
Please have a look at the nice vignette Introduction todata.table
: "As long asj
returns 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 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 at 0:04
Yes, @thelatemail, I'm gathering that. This is one key difference betweendata.table
summarizing 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 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
add a comment |
active
oldest
votes
active
oldest
votes
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.
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%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 at 22:40
2
Please have a look at the nice vignette Introduction to
data.table
: "As long asj
returns 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 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 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 at 0:06