How to exclude multiple columns in gather function












0















I have a data composed of columns with following names : ID,A,B,C,A_5,B_5,C_5



I want to use gather() function such that ID,A_5,B_5 and C_5 are excluded. I know that if I wanted to exclude only ID, I could have written :



df %>% gather(key = categories ,value = value, -ID)


But I could not figure out the way to exclude multiple columns at once. I would appreciate a lot if somebody can tell me the correct syntax. So far I have tried and failed with:



df %>% gather(key = categories ,value = value, -vars(ID,A_5,B_5,C_5)
df %>% gather(key = categories ,value = value, -c(ID,A_5,B_5,C_5)
df %>% gather(key = categories ,value = value, -list(ID,A_5,B_5,C_5)
df %>% gather(key = categories ,value = value, -list(ID,A_5,B_5,C_5)


Also I have several other data frames on which I would like to perform the same operation. They all have different column lengths and for some, I need to exclude more than 10 columns. Thus, referring to these columns by their index would be much more convenient. Is this possible? If so, how can it be achieved?



I did not provide a reproducible example since this is merely a question of syntax. I hope it is ok.



Thanks in advance.










share|improve this question




















  • 1





    df %>% gather(key = categories ,value = value, -ID, -A_5, -B_5, -C_5) ? Or df %>% gather(key = categories ,value = value, -c(ID, A_5, B_5, C_5)

    – Ronak Shah
    Jan 2 at 14:28


















0















I have a data composed of columns with following names : ID,A,B,C,A_5,B_5,C_5



I want to use gather() function such that ID,A_5,B_5 and C_5 are excluded. I know that if I wanted to exclude only ID, I could have written :



df %>% gather(key = categories ,value = value, -ID)


But I could not figure out the way to exclude multiple columns at once. I would appreciate a lot if somebody can tell me the correct syntax. So far I have tried and failed with:



df %>% gather(key = categories ,value = value, -vars(ID,A_5,B_5,C_5)
df %>% gather(key = categories ,value = value, -c(ID,A_5,B_5,C_5)
df %>% gather(key = categories ,value = value, -list(ID,A_5,B_5,C_5)
df %>% gather(key = categories ,value = value, -list(ID,A_5,B_5,C_5)


Also I have several other data frames on which I would like to perform the same operation. They all have different column lengths and for some, I need to exclude more than 10 columns. Thus, referring to these columns by their index would be much more convenient. Is this possible? If so, how can it be achieved?



I did not provide a reproducible example since this is merely a question of syntax. I hope it is ok.



Thanks in advance.










share|improve this question




















  • 1





    df %>% gather(key = categories ,value = value, -ID, -A_5, -B_5, -C_5) ? Or df %>% gather(key = categories ,value = value, -c(ID, A_5, B_5, C_5)

    – Ronak Shah
    Jan 2 at 14:28
















0












0








0








I have a data composed of columns with following names : ID,A,B,C,A_5,B_5,C_5



I want to use gather() function such that ID,A_5,B_5 and C_5 are excluded. I know that if I wanted to exclude only ID, I could have written :



df %>% gather(key = categories ,value = value, -ID)


But I could not figure out the way to exclude multiple columns at once. I would appreciate a lot if somebody can tell me the correct syntax. So far I have tried and failed with:



df %>% gather(key = categories ,value = value, -vars(ID,A_5,B_5,C_5)
df %>% gather(key = categories ,value = value, -c(ID,A_5,B_5,C_5)
df %>% gather(key = categories ,value = value, -list(ID,A_5,B_5,C_5)
df %>% gather(key = categories ,value = value, -list(ID,A_5,B_5,C_5)


Also I have several other data frames on which I would like to perform the same operation. They all have different column lengths and for some, I need to exclude more than 10 columns. Thus, referring to these columns by their index would be much more convenient. Is this possible? If so, how can it be achieved?



I did not provide a reproducible example since this is merely a question of syntax. I hope it is ok.



Thanks in advance.










share|improve this question
















I have a data composed of columns with following names : ID,A,B,C,A_5,B_5,C_5



I want to use gather() function such that ID,A_5,B_5 and C_5 are excluded. I know that if I wanted to exclude only ID, I could have written :



df %>% gather(key = categories ,value = value, -ID)


But I could not figure out the way to exclude multiple columns at once. I would appreciate a lot if somebody can tell me the correct syntax. So far I have tried and failed with:



df %>% gather(key = categories ,value = value, -vars(ID,A_5,B_5,C_5)
df %>% gather(key = categories ,value = value, -c(ID,A_5,B_5,C_5)
df %>% gather(key = categories ,value = value, -list(ID,A_5,B_5,C_5)
df %>% gather(key = categories ,value = value, -list(ID,A_5,B_5,C_5)


Also I have several other data frames on which I would like to perform the same operation. They all have different column lengths and for some, I need to exclude more than 10 columns. Thus, referring to these columns by their index would be much more convenient. Is this possible? If so, how can it be achieved?



I did not provide a reproducible example since this is merely a question of syntax. I hope it is ok.



Thanks in advance.







r dplyr






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 14:26







Elif Cansu Akoğuz

















asked Jan 2 at 14:19









Elif Cansu AkoğuzElif Cansu Akoğuz

368




368








  • 1





    df %>% gather(key = categories ,value = value, -ID, -A_5, -B_5, -C_5) ? Or df %>% gather(key = categories ,value = value, -c(ID, A_5, B_5, C_5)

    – Ronak Shah
    Jan 2 at 14:28
















  • 1





    df %>% gather(key = categories ,value = value, -ID, -A_5, -B_5, -C_5) ? Or df %>% gather(key = categories ,value = value, -c(ID, A_5, B_5, C_5)

    – Ronak Shah
    Jan 2 at 14:28










1




1





df %>% gather(key = categories ,value = value, -ID, -A_5, -B_5, -C_5) ? Or df %>% gather(key = categories ,value = value, -c(ID, A_5, B_5, C_5)

– Ronak Shah
Jan 2 at 14:28







df %>% gather(key = categories ,value = value, -ID, -A_5, -B_5, -C_5) ? Or df %>% gather(key = categories ,value = value, -c(ID, A_5, B_5, C_5)

– Ronak Shah
Jan 2 at 14:28














1 Answer
1






active

oldest

votes


















1














Your second attempt works for me:



df <- data.frame(Col1 = c(1, 2, 2, 3), Col2 = c(7, 9, 8, 1), ID = "ID", A_5 = "Test1", B_5 = "Test2", C_5 = "Test3")
df %>% gather(key = categories, value = value, -c(ID, A_5, B_5, C_5))


Output:



  ID   A_5   B_5   C_5 categories value
1 ID Test1 Test2 Test3 Col1 1
2 ID Test1 Test2 Test3 Col1 2
3 ID Test1 Test2 Test3 Col1 2
4 ID Test1 Test2 Test3 Col1 3
5 ID Test1 Test2 Test3 Col2 7
6 ID Test1 Test2 Test3 Col2 9
7 ID Test1 Test2 Test3 Col2 8
8 ID Test1 Test2 Test3 Col2 1


If this doesn't work for you for some reason, try updating your packages, or quoting the column names: -c("ID", "A_5", "B_5", "C_5")



Or, to exclude using the index of the columns (same output):



toExclude <- names(df)[3:6]
df %>% gather(key = categories, value = value, -toExclude)





share|improve this answer
























  • You're right. I was getting an error due to some other syntax error apparently, the second option indeed works. But the alternative with the indices is even better, thanks a lot.

    – Elif Cansu Akoğuz
    Jan 2 at 14:39











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


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54007971%2fhow-to-exclude-multiple-columns-in-gather-function%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









1














Your second attempt works for me:



df <- data.frame(Col1 = c(1, 2, 2, 3), Col2 = c(7, 9, 8, 1), ID = "ID", A_5 = "Test1", B_5 = "Test2", C_5 = "Test3")
df %>% gather(key = categories, value = value, -c(ID, A_5, B_5, C_5))


Output:



  ID   A_5   B_5   C_5 categories value
1 ID Test1 Test2 Test3 Col1 1
2 ID Test1 Test2 Test3 Col1 2
3 ID Test1 Test2 Test3 Col1 2
4 ID Test1 Test2 Test3 Col1 3
5 ID Test1 Test2 Test3 Col2 7
6 ID Test1 Test2 Test3 Col2 9
7 ID Test1 Test2 Test3 Col2 8
8 ID Test1 Test2 Test3 Col2 1


If this doesn't work for you for some reason, try updating your packages, or quoting the column names: -c("ID", "A_5", "B_5", "C_5")



Or, to exclude using the index of the columns (same output):



toExclude <- names(df)[3:6]
df %>% gather(key = categories, value = value, -toExclude)





share|improve this answer
























  • You're right. I was getting an error due to some other syntax error apparently, the second option indeed works. But the alternative with the indices is even better, thanks a lot.

    – Elif Cansu Akoğuz
    Jan 2 at 14:39
















1














Your second attempt works for me:



df <- data.frame(Col1 = c(1, 2, 2, 3), Col2 = c(7, 9, 8, 1), ID = "ID", A_5 = "Test1", B_5 = "Test2", C_5 = "Test3")
df %>% gather(key = categories, value = value, -c(ID, A_5, B_5, C_5))


Output:



  ID   A_5   B_5   C_5 categories value
1 ID Test1 Test2 Test3 Col1 1
2 ID Test1 Test2 Test3 Col1 2
3 ID Test1 Test2 Test3 Col1 2
4 ID Test1 Test2 Test3 Col1 3
5 ID Test1 Test2 Test3 Col2 7
6 ID Test1 Test2 Test3 Col2 9
7 ID Test1 Test2 Test3 Col2 8
8 ID Test1 Test2 Test3 Col2 1


If this doesn't work for you for some reason, try updating your packages, or quoting the column names: -c("ID", "A_5", "B_5", "C_5")



Or, to exclude using the index of the columns (same output):



toExclude <- names(df)[3:6]
df %>% gather(key = categories, value = value, -toExclude)





share|improve this answer
























  • You're right. I was getting an error due to some other syntax error apparently, the second option indeed works. But the alternative with the indices is even better, thanks a lot.

    – Elif Cansu Akoğuz
    Jan 2 at 14:39














1












1








1







Your second attempt works for me:



df <- data.frame(Col1 = c(1, 2, 2, 3), Col2 = c(7, 9, 8, 1), ID = "ID", A_5 = "Test1", B_5 = "Test2", C_5 = "Test3")
df %>% gather(key = categories, value = value, -c(ID, A_5, B_5, C_5))


Output:



  ID   A_5   B_5   C_5 categories value
1 ID Test1 Test2 Test3 Col1 1
2 ID Test1 Test2 Test3 Col1 2
3 ID Test1 Test2 Test3 Col1 2
4 ID Test1 Test2 Test3 Col1 3
5 ID Test1 Test2 Test3 Col2 7
6 ID Test1 Test2 Test3 Col2 9
7 ID Test1 Test2 Test3 Col2 8
8 ID Test1 Test2 Test3 Col2 1


If this doesn't work for you for some reason, try updating your packages, or quoting the column names: -c("ID", "A_5", "B_5", "C_5")



Or, to exclude using the index of the columns (same output):



toExclude <- names(df)[3:6]
df %>% gather(key = categories, value = value, -toExclude)





share|improve this answer













Your second attempt works for me:



df <- data.frame(Col1 = c(1, 2, 2, 3), Col2 = c(7, 9, 8, 1), ID = "ID", A_5 = "Test1", B_5 = "Test2", C_5 = "Test3")
df %>% gather(key = categories, value = value, -c(ID, A_5, B_5, C_5))


Output:



  ID   A_5   B_5   C_5 categories value
1 ID Test1 Test2 Test3 Col1 1
2 ID Test1 Test2 Test3 Col1 2
3 ID Test1 Test2 Test3 Col1 2
4 ID Test1 Test2 Test3 Col1 3
5 ID Test1 Test2 Test3 Col2 7
6 ID Test1 Test2 Test3 Col2 9
7 ID Test1 Test2 Test3 Col2 8
8 ID Test1 Test2 Test3 Col2 1


If this doesn't work for you for some reason, try updating your packages, or quoting the column names: -c("ID", "A_5", "B_5", "C_5")



Or, to exclude using the index of the columns (same output):



toExclude <- names(df)[3:6]
df %>% gather(key = categories, value = value, -toExclude)






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 2 at 14:31









Bram Van RensbergenBram Van Rensbergen

32516




32516













  • You're right. I was getting an error due to some other syntax error apparently, the second option indeed works. But the alternative with the indices is even better, thanks a lot.

    – Elif Cansu Akoğuz
    Jan 2 at 14:39



















  • You're right. I was getting an error due to some other syntax error apparently, the second option indeed works. But the alternative with the indices is even better, thanks a lot.

    – Elif Cansu Akoğuz
    Jan 2 at 14:39

















You're right. I was getting an error due to some other syntax error apparently, the second option indeed works. But the alternative with the indices is even better, thanks a lot.

– Elif Cansu Akoğuz
Jan 2 at 14:39





You're right. I was getting an error due to some other syntax error apparently, the second option indeed works. But the alternative with the indices is even better, thanks a lot.

– Elif Cansu Akoğuz
Jan 2 at 14:39




















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54007971%2fhow-to-exclude-multiple-columns-in-gather-function%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

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

How to fix TextFormField cause rebuild widget in Flutter