Renaming columns not containing specific suffix
I want to rename columns in dataframe 'SOURCE_SUBSET' not contain the substring "_dich" with the suffix '_dich' and all other columns to have the original name.
exmaple:
GGADA202_dich should remain the same
GGADA203 should become GGADA203_dich
This is what I have so far. It is not working correctly: GGADA202_dich becomes GGADA202_dich_dich_dich
ifelse(grepl("_dich", colnames(SOURCE_SUBSET), fixed=TRUE),
colnames(SOURCE_SUBSET) <- paste(colnames(SOURCE_SUBSET), "dich", sep = "_"),colnames(SOURCE_SUBSET))
r grepl
|
show 1 more comment
I want to rename columns in dataframe 'SOURCE_SUBSET' not contain the substring "_dich" with the suffix '_dich' and all other columns to have the original name.
exmaple:
GGADA202_dich should remain the same
GGADA203 should become GGADA203_dich
This is what I have so far. It is not working correctly: GGADA202_dich becomes GGADA202_dich_dich_dich
ifelse(grepl("_dich", colnames(SOURCE_SUBSET), fixed=TRUE),
colnames(SOURCE_SUBSET) <- paste(colnames(SOURCE_SUBSET), "dich", sep = "_"),colnames(SOURCE_SUBSET))
r grepl
1
Trycolnames(SOURCE_SUBSET) <- sub("^(?!.*_dich)(.*)", "\1_dich", colnames(SOURCE_SUBSET), perl=TRUE)
– Wiktor Stribiżew
Nov 23 '18 at 12:24
it renames all with the suffix...
– Mark Henry
Nov 23 '18 at 12:31
The"^(?!.*_dich)(.*)"pattern matches strings that do not contain_dich. Make sure you useperl=TRUE. Those that do not match remain as is. See this demo.
– Wiktor Stribiżew
Nov 23 '18 at 12:33
1
See ideone.com/3PjzjA
– Wiktor Stribiżew
Nov 23 '18 at 12:37
Thank you Wiktor!
– Mark Henry
Nov 23 '18 at 12:43
|
show 1 more comment
I want to rename columns in dataframe 'SOURCE_SUBSET' not contain the substring "_dich" with the suffix '_dich' and all other columns to have the original name.
exmaple:
GGADA202_dich should remain the same
GGADA203 should become GGADA203_dich
This is what I have so far. It is not working correctly: GGADA202_dich becomes GGADA202_dich_dich_dich
ifelse(grepl("_dich", colnames(SOURCE_SUBSET), fixed=TRUE),
colnames(SOURCE_SUBSET) <- paste(colnames(SOURCE_SUBSET), "dich", sep = "_"),colnames(SOURCE_SUBSET))
r grepl
I want to rename columns in dataframe 'SOURCE_SUBSET' not contain the substring "_dich" with the suffix '_dich' and all other columns to have the original name.
exmaple:
GGADA202_dich should remain the same
GGADA203 should become GGADA203_dich
This is what I have so far. It is not working correctly: GGADA202_dich becomes GGADA202_dich_dich_dich
ifelse(grepl("_dich", colnames(SOURCE_SUBSET), fixed=TRUE),
colnames(SOURCE_SUBSET) <- paste(colnames(SOURCE_SUBSET), "dich", sep = "_"),colnames(SOURCE_SUBSET))
r grepl
r grepl
asked Nov 23 '18 at 12:21
Mark HenryMark Henry
1,12952947
1,12952947
1
Trycolnames(SOURCE_SUBSET) <- sub("^(?!.*_dich)(.*)", "\1_dich", colnames(SOURCE_SUBSET), perl=TRUE)
– Wiktor Stribiżew
Nov 23 '18 at 12:24
it renames all with the suffix...
– Mark Henry
Nov 23 '18 at 12:31
The"^(?!.*_dich)(.*)"pattern matches strings that do not contain_dich. Make sure you useperl=TRUE. Those that do not match remain as is. See this demo.
– Wiktor Stribiżew
Nov 23 '18 at 12:33
1
See ideone.com/3PjzjA
– Wiktor Stribiżew
Nov 23 '18 at 12:37
Thank you Wiktor!
– Mark Henry
Nov 23 '18 at 12:43
|
show 1 more comment
1
Trycolnames(SOURCE_SUBSET) <- sub("^(?!.*_dich)(.*)", "\1_dich", colnames(SOURCE_SUBSET), perl=TRUE)
– Wiktor Stribiżew
Nov 23 '18 at 12:24
it renames all with the suffix...
– Mark Henry
Nov 23 '18 at 12:31
The"^(?!.*_dich)(.*)"pattern matches strings that do not contain_dich. Make sure you useperl=TRUE. Those that do not match remain as is. See this demo.
– Wiktor Stribiżew
Nov 23 '18 at 12:33
1
See ideone.com/3PjzjA
– Wiktor Stribiżew
Nov 23 '18 at 12:37
Thank you Wiktor!
– Mark Henry
Nov 23 '18 at 12:43
1
1
Try
colnames(SOURCE_SUBSET) <- sub("^(?!.*_dich)(.*)", "\1_dich", colnames(SOURCE_SUBSET), perl=TRUE)– Wiktor Stribiżew
Nov 23 '18 at 12:24
Try
colnames(SOURCE_SUBSET) <- sub("^(?!.*_dich)(.*)", "\1_dich", colnames(SOURCE_SUBSET), perl=TRUE)– Wiktor Stribiżew
Nov 23 '18 at 12:24
it renames all with the suffix...
– Mark Henry
Nov 23 '18 at 12:31
it renames all with the suffix...
– Mark Henry
Nov 23 '18 at 12:31
The
"^(?!.*_dich)(.*)" pattern matches strings that do not contain _dich. Make sure you use perl=TRUE. Those that do not match remain as is. See this demo.– Wiktor Stribiżew
Nov 23 '18 at 12:33
The
"^(?!.*_dich)(.*)" pattern matches strings that do not contain _dich. Make sure you use perl=TRUE. Those that do not match remain as is. See this demo.– Wiktor Stribiżew
Nov 23 '18 at 12:33
1
1
See ideone.com/3PjzjA
– Wiktor Stribiżew
Nov 23 '18 at 12:37
See ideone.com/3PjzjA
– Wiktor Stribiżew
Nov 23 '18 at 12:37
Thank you Wiktor!
– Mark Henry
Nov 23 '18 at 12:43
Thank you Wiktor!
– Mark Henry
Nov 23 '18 at 12:43
|
show 1 more comment
2 Answers
2
active
oldest
votes
You may use
colnames(SOURCE_SUBSET) <- sub("^(?!.*_dich)(.*)", "\1_dich", colnames(SOURCE_SUBSET), perl=TRUE)
See the R demo online and the regex demo.
The ^(?!.*_dich)(.*) pattern matches strings that do not contain _dich. Make sure you use perl=TRUE as lookaheads are not supported by the default TRE library. Those names that do not match will remain as is.
Pattern details
^- start of string
(?!.*_dich)- no_dichis allowed after any 0+ chars other than line break chars as many as possible from the start of the string
(.*)- grabs into Group 1 (this text is inserted back into the result using'\1'in the replacement pattern) any 0+ chars other than line break chars as many as possible.
add a comment |
tidyverse approach
library( tidyverse )
#if a column name does not contain the string"_dich", add the suffix "_dich"
df %>% rename_at( vars( -contains( "_dich" ) ), funs( paste0(., "_dich") ) )
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%2f53446654%2frenaming-columns-not-containing-specific-suffix%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You may use
colnames(SOURCE_SUBSET) <- sub("^(?!.*_dich)(.*)", "\1_dich", colnames(SOURCE_SUBSET), perl=TRUE)
See the R demo online and the regex demo.
The ^(?!.*_dich)(.*) pattern matches strings that do not contain _dich. Make sure you use perl=TRUE as lookaheads are not supported by the default TRE library. Those names that do not match will remain as is.
Pattern details
^- start of string
(?!.*_dich)- no_dichis allowed after any 0+ chars other than line break chars as many as possible from the start of the string
(.*)- grabs into Group 1 (this text is inserted back into the result using'\1'in the replacement pattern) any 0+ chars other than line break chars as many as possible.
add a comment |
You may use
colnames(SOURCE_SUBSET) <- sub("^(?!.*_dich)(.*)", "\1_dich", colnames(SOURCE_SUBSET), perl=TRUE)
See the R demo online and the regex demo.
The ^(?!.*_dich)(.*) pattern matches strings that do not contain _dich. Make sure you use perl=TRUE as lookaheads are not supported by the default TRE library. Those names that do not match will remain as is.
Pattern details
^- start of string
(?!.*_dich)- no_dichis allowed after any 0+ chars other than line break chars as many as possible from the start of the string
(.*)- grabs into Group 1 (this text is inserted back into the result using'\1'in the replacement pattern) any 0+ chars other than line break chars as many as possible.
add a comment |
You may use
colnames(SOURCE_SUBSET) <- sub("^(?!.*_dich)(.*)", "\1_dich", colnames(SOURCE_SUBSET), perl=TRUE)
See the R demo online and the regex demo.
The ^(?!.*_dich)(.*) pattern matches strings that do not contain _dich. Make sure you use perl=TRUE as lookaheads are not supported by the default TRE library. Those names that do not match will remain as is.
Pattern details
^- start of string
(?!.*_dich)- no_dichis allowed after any 0+ chars other than line break chars as many as possible from the start of the string
(.*)- grabs into Group 1 (this text is inserted back into the result using'\1'in the replacement pattern) any 0+ chars other than line break chars as many as possible.
You may use
colnames(SOURCE_SUBSET) <- sub("^(?!.*_dich)(.*)", "\1_dich", colnames(SOURCE_SUBSET), perl=TRUE)
See the R demo online and the regex demo.
The ^(?!.*_dich)(.*) pattern matches strings that do not contain _dich. Make sure you use perl=TRUE as lookaheads are not supported by the default TRE library. Those names that do not match will remain as is.
Pattern details
^- start of string
(?!.*_dich)- no_dichis allowed after any 0+ chars other than line break chars as many as possible from the start of the string
(.*)- grabs into Group 1 (this text is inserted back into the result using'\1'in the replacement pattern) any 0+ chars other than line break chars as many as possible.
answered Nov 23 '18 at 12:45
Wiktor StribiżewWiktor Stribiżew
328k16147227
328k16147227
add a comment |
add a comment |
tidyverse approach
library( tidyverse )
#if a column name does not contain the string"_dich", add the suffix "_dich"
df %>% rename_at( vars( -contains( "_dich" ) ), funs( paste0(., "_dich") ) )
add a comment |
tidyverse approach
library( tidyverse )
#if a column name does not contain the string"_dich", add the suffix "_dich"
df %>% rename_at( vars( -contains( "_dich" ) ), funs( paste0(., "_dich") ) )
add a comment |
tidyverse approach
library( tidyverse )
#if a column name does not contain the string"_dich", add the suffix "_dich"
df %>% rename_at( vars( -contains( "_dich" ) ), funs( paste0(., "_dich") ) )
tidyverse approach
library( tidyverse )
#if a column name does not contain the string"_dich", add the suffix "_dich"
df %>% rename_at( vars( -contains( "_dich" ) ), funs( paste0(., "_dich") ) )
answered Nov 23 '18 at 14:15
WimpelWimpel
6,302323
6,302323
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%2f53446654%2frenaming-columns-not-containing-specific-suffix%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
Try
colnames(SOURCE_SUBSET) <- sub("^(?!.*_dich)(.*)", "\1_dich", colnames(SOURCE_SUBSET), perl=TRUE)– Wiktor Stribiżew
Nov 23 '18 at 12:24
it renames all with the suffix...
– Mark Henry
Nov 23 '18 at 12:31
The
"^(?!.*_dich)(.*)"pattern matches strings that do not contain_dich. Make sure you useperl=TRUE. Those that do not match remain as is. See this demo.– Wiktor Stribiżew
Nov 23 '18 at 12:33
1
See ideone.com/3PjzjA
– Wiktor Stribiżew
Nov 23 '18 at 12:37
Thank you Wiktor!
– Mark Henry
Nov 23 '18 at 12:43