Pass variable to tidyr's gather to rename key/value columns?
I would like to call tidyr::gather()
inside a custom function, to which I pass a pair of character variables that will be used to rename the key
and value
columns. e.g.
myFunc <- function(mydata, key.col, val.col) {
new.data <- tidyr::gather(data = mydata, key = key.col, value = val.col)
return(new.data)
}
However, this does not work as desired.
temp.data <- data.frame(day.1 = c(20, 22, 23), day.2 = c(32, 22, 45), day.3 = c(17, 9, 33))
# Call my custom function, renaming the key and value columns
# "day" and "temp", respectively
long.data <- myFunc(mydata = temp.data, key.col = "day", val.col = "temp")
# Columns have *not* been renamed as desired
head(long.data)
key.col val.col
1 day.1 20
2 day.1 22
3 day.1 23
4 day.2 32
5 day.2 22
6 day.2 45
Desired output:
head(long.data)
day temp
1 day.1 20
2 day.1 22
3 day.1 23
4 day.2 32
5 day.2 22
6 day.2 45
My understanding is that gather()
uses bare variable names for most arguments (as it has in this example, using "key.col"
as the column name as opposed to the value stored in key.col
). I have attempted a number of ways of passing a value in the gather()
call, but most return errors. For example, these three variants of the gather()
call within myFunc
return Error: Invalid column specification
(ignoring, for illustrative purposes, the value
parameter, which has identical behavior):
gather(data = mydata, key = as.character(key.col) value = val.col)
gather(data = mydata, key = as.name(key.col) value = val.col)
gather(data = mydata, key = as.name(as.character(key.col)) value = val.col)
As a workaround, I just rename the columns following the call to gather()
:
colnames(long.data)[colnames(long.data) == "key"] <- "day"
But given gather()
's purported functionality for renaming the key/value columns, how can I do this in the gather()
call within a custom function?
r tidyr
add a comment |
I would like to call tidyr::gather()
inside a custom function, to which I pass a pair of character variables that will be used to rename the key
and value
columns. e.g.
myFunc <- function(mydata, key.col, val.col) {
new.data <- tidyr::gather(data = mydata, key = key.col, value = val.col)
return(new.data)
}
However, this does not work as desired.
temp.data <- data.frame(day.1 = c(20, 22, 23), day.2 = c(32, 22, 45), day.3 = c(17, 9, 33))
# Call my custom function, renaming the key and value columns
# "day" and "temp", respectively
long.data <- myFunc(mydata = temp.data, key.col = "day", val.col = "temp")
# Columns have *not* been renamed as desired
head(long.data)
key.col val.col
1 day.1 20
2 day.1 22
3 day.1 23
4 day.2 32
5 day.2 22
6 day.2 45
Desired output:
head(long.data)
day temp
1 day.1 20
2 day.1 22
3 day.1 23
4 day.2 32
5 day.2 22
6 day.2 45
My understanding is that gather()
uses bare variable names for most arguments (as it has in this example, using "key.col"
as the column name as opposed to the value stored in key.col
). I have attempted a number of ways of passing a value in the gather()
call, but most return errors. For example, these three variants of the gather()
call within myFunc
return Error: Invalid column specification
(ignoring, for illustrative purposes, the value
parameter, which has identical behavior):
gather(data = mydata, key = as.character(key.col) value = val.col)
gather(data = mydata, key = as.name(key.col) value = val.col)
gather(data = mydata, key = as.name(as.character(key.col)) value = val.col)
As a workaround, I just rename the columns following the call to gather()
:
colnames(long.data)[colnames(long.data) == "key"] <- "day"
But given gather()
's purported functionality for renaming the key/value columns, how can I do this in the gather()
call within a custom function?
r tidyr
Read?gather
and pay attention to the "See Also" section. Then Googling the appropriate function name would likely lead to you this.
– joran
Jun 10 '16 at 20:08
add a comment |
I would like to call tidyr::gather()
inside a custom function, to which I pass a pair of character variables that will be used to rename the key
and value
columns. e.g.
myFunc <- function(mydata, key.col, val.col) {
new.data <- tidyr::gather(data = mydata, key = key.col, value = val.col)
return(new.data)
}
However, this does not work as desired.
temp.data <- data.frame(day.1 = c(20, 22, 23), day.2 = c(32, 22, 45), day.3 = c(17, 9, 33))
# Call my custom function, renaming the key and value columns
# "day" and "temp", respectively
long.data <- myFunc(mydata = temp.data, key.col = "day", val.col = "temp")
# Columns have *not* been renamed as desired
head(long.data)
key.col val.col
1 day.1 20
2 day.1 22
3 day.1 23
4 day.2 32
5 day.2 22
6 day.2 45
Desired output:
head(long.data)
day temp
1 day.1 20
2 day.1 22
3 day.1 23
4 day.2 32
5 day.2 22
6 day.2 45
My understanding is that gather()
uses bare variable names for most arguments (as it has in this example, using "key.col"
as the column name as opposed to the value stored in key.col
). I have attempted a number of ways of passing a value in the gather()
call, but most return errors. For example, these three variants of the gather()
call within myFunc
return Error: Invalid column specification
(ignoring, for illustrative purposes, the value
parameter, which has identical behavior):
gather(data = mydata, key = as.character(key.col) value = val.col)
gather(data = mydata, key = as.name(key.col) value = val.col)
gather(data = mydata, key = as.name(as.character(key.col)) value = val.col)
As a workaround, I just rename the columns following the call to gather()
:
colnames(long.data)[colnames(long.data) == "key"] <- "day"
But given gather()
's purported functionality for renaming the key/value columns, how can I do this in the gather()
call within a custom function?
r tidyr
I would like to call tidyr::gather()
inside a custom function, to which I pass a pair of character variables that will be used to rename the key
and value
columns. e.g.
myFunc <- function(mydata, key.col, val.col) {
new.data <- tidyr::gather(data = mydata, key = key.col, value = val.col)
return(new.data)
}
However, this does not work as desired.
temp.data <- data.frame(day.1 = c(20, 22, 23), day.2 = c(32, 22, 45), day.3 = c(17, 9, 33))
# Call my custom function, renaming the key and value columns
# "day" and "temp", respectively
long.data <- myFunc(mydata = temp.data, key.col = "day", val.col = "temp")
# Columns have *not* been renamed as desired
head(long.data)
key.col val.col
1 day.1 20
2 day.1 22
3 day.1 23
4 day.2 32
5 day.2 22
6 day.2 45
Desired output:
head(long.data)
day temp
1 day.1 20
2 day.1 22
3 day.1 23
4 day.2 32
5 day.2 22
6 day.2 45
My understanding is that gather()
uses bare variable names for most arguments (as it has in this example, using "key.col"
as the column name as opposed to the value stored in key.col
). I have attempted a number of ways of passing a value in the gather()
call, but most return errors. For example, these three variants of the gather()
call within myFunc
return Error: Invalid column specification
(ignoring, for illustrative purposes, the value
parameter, which has identical behavior):
gather(data = mydata, key = as.character(key.col) value = val.col)
gather(data = mydata, key = as.name(key.col) value = val.col)
gather(data = mydata, key = as.name(as.character(key.col)) value = val.col)
As a workaround, I just rename the columns following the call to gather()
:
colnames(long.data)[colnames(long.data) == "key"] <- "day"
But given gather()
's purported functionality for renaming the key/value columns, how can I do this in the gather()
call within a custom function?
r tidyr
r tidyr
edited Jun 10 '16 at 20:27
Molx
5,41921938
5,41921938
asked Jun 10 '16 at 19:42
JeffJeff
1108
1108
Read?gather
and pay attention to the "See Also" section. Then Googling the appropriate function name would likely lead to you this.
– joran
Jun 10 '16 at 20:08
add a comment |
Read?gather
and pay attention to the "See Also" section. Then Googling the appropriate function name would likely lead to you this.
– joran
Jun 10 '16 at 20:08
Read
?gather
and pay attention to the "See Also" section. Then Googling the appropriate function name would likely lead to you this.– joran
Jun 10 '16 at 20:08
Read
?gather
and pay attention to the "See Also" section. Then Googling the appropriate function name would likely lead to you this.– joran
Jun 10 '16 at 20:08
add a comment |
4 Answers
4
active
oldest
votes
To put it in a function you have to use gather_()
like so.
myFunc <- function(mydata, key.col, val.col, gather.cols) {
new.data <- gather_(data = mydata,
key_col = key.col,
value_col = val.col,
gather_cols = colnames(mydata)[gather.cols])
return(new.data)
}
temp.data <- data.frame(day.1 = c(20, 22, 23), day.2 = c(32, 22, 45),
day.3 = c(17, 9, 33))
temp.data
day.1 day.2 day.3
1 20 32 17
2 22 22 9
3 23 45 33
# Call my custom function, renaming the key and value columns
# "day" and "temp", respectively
long.data <- myFunc(mydata = temp.data, key.col = "day", val.col =
"temp", gather.cols = 1:3)
# Columns *have* been renamed as desired
head(long.data)
day temp
1 day.1 20
2 day.1 22
3 day.1 23
4 day.2 32
5 day.2 22
6 day.2 45
As stated, the main difference is in gather_
you have to specify the columns you want to gather up with the gather_cols
argument.
2
Great explanation. I did not realize Wickham had imbued the humble underscore with such power.
– Jeff
Jun 10 '16 at 22:06
add a comment |
Most (if not all) Haldey's functions that use bare variable names as arguments (such as dplyr
's functions) have a function_
version that uses regular evaluation and are "suitable for programming with". So, what you need should just be:
myFunc <- function(mydata, key.col, val.col) {
tidyr::gather_(data = mydata, key_col = key.col,
value_col = val.col, gather_cols = colnames(mydata))
}
The only "catch" here is that it is mandatory to specify the gather_cols
, which isn't necessary when using gather
or can be done separately as ...
.
And then:
> myFunc2(mydata = temp.data, key.col = "day", val.col = "temp")
day temp
1 day.1 20
2 day.1 22
3 day.1 23
4 day.2 32
5 day.2 22
6 day.2 45
7 day.3 17
8 day.3 9
9 day.3 33
Good explanation; inclusion ofgather_cols
was thoughtful.
– Jeff
Jun 10 '16 at 22:07
add a comment |
Note the underscore versions of functions are now deprecated (at least as of tidyr version 0.8.2). See, e.g., ?gather_
add a comment |
...and having had the same question, I now found the answer here: https://dplyr.tidyverse.org/articles/programming.html
You can have dplyr evaluate symbols by setting them off with exclamation marks. In your original question, the code would be:
gather(data = mydata, key = !!key.col value = !!val.col)
add a comment |
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%2f37756014%2fpass-variable-to-tidyrs-gather-to-rename-key-value-columns%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
To put it in a function you have to use gather_()
like so.
myFunc <- function(mydata, key.col, val.col, gather.cols) {
new.data <- gather_(data = mydata,
key_col = key.col,
value_col = val.col,
gather_cols = colnames(mydata)[gather.cols])
return(new.data)
}
temp.data <- data.frame(day.1 = c(20, 22, 23), day.2 = c(32, 22, 45),
day.3 = c(17, 9, 33))
temp.data
day.1 day.2 day.3
1 20 32 17
2 22 22 9
3 23 45 33
# Call my custom function, renaming the key and value columns
# "day" and "temp", respectively
long.data <- myFunc(mydata = temp.data, key.col = "day", val.col =
"temp", gather.cols = 1:3)
# Columns *have* been renamed as desired
head(long.data)
day temp
1 day.1 20
2 day.1 22
3 day.1 23
4 day.2 32
5 day.2 22
6 day.2 45
As stated, the main difference is in gather_
you have to specify the columns you want to gather up with the gather_cols
argument.
2
Great explanation. I did not realize Wickham had imbued the humble underscore with such power.
– Jeff
Jun 10 '16 at 22:06
add a comment |
To put it in a function you have to use gather_()
like so.
myFunc <- function(mydata, key.col, val.col, gather.cols) {
new.data <- gather_(data = mydata,
key_col = key.col,
value_col = val.col,
gather_cols = colnames(mydata)[gather.cols])
return(new.data)
}
temp.data <- data.frame(day.1 = c(20, 22, 23), day.2 = c(32, 22, 45),
day.3 = c(17, 9, 33))
temp.data
day.1 day.2 day.3
1 20 32 17
2 22 22 9
3 23 45 33
# Call my custom function, renaming the key and value columns
# "day" and "temp", respectively
long.data <- myFunc(mydata = temp.data, key.col = "day", val.col =
"temp", gather.cols = 1:3)
# Columns *have* been renamed as desired
head(long.data)
day temp
1 day.1 20
2 day.1 22
3 day.1 23
4 day.2 32
5 day.2 22
6 day.2 45
As stated, the main difference is in gather_
you have to specify the columns you want to gather up with the gather_cols
argument.
2
Great explanation. I did not realize Wickham had imbued the humble underscore with such power.
– Jeff
Jun 10 '16 at 22:06
add a comment |
To put it in a function you have to use gather_()
like so.
myFunc <- function(mydata, key.col, val.col, gather.cols) {
new.data <- gather_(data = mydata,
key_col = key.col,
value_col = val.col,
gather_cols = colnames(mydata)[gather.cols])
return(new.data)
}
temp.data <- data.frame(day.1 = c(20, 22, 23), day.2 = c(32, 22, 45),
day.3 = c(17, 9, 33))
temp.data
day.1 day.2 day.3
1 20 32 17
2 22 22 9
3 23 45 33
# Call my custom function, renaming the key and value columns
# "day" and "temp", respectively
long.data <- myFunc(mydata = temp.data, key.col = "day", val.col =
"temp", gather.cols = 1:3)
# Columns *have* been renamed as desired
head(long.data)
day temp
1 day.1 20
2 day.1 22
3 day.1 23
4 day.2 32
5 day.2 22
6 day.2 45
As stated, the main difference is in gather_
you have to specify the columns you want to gather up with the gather_cols
argument.
To put it in a function you have to use gather_()
like so.
myFunc <- function(mydata, key.col, val.col, gather.cols) {
new.data <- gather_(data = mydata,
key_col = key.col,
value_col = val.col,
gather_cols = colnames(mydata)[gather.cols])
return(new.data)
}
temp.data <- data.frame(day.1 = c(20, 22, 23), day.2 = c(32, 22, 45),
day.3 = c(17, 9, 33))
temp.data
day.1 day.2 day.3
1 20 32 17
2 22 22 9
3 23 45 33
# Call my custom function, renaming the key and value columns
# "day" and "temp", respectively
long.data <- myFunc(mydata = temp.data, key.col = "day", val.col =
"temp", gather.cols = 1:3)
# Columns *have* been renamed as desired
head(long.data)
day temp
1 day.1 20
2 day.1 22
3 day.1 23
4 day.2 32
5 day.2 22
6 day.2 45
As stated, the main difference is in gather_
you have to specify the columns you want to gather up with the gather_cols
argument.
answered Jun 10 '16 at 20:25


