What's the difference between using select + unlist from dplyr package and using the dollar sign?
I've been taking an online course in which the instructor always does the following to obtain, say, the column Col1
from a data.frame
object Dat
:
library(dplyr)
unlist(select(Dat, Col1))
Why not simply run Dat$Col1
? I notice a difference in the "presentation" of both results, but is there any other significant divergence between the two forms? Any operation will result in the same product for both?
r dplyr
New contributor
|
show 1 more comment
I've been taking an online course in which the instructor always does the following to obtain, say, the column Col1
from a data.frame
object Dat
:
library(dplyr)
unlist(select(Dat, Col1))
Why not simply run Dat$Col1
? I notice a difference in the "presentation" of both results, but is there any other significant divergence between the two forms? Any operation will result in the same product for both?
r dplyr
New contributor
8
Your instructor is probably just a fan of the tidyverse.Dat$Col1
is my preferred method for getting a column.
– Rich Scriven
2 days ago
9
...but not well-verse
d enough to know ofpull
.
– Henrik
2 days ago
4
Touche. Maybe OP can take the instructor to school.
– Rich Scriven
2 days ago
3
Wow, he loads an entire new library for that?
– cory
2 days ago
5
The rumours about the SO incidentspread
, and the instructor decided togather
every
student.today
he had started toselect
cigarettes withoutfilter
, and took a long, deeppull
tocompose
himself for aminute
. It was a mare’snest
. “Well, I have a crow topluck
with you,who
did this?”, the teacher started. "between
you and me: this is nofuns
. You better notcross
me, or I willreduce
your grades,every
grade. Whichever way youslice
it -period
!" The students looked at each other. Acomplete
farce. Happy newyear
every
one -tally
-ho!
– Henrik
2 days ago
|
show 1 more comment
I've been taking an online course in which the instructor always does the following to obtain, say, the column Col1
from a data.frame
object Dat
:
library(dplyr)
unlist(select(Dat, Col1))
Why not simply run Dat$Col1
? I notice a difference in the "presentation" of both results, but is there any other significant divergence between the two forms? Any operation will result in the same product for both?
r dplyr
New contributor
I've been taking an online course in which the instructor always does the following to obtain, say, the column Col1
from a data.frame
object Dat
:
library(dplyr)
unlist(select(Dat, Col1))
Why not simply run Dat$Col1
? I notice a difference in the "presentation" of both results, but is there any other significant divergence between the two forms? Any operation will result in the same product for both?
r dplyr
r dplyr
New contributor
New contributor
edited 2 days ago
Ben Bolker
132k11222309
132k11222309
New contributor
asked 2 days ago
G. MonteiroG. Monteiro
513
513
New contributor
New contributor
8
Your instructor is probably just a fan of the tidyverse.Dat$Col1
is my preferred method for getting a column.
– Rich Scriven
2 days ago
9
...but not well-verse
d enough to know ofpull
.
– Henrik
2 days ago
4
Touche. Maybe OP can take the instructor to school.
– Rich Scriven
2 days ago
3
Wow, he loads an entire new library for that?
– cory
2 days ago
5
The rumours about the SO incidentspread
, and the instructor decided togather
every
student.today
he had started toselect
cigarettes withoutfilter
, and took a long, deeppull
tocompose
himself for aminute
. It was a mare’snest
. “Well, I have a crow topluck
with you,who
did this?”, the teacher started. "between
you and me: this is nofuns
. You better notcross
me, or I willreduce
your grades,every
grade. Whichever way youslice
it -period
!" The students looked at each other. Acomplete
farce. Happy newyear
every
one -tally
-ho!
– Henrik
2 days ago
|
show 1 more comment
8
Your instructor is probably just a fan of the tidyverse.Dat$Col1
is my preferred method for getting a column.
– Rich Scriven
2 days ago
9
...but not well-verse
d enough to know ofpull
.
– Henrik
2 days ago
4
Touche. Maybe OP can take the instructor to school.
– Rich Scriven
2 days ago
3
Wow, he loads an entire new library for that?
– cory
2 days ago
5
The rumours about the SO incidentspread
, and the instructor decided togather
every
student.today
he had started toselect
cigarettes withoutfilter
, and took a long, deeppull
tocompose
himself for aminute
. It was a mare’snest
. “Well, I have a crow topluck
with you,who
did this?”, the teacher started. "between
you and me: this is nofuns
. You better notcross
me, or I willreduce
your grades,every
grade. Whichever way youslice
it -period
!" The students looked at each other. Acomplete
farce. Happy newyear
every
one -tally
-ho!
– Henrik
2 days ago
8
8
Your instructor is probably just a fan of the tidyverse.
Dat$Col1
is my preferred method for getting a column.– Rich Scriven
2 days ago
Your instructor is probably just a fan of the tidyverse.
Dat$Col1
is my preferred method for getting a column.– Rich Scriven
2 days ago
9
9
...but not well-
verse
d enough to know of pull
.– Henrik
2 days ago
...but not well-
verse
d enough to know of pull
.– Henrik
2 days ago
4
4
Touche. Maybe OP can take the instructor to school.
– Rich Scriven
2 days ago
Touche. Maybe OP can take the instructor to school.
– Rich Scriven
2 days ago
3
3
Wow, he loads an entire new library for that?
– cory
2 days ago
Wow, he loads an entire new library for that?
– cory
2 days ago
5
5
The rumours about the SO incident
spread
, and the instructor decided to gather
every
student. today
he had started to select
cigarettes without filter
, and took a long, deep pull
to compose
himself for a minute
. It was a mare’s nest
. “Well, I have a crow to pluck
with you, who
did this?”, the teacher started. "between
you and me: this is no funs
. You better not cross
me, or I will reduce
your grades, every
grade. Whichever way you slice
it - period
!" The students looked at each other. A complete
farce. Happy new year
every
one - tally
-ho!– Henrik
2 days ago
The rumours about the SO incident
spread
, and the instructor decided to gather
every
student. today
he had started to select
cigarettes without filter
, and took a long, deep pull
to compose
himself for a minute
. It was a mare’s nest
. “Well, I have a crow to pluck
with you, who
did this?”, the teacher started. "between
you and me: this is no funs
. You better not cross
me, or I will reduce
your grades, every
grade. Whichever way you slice
it - period
!" The students looked at each other. A complete
farce. Happy new year
every
one - tally
-ho!– Henrik
2 days ago
|
show 1 more comment
1 Answer
1
active
oldest
votes
(Posting comments as community wiki.)
These are not quite equivalent - unlist(select(.))
keeps (probably unwanted) names.
dd <- data.frame(Col1=c("abc","def"))
str(unlist(select(dd,Col1)))
## Factor w/ 2 levels "abc","def": 1 2
## - attr(*, "names")= chr [1:2] "Col11" "Col12"
str(dd$Col1)
## Factor w/ 2 levels "abc","def": 1 2
Your instructor is probably just a fan of the tidyverse (@RichScriven); pull(Dat, Col1)
or (for extreme "tidiness") Dat %>% pull(Col1)
would be more idiomatic (@Henrik). Dat$Col1
or Dat[["Col1"]]
would be the base-R equivalents (the former is more convenient for interactive use, the latter is marginally safer for programming purposes since it won't do name-completion).
It hardly matters, but the tidyverse approaches are much slower.
microbenchmark(dd$Col1,dd[["Col1"]],pull(dd,Col1),unlist(select(dd,Col1)))
Unit: microseconds
expr min lq mean median uq
dd$Col1 5.296 10.9630 14.86871 13.4040 17.160
dd[["Col1"]] 7.870 9.6535 15.18874 11.8270 16.635
pull(dd, Col1) 44.160 108.7625 128.89342 117.8415 136.890
unlist(select(dd, Col1)) 601.480 1132.8240 1436.44178 1214.4420 1378.141
max neval cld
31.036 100 a
88.842 100 a
422.462 100 a
8796.964 100 b
2
Since you mentioned it,pull(dd, Col1)
is twice as fast asdd %>% pull(Col1)
. Tested with a much largerdd <- data.frame(Col1 = sample(c("abc", "def"), 1e6, TRUE))
.
– Rui Barradas
2 days ago
1
Just to note, there's always the use.names argument to the base R function,unlist
which will drop any unwanted names:unlist(select(.), use.names=FALSE)
.
– lmo
2 days ago
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
});
}
});
G. Monteiro is a new contributor. Be nice, and check out our Code of Conduct.
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%2f54056096%2fwhats-the-difference-between-using-select-unlist-from-dplyr-package-and-using%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
(Posting comments as community wiki.)
These are not quite equivalent - unlist(select(.))
keeps (probably unwanted) names.
dd <- data.frame(Col1=c("abc","def"))
str(unlist(select(dd,Col1)))
## Factor w/ 2 levels "abc","def": 1 2
## - attr(*, "names")= chr [1:2] "Col11" "Col12"
str(dd$Col1)
## Factor w/ 2 levels "abc","def": 1 2
Your instructor is probably just a fan of the tidyverse (@RichScriven); pull(Dat, Col1)
or (for extreme "tidiness") Dat %>% pull(Col1)
would be more idiomatic (@Henrik). Dat$Col1
or Dat[["Col1"]]
would be the base-R equivalents (the former is more convenient for interactive use, the latter is marginally safer for programming purposes since it won't do name-completion).
It hardly matters, but the tidyverse approaches are much slower.
microbenchmark(dd$Col1,dd[["Col1"]],pull(dd,Col1),unlist(select(dd,Col1)))
Unit: microseconds
expr min lq mean median uq
dd$Col1 5.296 10.9630 14.86871 13.4040 17.160
dd[["Col1"]] 7.870 9.6535 15.18874 11.8270 16.635
pull(dd, Col1) 44.160 108.7625 128.89342 117.8415 136.890
unlist(select(dd, Col1)) 601.480 1132.8240 1436.44178 1214.4420 1378.141
max neval cld
31.036 100 a
88.842 100 a
422.462 100 a
8796.964 100 b
2
Since you mentioned it,pull(dd, Col1)
is twice as fast asdd %>% pull(Col1)
. Tested with a much largerdd <- data.frame(Col1 = sample(c("abc", "def"), 1e6, TRUE))
.
– Rui Barradas
2 days ago
1
Just to note, there's always the use.names argument to the base R function,unlist
which will drop any unwanted names:unlist(select(.), use.names=FALSE)
.
– lmo
2 days ago
add a comment |
(Posting comments as community wiki.)
These are not quite equivalent - unlist(select(.))
keeps (probably unwanted) names.
dd <- data.frame(Col1=c("abc","def"))
str(unlist(select(dd,Col1)))
## Factor w/ 2 levels "abc","def": 1 2
## - attr(*, "names")= chr [1:2] "Col11" "Col12"
str(dd$Col1)
## Factor w/ 2 levels "abc","def": 1 2
Your instructor is probably just a fan of the tidyverse (@RichScriven); pull(Dat, Col1)
or (for extreme "tidiness") Dat %>% pull(Col1)
would be more idiomatic (@Henrik). Dat$Col1
or Dat[["Col1"]]
would be the base-R equivalents (the former is more convenient for interactive use, the latter is marginally safer for programming purposes since it won't do name-completion).
It hardly matters, but the tidyverse approaches are much slower.
microbenchmark(dd$Col1,dd[["Col1"]],pull(dd,Col1),unlist(select(dd,Col1)))
Unit: microseconds
expr min lq mean median uq
dd$Col1 5.296 10.9630 14.86871 13.4040 17.160
dd[["Col1"]] 7.870 9.6535 15.18874 11.8270 16.635
pull(dd, Col1) 44.160 108.7625 128.89342 117.8415 136.890
unlist(select(dd, Col1)) 601.480 1132.8240 1436.44178 1214.4420 1378.141
max neval cld
31.036 100 a
88.842 100 a
422.462 100 a
8796.964 100 b
2
Since you mentioned it,pull(dd, Col1)
is twice as fast asdd %>% pull(Col1)
. Tested with a much largerdd <- data.frame(Col1 = sample(c("abc", "def"), 1e6, TRUE))
.
– Rui Barradas
2 days ago
1
Just to note, there's always the use.names argument to the base R function,unlist
which will drop any unwanted names:unlist(select(.), use.names=FALSE)
.
– lmo
2 days ago
add a comment |
(Posting comments as community wiki.)
These are not quite equivalent - unlist(select(.))
keeps (probably unwanted) names.
dd <- data.frame(Col1=c("abc","def"))
str(unlist(select(dd,Col1)))
## Factor w/ 2 levels "abc","def": 1 2
## - attr(*, "names")= chr [1:2] "Col11" "Col12"
str(dd$Col1)
## Factor w/ 2 levels "abc","def": 1 2
Your instructor is probably just a fan of the tidyverse (@RichScriven); pull(Dat, Col1)
or (for extreme "tidiness") Dat %>% pull(Col1)
would be more idiomatic (@Henrik). Dat$Col1
or Dat[["Col1"]]
would be the base-R equivalents (the former is more convenient for interactive use, the latter is marginally safer for programming purposes since it won't do name-completion).
It hardly matters, but the tidyverse approaches are much slower.
microbenchmark(dd$Col1,dd[["Col1"]],pull(dd,Col1),unlist(select(dd,Col1)))
Unit: microseconds
expr min lq mean median uq
dd$Col1 5.296 10.9630 14.86871 13.4040 17.160
dd[["Col1"]] 7.870 9.6535 15.18874 11.8270 16.635
pull(dd, Col1) 44.160 108.7625 128.89342 117.8415 136.890
unlist(select(dd, Col1)) 601.480 1132.8240 1436.44178 1214.4420 1378.141
max neval cld
31.036 100 a
88.842 100 a
422.462 100 a
8796.964 100 b
(Posting comments as community wiki.)
These are not quite equivalent - unlist(select(.))
keeps (probably unwanted) names.
dd <- data.frame(Col1=c("abc","def"))
str(unlist(select(dd,Col1)))
## Factor w/ 2 levels "abc","def": 1 2
## - attr(*, "names")= chr [1:2] "Col11" "Col12"
str(dd$Col1)
## Factor w/ 2 levels "abc","def": 1 2
Your instructor is probably just a fan of the tidyverse (@RichScriven); pull(Dat, Col1)
or (for extreme "tidiness") Dat %>% pull(Col1)
would be more idiomatic (@Henrik). Dat$Col1
or Dat[["Col1"]]
would be the base-R equivalents (the former is more convenient for interactive use, the latter is marginally safer for programming purposes since it won't do name-completion).
It hardly matters, but the tidyverse approaches are much slower.
microbenchmark(dd$Col1,dd[["Col1"]],pull(dd,Col1),unlist(select(dd,Col1)))
Unit: microseconds
expr min lq mean median uq
dd$Col1 5.296 10.9630 14.86871 13.4040 17.160
dd[["Col1"]] 7.870 9.6535 15.18874 11.8270 16.635
pull(dd, Col1) 44.160 108.7625 128.89342 117.8415 136.890
unlist(select(dd, Col1)) 601.480 1132.8240 1436.44178 1214.4420 1378.141
max neval cld
31.036 100 a
88.842 100 a
422.462 100 a
8796.964 100 b
edited 2 days ago
community wiki
2 revs
Ben Bolker
2
Since you mentioned it,pull(dd, Col1)
is twice as fast asdd %>% pull(Col1)
. Tested with a much largerdd <- data.frame(Col1 = sample(c("abc", "def"), 1e6, TRUE))
.
– Rui Barradas
2 days ago
1
Just to note, there's always the use.names argument to the base R function,unlist
which will drop any unwanted names:unlist(select(.), use.names=FALSE)
.
– lmo
2 days ago
add a comment |
2
Since you mentioned it,pull(dd, Col1)
is twice as fast asdd %>% pull(Col1)
. Tested with a much largerdd <- data.frame(Col1 = sample(c("abc", "def"), 1e6, TRUE))
.
– Rui Barradas
2 days ago
1
Just to note, there's always the use.names argument to the base R function,unlist
which will drop any unwanted names:unlist(select(.), use.names=FALSE)
.
– lmo
2 days ago
2
2
Since you mentioned it,
pull(dd, Col1)
is twice as fast as dd %>% pull(Col1)
. Tested with a much larger dd <- data.frame(Col1 = sample(c("abc", "def"), 1e6, TRUE))
.– Rui Barradas
2 days ago
Since you mentioned it,
pull(dd, Col1)
is twice as fast as dd %>% pull(Col1)
. Tested with a much larger dd <- data.frame(Col1 = sample(c("abc", "def"), 1e6, TRUE))
.– Rui Barradas
2 days ago
1
1
Just to note, there's always the use.names argument to the base R function,
unlist
which will drop any unwanted names: unlist(select(.), use.names=FALSE)
.– lmo
2 days ago
Just to note, there's always the use.names argument to the base R function,
unlist
which will drop any unwanted names: unlist(select(.), use.names=FALSE)
.– lmo
2 days ago
add a comment |
G. Monteiro is a new contributor. Be nice, and check out our Code of Conduct.
G. Monteiro is a new contributor. Be nice, and check out our Code of Conduct.
G. Monteiro is a new contributor. Be nice, and check out our Code of Conduct.
G. Monteiro is a new contributor. Be nice, and check out our Code of Conduct.
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%2f54056096%2fwhats-the-difference-between-using-select-unlist-from-dplyr-package-and-using%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
8
Your instructor is probably just a fan of the tidyverse.
Dat$Col1
is my preferred method for getting a column.– Rich Scriven
2 days ago
9
...but not well-
verse
d enough to know ofpull
.– Henrik
2 days ago
4
Touche. Maybe OP can take the instructor to school.
– Rich Scriven
2 days ago
3
Wow, he loads an entire new library for that?
– cory
2 days ago
5
The rumours about the SO incident
spread
, and the instructor decided togather
every
student.today
he had started toselect
cigarettes withoutfilter
, and took a long, deeppull
tocompose
himself for aminute
. It was a mare’snest
. “Well, I have a crow topluck
with you,who
did this?”, the teacher started. "between
you and me: this is nofuns
. You better notcross
me, or I willreduce
your grades,every
grade. Whichever way youslice
it -period
!" The students looked at each other. Acomplete
farce. Happy newyear
every
one -tally
-ho!– Henrik
2 days ago