Is there a way to produce a gather_at or gather_if function similar to mutate_at or mutate_if
I think the title is rather straight forward.
But just to provide some data and example:
test <- tibble(
ID1 = letters,
ID2 = LETTERS,
A1 = runif(26),
B1 = runif(26),
A2 = runif(26),
B2 = runif(26)
)
Is there a way to gather on only e.g., numerical columns with a simple
command such that:
test %>% gather_if(is.numeric, 'key', 'value')
? Which would give the same output as the following:
> test %>% gather('key', 'value', -ID1, -ID2)
# A tibble: 104 x 4
ID1 ID2 key value
<chr> <chr> <chr> <dbl>
1 a A A1 0.558
2 b B A1 0.0614
3 c C A1 0.999
4 d D A1 0.854
5 e E A1 0.463
6 f F A1 0.875
7 g G A1 0.796
8 h H A1 0.484
9 i I A1 0.336
10 j J A1 0.191
# ... with 94 more rows
Looking at the gather function:
> gather
function (data, key = "key", value = "value", ..., na.rm = FALSE,
convert = FALSE, factor_key = FALSE)
{
UseMethod("gather")
}
<bytecode: 0x000000001b71ff18>
<environment: namespace:tidyr>
It does not seem that straight forward to modify it (at least not for me who is a semi-novel R user).
Edit:
My vocabulary choice in dplyr might not be exactly accurate. But I think the MWE explains fairly well what type of function I'm going for.
Edit2:
Using the answer by bschneidr, an ad-hoc version of this could be done in the following way.
gather_if <- function(data, fun, key, value, ..., na.rm = FALSE, convert =
FALSE, factor_key = FALSE){
data %>%
gather(!!key, !!value, select_if(., fun) %>% colnames(), ...,
na.rm = FALSE, convert = FALSE, factor_key = FALSE)
}
Which gives:
> test %>% gather_if(is.numeric, 'key', 'value')
# A tibble: 104 x 4
ID1 ID2 key value
<chr> <chr> <chr> <dbl>
1 a A A1 0.558
2 b B A1 0.0614
3 c C A1 0.999
4 d D A1 0.854
5 e E A1 0.463
6 f F A1 0.875
7 g G A1 0.796
8 h H A1 0.484
9 i I A1 0.336
10 j J A1 0.191
# ... with 94 more rows
r dplyr
add a comment |
I think the title is rather straight forward.
But just to provide some data and example:
test <- tibble(
ID1 = letters,
ID2 = LETTERS,
A1 = runif(26),
B1 = runif(26),
A2 = runif(26),
B2 = runif(26)
)
Is there a way to gather on only e.g., numerical columns with a simple
command such that:
test %>% gather_if(is.numeric, 'key', 'value')
? Which would give the same output as the following:
> test %>% gather('key', 'value', -ID1, -ID2)
# A tibble: 104 x 4
ID1 ID2 key value
<chr> <chr> <chr> <dbl>
1 a A A1 0.558
2 b B A1 0.0614
3 c C A1 0.999
4 d D A1 0.854
5 e E A1 0.463
6 f F A1 0.875
7 g G A1 0.796
8 h H A1 0.484
9 i I A1 0.336
10 j J A1 0.191
# ... with 94 more rows
Looking at the gather function:
> gather
function (data, key = "key", value = "value", ..., na.rm = FALSE,
convert = FALSE, factor_key = FALSE)
{
UseMethod("gather")
}
<bytecode: 0x000000001b71ff18>
<environment: namespace:tidyr>
It does not seem that straight forward to modify it (at least not for me who is a semi-novel R user).
Edit:
My vocabulary choice in dplyr might not be exactly accurate. But I think the MWE explains fairly well what type of function I'm going for.
Edit2:
Using the answer by bschneidr, an ad-hoc version of this could be done in the following way.
gather_if <- function(data, fun, key, value, ..., na.rm = FALSE, convert =
FALSE, factor_key = FALSE){
data %>%
gather(!!key, !!value, select_if(., fun) %>% colnames(), ...,
na.rm = FALSE, convert = FALSE, factor_key = FALSE)
}
Which gives:
> test %>% gather_if(is.numeric, 'key', 'value')
# A tibble: 104 x 4
ID1 ID2 key value
<chr> <chr> <chr> <dbl>
1 a A A1 0.558
2 b B A1 0.0614
3 c C A1 0.999
4 d D A1 0.854
5 e E A1 0.463
6 f F A1 0.875
7 g G A1 0.796
8 h H A1 0.484
9 i I A1 0.336
10 j J A1 0.191
# ... with 94 more rows
r dplyr
1
Is the problem that you need it to be done programmatically without specifyingID1
andID2
?
– Sotos
Nov 21 '18 at 15:36
@Sotos Yes, that is exactly what I'm going for! :) I just want to exclude all categorical columns in the gather but keep them as separate columns. If that makes sense? I guess I could first identify the categorical columns and store them as symbols or some rlang tidy_eval. But it would seem, to me, that this type of function that I'm suggesting could be useful.
– Baraliuh
Nov 21 '18 at 15:39
add a comment |
I think the title is rather straight forward.
But just to provide some data and example:
test <- tibble(
ID1 = letters,
ID2 = LETTERS,
A1 = runif(26),
B1 = runif(26),
A2 = runif(26),
B2 = runif(26)
)
Is there a way to gather on only e.g., numerical columns with a simple
command such that:
test %>% gather_if(is.numeric, 'key', 'value')
? Which would give the same output as the following:
> test %>% gather('key', 'value', -ID1, -ID2)
# A tibble: 104 x 4
ID1 ID2 key value
<chr> <chr> <chr> <dbl>
1 a A A1 0.558
2 b B A1 0.0614
3 c C A1 0.999
4 d D A1 0.854
5 e E A1 0.463
6 f F A1 0.875
7 g G A1 0.796
8 h H A1 0.484
9 i I A1 0.336
10 j J A1 0.191
# ... with 94 more rows
Looking at the gather function:
> gather
function (data, key = "key", value = "value", ..., na.rm = FALSE,
convert = FALSE, factor_key = FALSE)
{
UseMethod("gather")
}
<bytecode: 0x000000001b71ff18>
<environment: namespace:tidyr>
It does not seem that straight forward to modify it (at least not for me who is a semi-novel R user).
Edit:
My vocabulary choice in dplyr might not be exactly accurate. But I think the MWE explains fairly well what type of function I'm going for.
Edit2:
Using the answer by bschneidr, an ad-hoc version of this could be done in the following way.
gather_if <- function(data, fun, key, value, ..., na.rm = FALSE, convert =
FALSE, factor_key = FALSE){
data %>%
gather(!!key, !!value, select_if(., fun) %>% colnames(), ...,
na.rm = FALSE, convert = FALSE, factor_key = FALSE)
}
Which gives:
> test %>% gather_if(is.numeric, 'key', 'value')
# A tibble: 104 x 4
ID1 ID2 key value
<chr> <chr> <chr> <dbl>
1 a A A1 0.558
2 b B A1 0.0614
3 c C A1 0.999
4 d D A1 0.854
5 e E A1 0.463
6 f F A1 0.875
7 g G A1 0.796
8 h H A1 0.484
9 i I A1 0.336
10 j J A1 0.191
# ... with 94 more rows
r dplyr
I think the title is rather straight forward.
But just to provide some data and example:
test <- tibble(
ID1 = letters,
ID2 = LETTERS,
A1 = runif(26),
B1 = runif(26),
A2 = runif(26),
B2 = runif(26)
)
Is there a way to gather on only e.g., numerical columns with a simple
command such that:
test %>% gather_if(is.numeric, 'key', 'value')
? Which would give the same output as the following:
> test %>% gather('key', 'value', -ID1, -ID2)
# A tibble: 104 x 4
ID1 ID2 key value
<chr> <chr> <chr> <dbl>
1 a A A1 0.558
2 b B A1 0.0614
3 c C A1 0.999
4 d D A1 0.854
5 e E A1 0.463
6 f F A1 0.875
7 g G A1 0.796
8 h H A1 0.484
9 i I A1 0.336
10 j J A1 0.191
# ... with 94 more rows
Looking at the gather function:
> gather
function (data, key = "key", value = "value", ..., na.rm = FALSE,
convert = FALSE, factor_key = FALSE)
{
UseMethod("gather")
}
<bytecode: 0x000000001b71ff18>
<environment: namespace:tidyr>
It does not seem that straight forward to modify it (at least not for me who is a semi-novel R user).
Edit:
My vocabulary choice in dplyr might not be exactly accurate. But I think the MWE explains fairly well what type of function I'm going for.
Edit2:
Using the answer by bschneidr, an ad-hoc version of this could be done in the following way.
gather_if <- function(data, fun, key, value, ..., na.rm = FALSE, convert =
FALSE, factor_key = FALSE){
data %>%
gather(!!key, !!value, select_if(., fun) %>% colnames(), ...,
na.rm = FALSE, convert = FALSE, factor_key = FALSE)
}
Which gives:
> test %>% gather_if(is.numeric, 'key', 'value')
# A tibble: 104 x 4
ID1 ID2 key value
<chr> <chr> <chr> <dbl>
1 a A A1 0.558
2 b B A1 0.0614
3 c C A1 0.999
4 d D A1 0.854
5 e E A1 0.463
6 f F A1 0.875
7 g G A1 0.796
8 h H A1 0.484
9 i I A1 0.336
10 j J A1 0.191
# ... with 94 more rows
r dplyr
r dplyr
edited Nov 21 '18 at 16:54
Baraliuh
asked Nov 21 '18 at 15:26
BaraliuhBaraliuh
1168
1168
1
Is the problem that you need it to be done programmatically without specifyingID1
andID2
?
– Sotos
Nov 21 '18 at 15:36
@Sotos Yes, that is exactly what I'm going for! :) I just want to exclude all categorical columns in the gather but keep them as separate columns. If that makes sense? I guess I could first identify the categorical columns and store them as symbols or some rlang tidy_eval. But it would seem, to me, that this type of function that I'm suggesting could be useful.
– Baraliuh
Nov 21 '18 at 15:39
add a comment |
1
Is the problem that you need it to be done programmatically without specifyingID1
andID2
?
– Sotos
Nov 21 '18 at 15:36
@Sotos Yes, that is exactly what I'm going for! :) I just want to exclude all categorical columns in the gather but keep them as separate columns. If that makes sense? I guess I could first identify the categorical columns and store them as symbols or some rlang tidy_eval. But it would seem, to me, that this type of function that I'm suggesting could be useful.
– Baraliuh
Nov 21 '18 at 15:39
1
1
Is the problem that you need it to be done programmatically without specifying
ID1
and ID2
?– Sotos
Nov 21 '18 at 15:36
Is the problem that you need it to be done programmatically without specifying
ID1
and ID2
?– Sotos
Nov 21 '18 at 15:36
@Sotos Yes, that is exactly what I'm going for! :) I just want to exclude all categorical columns in the gather but keep them as separate columns. If that makes sense? I guess I could first identify the categorical columns and store them as symbols or some rlang tidy_eval. But it would seem, to me, that this type of function that I'm suggesting could be useful.
– Baraliuh
Nov 21 '18 at 15:39
@Sotos Yes, that is exactly what I'm going for! :) I just want to exclude all categorical columns in the gather but keep them as separate columns. If that makes sense? I guess I could first identify the categorical columns and store them as symbols or some rlang tidy_eval. But it would seem, to me, that this type of function that I'm suggesting could be useful.
– Baraliuh
Nov 21 '18 at 15:39
add a comment |
3 Answers
3
active
oldest
votes
I think a gather_if
function is in the works for tidyr (see this pull request on tidyr's Github repo).
For the moment, I think the easiest method is to use dplyr's select_if
function inside the call to gather
.
test %>%
gather('key', 'value',
colnames(select_if(., is.numeric)))
1
Oh, that is great! Then I guess this thread is semi pointless. Is there a way to evaluate is.numeric as any general function using e.g., rlang? That way making an ad-hoc function in the meantime would be fairly simple.
– Baraliuh
Nov 21 '18 at 16:04
Ah, it looks like you've figured that out and added that to the question, unless I'm missing something.
– bschneidr
Nov 21 '18 at 16:51
Yes I did! Thanks to you! :) Making a last edit now and it should be working properly.
– Baraliuh
Nov 21 '18 at 16:53
add a comment |
One way to do it is to negate()
the numeric condition and extract the names. It does look a bit cumbersome but here it is,
library(tidyverse)
gather(test, key, value, -c(test %>% select_if(negate(is.numeric)) %>% names()))
which gives,
# A tibble: 104 x 4
ID1 ID2 key value
<chr> <chr> <chr> <dbl>
1 a A A1 0.624
2 b B A1 0.0740
3 c C A1 0.790
4 d D A1 0.312
5 e E A1 0.323
6 f F A1 0.826
7 g G A1 0.0533
8 h H A1 0.0828
9 i I A1 0.979
10 j J A1 0.453
# ... with 94 more rows
add a comment |
If think you want this:
gather_if <- function(data, FUN, key = "key", value = "value", na.rm = FALSE, convert = FALSE, factor_key = FALSE) {
data %>% {gather(., key = key, value = value , names(.)[sapply(., FUN = FUN)], na.rm = na.rm, convert = convert, factor_key = factor_key )}
}
call your new cool function:
test %>% gather_if(is.numeric, 'key', 'value')
result:
# A tibble: 104 x 4
# ID1 ID2 key value
# <chr> <chr> <chr> <dbl>
# 1 a A A1 0.693
# 2 b B A1 0.356
# 3 c C A1 0.650
# 4 d D A1 0.358
# 5 e E A1 0.650
# 6 f F A1 0.461
# 7 g G A1 0.222
# 8 h H A1 0.993
# 9 i I A1 0.679
#10 j J A1 0.331
# ... with 94 more rows
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
});
}
});
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%2f53415303%2fis-there-a-way-to-produce-a-gather-at-or-gather-if-function-similar-to-mutate-at%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think a gather_if
function is in the works for tidyr (see this pull request on tidyr's Github repo).
For the moment, I think the easiest method is to use dplyr's select_if
function inside the call to gather
.
test %>%
gather('key', 'value',
colnames(select_if(., is.numeric)))
1
Oh, that is great! Then I guess this thread is semi pointless. Is there a way to evaluate is.numeric as any general function using e.g., rlang? That way making an ad-hoc function in the meantime would be fairly simple.
– Baraliuh
Nov 21 '18 at 16:04
Ah, it looks like you've figured that out and added that to the question, unless I'm missing something.
– bschneidr
Nov 21 '18 at 16:51
Yes I did! Thanks to you! :) Making a last edit now and it should be working properly.
– Baraliuh
Nov 21 '18 at 16:53
add a comment |
I think a gather_if
function is in the works for tidyr (see this pull request on tidyr's Github repo).
For the moment, I think the easiest method is to use dplyr's select_if
function inside the call to gather
.
test %>%
gather('key', 'value',
colnames(select_if(., is.numeric)))
1
Oh, that is great! Then I guess this thread is semi pointless. Is there a way to evaluate is.numeric as any general function using e.g., rlang? That way making an ad-hoc function in the meantime would be fairly simple.
– Baraliuh
Nov 21 '18 at 16:04
Ah, it looks like you've figured that out and added that to the question, unless I'm missing something.
– bschneidr
Nov 21 '18 at 16:51
Yes I did! Thanks to you! :) Making a last edit now and it should be working properly.
– Baraliuh
Nov 21 '18 at 16:53
add a comment |
I think a gather_if
function is in the works for tidyr (see this pull request on tidyr's Github repo).
For the moment, I think the easiest method is to use dplyr's select_if
function inside the call to gather
.
test %>%
gather('key', 'value',
colnames(select_if(., is.numeric)))
I think a gather_if
function is in the works for tidyr (see this pull request on tidyr's Github repo).
For the moment, I think the easiest method is to use dplyr's select_if
function inside the call to gather
.
test %>%
gather('key', 'value',
colnames(select_if(., is.numeric)))
answered Nov 21 '18 at 15:59
bschneidrbschneidr
1,78211733
1,78211733
1
Oh, that is great! Then I guess this thread is semi pointless. Is there a way to evaluate is.numeric as any general function using e.g., rlang? That way making an ad-hoc function in the meantime would be fairly simple.
– Baraliuh
Nov 21 '18 at 16:04
Ah, it looks like you've figured that out and added that to the question, unless I'm missing something.
– bschneidr
Nov 21 '18 at 16:51
Yes I did! Thanks to you! :) Making a last edit now and it should be working properly.
– Baraliuh
Nov 21 '18 at 16:53
add a comment |
1
Oh, that is great! Then I guess this thread is semi pointless. Is there a way to evaluate is.numeric as any general function using e.g., rlang? That way making an ad-hoc function in the meantime would be fairly simple.
– Baraliuh
Nov 21 '18 at 16:04
Ah, it looks like you've figured that out and added that to the question, unless I'm missing something.
– bschneidr
Nov 21 '18 at 16:51
Yes I did! Thanks to you! :) Making a last edit now and it should be working properly.
– Baraliuh
Nov 21 '18 at 16:53
1
1
Oh, that is great! Then I guess this thread is semi pointless. Is there a way to evaluate is.numeric as any general function using e.g., rlang? That way making an ad-hoc function in the meantime would be fairly simple.
– Baraliuh
Nov 21 '18 at 16:04
Oh, that is great! Then I guess this thread is semi pointless. Is there a way to evaluate is.numeric as any general function using e.g., rlang? That way making an ad-hoc function in the meantime would be fairly simple.
– Baraliuh
Nov 21 '18 at 16:04
Ah, it looks like you've figured that out and added that to the question, unless I'm missing something.
– bschneidr
Nov 21 '18 at 16:51
Ah, it looks like you've figured that out and added that to the question, unless I'm missing something.
– bschneidr
Nov 21 '18 at 16:51
Yes I did! Thanks to you! :) Making a last edit now and it should be working properly.
– Baraliuh
Nov 21 '18 at 16:53
Yes I did! Thanks to you! :) Making a last edit now and it should be working properly.
– Baraliuh
Nov 21 '18 at 16:53
add a comment |
One way to do it is to negate()
the numeric condition and extract the names. It does look a bit cumbersome but here it is,
library(tidyverse)
gather(test, key, value, -c(test %>% select_if(negate(is.numeric)) %>% names()))
which gives,
# A tibble: 104 x 4
ID1 ID2 key value
<chr> <chr> <chr> <dbl>
1 a A A1 0.624
2 b B A1 0.0740
3 c C A1 0.790
4 d D A1 0.312
5 e E A1 0.323
6 f F A1 0.826
7 g G A1 0.0533
8 h H A1 0.0828
9 i I A1 0.979
10 j J A1 0.453
# ... with 94 more rows
add a comment |
One way to do it is to negate()
the numeric condition and extract the names. It does look a bit cumbersome but here it is,
library(tidyverse)
gather(test, key, value, -c(test %>% select_if(negate(is.numeric)) %>% names()))
which gives,
# A tibble: 104 x 4
ID1 ID2 key value
<chr> <chr> <chr> <dbl>
1 a A A1 0.624
2 b B A1 0.0740
3 c C A1 0.790
4 d D A1 0.312
5 e E A1 0.323
6 f F A1 0.826
7 g G A1 0.0533
8 h H A1 0.0828
9 i I A1 0.979
10 j J A1 0.453
# ... with 94 more rows
add a comment |
One way to do it is to negate()
the numeric condition and extract the names. It does look a bit cumbersome but here it is,
library(tidyverse)
gather(test, key, value, -c(test %>% select_if(negate(is.numeric)) %>% names()))
which gives,
# A tibble: 104 x 4
ID1 ID2 key value
<chr> <chr> <chr> <dbl>
1 a A A1 0.624
2 b B A1 0.0740
3 c C A1 0.790
4 d D A1 0.312
5 e E A1 0.323
6 f F A1 0.826
7 g G A1 0.0533
8 h H A1 0.0828
9 i I A1 0.979
10 j J A1 0.453
# ... with 94 more rows
One way to do it is to negate()
the numeric condition and extract the names. It does look a bit cumbersome but here it is,
library(tidyverse)
gather(test, key, value, -c(test %>% select_if(negate(is.numeric)) %>% names()))
which gives,
# A tibble: 104 x 4
ID1 ID2 key value
<chr> <chr> <chr> <dbl>
1 a A A1 0.624
2 b B A1 0.0740
3 c C A1 0.790
4 d D A1 0.312
5 e E A1 0.323
6 f F A1 0.826
7 g G A1 0.0533
8 h H A1 0.0828
9 i I A1 0.979
10 j J A1 0.453
# ... with 94 more rows
answered Nov 21 '18 at 15:50
SotosSotos
30.7k51641
30.7k51641
add a comment |
add a comment |
If think you want this:
gather_if <- function(data, FUN, key = "key", value = "value", na.rm = FALSE, convert = FALSE, factor_key = FALSE) {
data %>% {gather(., key = key, value = value , names(.)[sapply(., FUN = FUN)], na.rm = na.rm, convert = convert, factor_key = factor_key )}
}
call your new cool function:
test %>% gather_if(is.numeric, 'key', 'value')
result:
# A tibble: 104 x 4
# ID1 ID2 key value
# <chr> <chr> <chr> <dbl>
# 1 a A A1 0.693
# 2 b B A1 0.356
# 3 c C A1 0.650
# 4 d D A1 0.358
# 5 e E A1 0.650
# 6 f F A1 0.461
# 7 g G A1 0.222
# 8 h H A1 0.993
# 9 i I A1 0.679
#10 j J A1 0.331
# ... with 94 more rows
add a comment |
If think you want this:
gather_if <- function(data, FUN, key = "key", value = "value", na.rm = FALSE, convert = FALSE, factor_key = FALSE) {
data %>% {gather(., key = key, value = value , names(.)[sapply(., FUN = FUN)], na.rm = na.rm, convert = convert, factor_key = factor_key )}
}
call your new cool function:
test %>% gather_if(is.numeric, 'key', 'value')
result:
# A tibble: 104 x 4
# ID1 ID2 key value
# <chr> <chr> <chr> <dbl>
# 1 a A A1 0.693
# 2 b B A1 0.356
# 3 c C A1 0.650
# 4 d D A1 0.358
# 5 e E A1 0.650
# 6 f F A1 0.461
# 7 g G A1 0.222
# 8 h H A1 0.993
# 9 i I A1 0.679
#10 j J A1 0.331
# ... with 94 more rows
add a comment |
If think you want this:
gather_if <- function(data, FUN, key = "key", value = "value", na.rm = FALSE, convert = FALSE, factor_key = FALSE) {
data %>% {gather(., key = key, value = value , names(.)[sapply(., FUN = FUN)], na.rm = na.rm, convert = convert, factor_key = factor_key )}
}
call your new cool function:
test %>% gather_if(is.numeric, 'key', 'value')
result:
# A tibble: 104 x 4
# ID1 ID2 key value
# <chr> <chr> <chr> <dbl>
# 1 a A A1 0.693
# 2 b B A1 0.356
# 3 c C A1 0.650
# 4 d D A1 0.358
# 5 e E A1 0.650
# 6 f F A1 0.461
# 7 g G A1 0.222
# 8 h H A1 0.993
# 9 i I A1 0.679
#10 j J A1 0.331
# ... with 94 more rows
If think you want this:
gather_if <- function(data, FUN, key = "key", value = "value", na.rm = FALSE, convert = FALSE, factor_key = FALSE) {
data %>% {gather(., key = key, value = value , names(.)[sapply(., FUN = FUN)], na.rm = na.rm, convert = convert, factor_key = factor_key )}
}
call your new cool function:
test %>% gather_if(is.numeric, 'key', 'value')
result:
# A tibble: 104 x 4
# ID1 ID2 key value
# <chr> <chr> <chr> <dbl>
# 1 a A A1 0.693
# 2 b B A1 0.356
# 3 c C A1 0.650
# 4 d D A1 0.358
# 5 e E A1 0.650
# 6 f F A1 0.461
# 7 g G A1 0.222
# 8 h H A1 0.993
# 9 i I A1 0.679
#10 j J A1 0.331
# ... with 94 more rows
answered Nov 21 '18 at 15:58
Andre ElricoAndre Elrico
5,74511230
5,74511230
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%2f53415303%2fis-there-a-way-to-produce-a-gather-at-or-gather-if-function-similar-to-mutate-at%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
1
Is the problem that you need it to be done programmatically without specifying
ID1
andID2
?– Sotos
Nov 21 '18 at 15:36
@Sotos Yes, that is exactly what I'm going for! :) I just want to exclude all categorical columns in the gather but keep them as separate columns. If that makes sense? I guess I could first identify the categorical columns and store them as symbols or some rlang tidy_eval. But it would seem, to me, that this type of function that I'm suggesting could be useful.
– Baraliuh
Nov 21 '18 at 15:39