Bryan GogginBryan Goggin
1,775716
1,775716
2
Great explanation. I did not realize Wickham had imbued the humble underscore with such power.
– Jeff
Jun 10 '16 at 22:06
add a comment |
2
Great explanation. I did not realize Wickham had imbued the humble underscore with such power.
– Jeff
Jun 10 '16 at 22:06
2
2
Great explanation. I did not realize Wickham had imbued the humble underscore with such power.
– Jeff
Jun 10 '16 at 22:06
Great explanation. I did not realize Wickham had imbued the humble underscore with such power.
– Jeff
Jun 10 '16 at 22:06
add a comment |
Most (if not all) Haldey's functions that use bare variable names as arguments (such as dplyr
's functions) have a function_
version that uses regular evaluation and are "suitable for programming with". So, what you need should just be:
myFunc <- function(mydata, key.col, val.col) {
tidyr::gather_(data = mydata, key_col = key.col,
value_col = val.col, gather_cols = colnames(mydata))
}
The only "catch" here is that it is mandatory to specify the gather_cols
, which isn't necessary when using gather
or can be done separately as ...
.
And then:
> myFunc2(mydata = temp.data, key.col = "day", val.col = "temp")
day temp
1 day.1 20
2 day.1 22
3 day.1 23
4 day.2 32
5 day.2 22
6 day.2 45
7 day.3 17
8 day.3 9
9 day.3 33
Good explanation; inclusion ofgather_cols
was thoughtful.
– Jeff
Jun 10 '16 at 22:07
add a comment |
Most (if not all) Haldey's functions that use bare variable names as arguments (such as dplyr
's functions) have a function_
version that uses regular evaluation and are "suitable for programming with". So, what you need should just be:
myFunc <- function(mydata, key.col, val.col) {
tidyr::gather_(data = mydata, key_col = key.col,
value_col = val.col, gather_cols = colnames(mydata))
}
The only "catch" here is that it is mandatory to specify the gather_cols
, which isn't necessary when using gather
or can be done separately as ...
.
And then:
> myFunc2(mydata = temp.data, key.col = "day", val.col = "temp")
day temp
1 day.1 20
2 day.1 22
3 day.1 23
4 day.2 32
5 day.2 22
6 day.2 45
7 day.3 17
8 day.3 9
9 day.3 33
Good explanation; inclusion ofgather_cols
was thoughtful.
– Jeff
Jun 10 '16 at 22:07
add a comment |
Most (if not all) Haldey's functions that use bare variable names as arguments (such as dplyr
's functions) have a function_
version that uses regular evaluation and are "suitable for programming with". So, what you need should just be:
myFunc <- function(mydata, key.col, val.col) {
tidyr::gather_(data = mydata, key_col = key.col,
value_col = val.col, gather_cols = colnames(mydata))
}
The only "catch" here is that it is mandatory to specify the gather_cols
, which isn't necessary when using gather
or can be done separately as ...
.
And then:
> myFunc2(mydata = temp.data, key.col = "day", val.col = "temp")
day temp
1 day.1 20
2 day.1 22
3 day.1 23
4 day.2 32
5 day.2 22
6 day.2 45
7 day.3 17
8 day.3 9
9 day.3 33
Most (if not all) Haldey's functions that use bare variable names as arguments (such as dplyr
's functions) have a function_
version that uses regular evaluation and are "suitable for programming with". So, what you need should just be:
myFunc <- function(mydata, key.col, val.col) {
tidyr::gather_(data = mydata, key_col = key.col,
value_col = val.col, gather_cols = colnames(mydata))
}
The only "catch" here is that it is mandatory to specify the gather_cols
, which isn't necessary when using gather
or can be done separately as ...
.
And then:
> myFunc2(mydata = temp.data, key.col = "day", val.col = "temp")
day temp
1 day.1 20
2 day.1 22
3 day.1 23
4 day.2 32
5 day.2 22
6 day.2 45
7 day.3 17
8 day.3 9
9 day.3 33
answered Jun 10 '16 at 20:22
MolxMolx
5,41921938
5,41921938
Good explanation; inclusion ofgather_cols
was thoughtful.
– Jeff
Jun 10 '16 at 22:07
add a comment |
Good explanation; inclusion ofgather_cols
was thoughtful.
– Jeff
Jun 10 '16 at 22:07
Good explanation; inclusion of
gather_cols
was thoughtful.– Jeff
Jun 10 '16 at 22:07
Good explanation; inclusion of
gather_cols
was thoughtful.– Jeff
Jun 10 '16 at 22:07
add a comment |
Note the underscore versions of functions are now deprecated (at least as of tidyr version 0.8.2). See, e.g., ?gather_
add a comment |
Note the underscore versions of functions are now deprecated (at least as of tidyr version 0.8.2). See, e.g., ?gather_
add a comment |
Note the underscore versions of functions are now deprecated (at least as of tidyr version 0.8.2). See, e.g., ?gather_
Note the underscore versions of functions are now deprecated (at least as of tidyr version 0.8.2). See, e.g., ?gather_
answered Jan 2 at 6:11
invertdnainvertdna
111
111
add a comment |
add a comment |
...and having had the same question, I now found the answer here: https://dplyr.tidyverse.org/articles/programming.html
You can have dplyr evaluate symbols by setting them off with exclamation marks. In your original question, the code would be:
gather(data = mydata, key = !!key.col value = !!val.col)
add a comment |
...and having had the same question, I now found the answer here: https://dplyr.tidyverse.org/articles/programming.html
You can have dplyr evaluate symbols by setting them off with exclamation marks. In your original question, the code would be:
gather(data = mydata, key = !!key.col value = !!val.col)
add a comment |
...and having had the same question, I now found the answer here: https://dplyr.tidyverse.org/articles/programming.html
You can have dplyr evaluate symbols by setting them off with exclamation marks. In your original question, the code would be:
gather(data = mydata, key = !!key.col value = !!val.col)
...and having had the same question, I now found the answer here: https://dplyr.tidyverse.org/articles/programming.html
You can have dplyr evaluate symbols by setting them off with exclamation marks. In your original question, the code would be:
gather(data = mydata, key = !!key.col value = !!val.col)
answered Jan 2 at 20:57
invertdnainvertdna
111
111
add a comment |
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.
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%2f37756014%2fpass-variable-to-tidyrs-gather-to-rename-key-value-columns%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
Read
?gather
and pay attention to the "See Also" section. Then Googling the appropriate function name would likely lead to you this.– joran
Jun 10 '16 at 20:08