Concatenate columns based on separator in order
I have 2 columns that I would like to concatenate into a single column. Each column has separators ">" and each row either has the same number of separators or none at all.
col1 <- c('A > B > C > B > B > A > B > A > C',
'A > B',
'A > C > B',
'A',
'A > B > B > C')
col2 <- c('D > E > E > E > F > F > D > F > E',
'D > F',
'F > F > E',
'E',
'E > F > D > D')
df <- data.frame(col1, col2)
I would like to end up with a data frame like this:
col1
A|D > B|E > C|E > B|E > B|F > A|F > B|D > A|F > C|E
A|D > B|F
A|F > C|F > B|E
A|E
A|E > B|F > B|D > C|D
r
add a comment |
I have 2 columns that I would like to concatenate into a single column. Each column has separators ">" and each row either has the same number of separators or none at all.
col1 <- c('A > B > C > B > B > A > B > A > C',
'A > B',
'A > C > B',
'A',
'A > B > B > C')
col2 <- c('D > E > E > E > F > F > D > F > E',
'D > F',
'F > F > E',
'E',
'E > F > D > D')
df <- data.frame(col1, col2)
I would like to end up with a data frame like this:
col1
A|D > B|E > C|E > B|E > B|F > A|F > B|D > A|F > C|E
A|D > B|F
A|F > C|F > B|E
A|E
A|E > B|F > B|D > C|D
r
1
My original try comprised of calculating the max number of separators and using that in a split function. df$pathlength <- stringr::str_count(df$col1, ">") + 1 stringr::str_split_fixed(df$col1,">",max(df$pathlength)) stringr::str_split_fixed(df$col2,">",max(df$pathlength)) This gives 2 data frames with equal number of columns, however, I am not sure how to join the 2 to keep the relationship between each original combination.
– ericbrownaustin
Nov 19 '18 at 18:27
add a comment |
I have 2 columns that I would like to concatenate into a single column. Each column has separators ">" and each row either has the same number of separators or none at all.
col1 <- c('A > B > C > B > B > A > B > A > C',
'A > B',
'A > C > B',
'A',
'A > B > B > C')
col2 <- c('D > E > E > E > F > F > D > F > E',
'D > F',
'F > F > E',
'E',
'E > F > D > D')
df <- data.frame(col1, col2)
I would like to end up with a data frame like this:
col1
A|D > B|E > C|E > B|E > B|F > A|F > B|D > A|F > C|E
A|D > B|F
A|F > C|F > B|E
A|E
A|E > B|F > B|D > C|D
r
I have 2 columns that I would like to concatenate into a single column. Each column has separators ">" and each row either has the same number of separators or none at all.
col1 <- c('A > B > C > B > B > A > B > A > C',
'A > B',
'A > C > B',
'A',
'A > B > B > C')
col2 <- c('D > E > E > E > F > F > D > F > E',
'D > F',
'F > F > E',
'E',
'E > F > D > D')
df <- data.frame(col1, col2)
I would like to end up with a data frame like this:
col1
A|D > B|E > C|E > B|E > B|F > A|F > B|D > A|F > C|E
A|D > B|F
A|F > C|F > B|E
A|E
A|E > B|F > B|D > C|D
r
r
asked Nov 19 '18 at 18:09
ericbrownaustinericbrownaustin
309316
309316
1
My original try comprised of calculating the max number of separators and using that in a split function. df$pathlength <- stringr::str_count(df$col1, ">") + 1 stringr::str_split_fixed(df$col1,">",max(df$pathlength)) stringr::str_split_fixed(df$col2,">",max(df$pathlength)) This gives 2 data frames with equal number of columns, however, I am not sure how to join the 2 to keep the relationship between each original combination.
– ericbrownaustin
Nov 19 '18 at 18:27
add a comment |
1
My original try comprised of calculating the max number of separators and using that in a split function. df$pathlength <- stringr::str_count(df$col1, ">") + 1 stringr::str_split_fixed(df$col1,">",max(df$pathlength)) stringr::str_split_fixed(df$col2,">",max(df$pathlength)) This gives 2 data frames with equal number of columns, however, I am not sure how to join the 2 to keep the relationship between each original combination.
– ericbrownaustin
Nov 19 '18 at 18:27
1
1
My original try comprised of calculating the max number of separators and using that in a split function. df$pathlength <- stringr::str_count(df$col1, ">") + 1 stringr::str_split_fixed(df$col1,">",max(df$pathlength)) stringr::str_split_fixed(df$col2,">",max(df$pathlength)) This gives 2 data frames with equal number of columns, however, I am not sure how to join the 2 to keep the relationship between each original combination.
– ericbrownaustin
Nov 19 '18 at 18:27
My original try comprised of calculating the max number of separators and using that in a split function. df$pathlength <- stringr::str_count(df$col1, ">") + 1 stringr::str_split_fixed(df$col1,">",max(df$pathlength)) stringr::str_split_fixed(df$col2,">",max(df$pathlength)) This gives 2 data frames with equal number of columns, however, I am not sure how to join the 2 to keep the relationship between each original combination.
– ericbrownaustin
Nov 19 '18 at 18:27
add a comment |
1 Answer
1
active
oldest
votes
You are looking for:
mapply(paste,strsplit(col1,' > '),strsplit(col2,' > '),sep='|',collapse=' > ')
[1] "A|D > B|E > C|E > B|E > B|F > A|F > B|D > A|F > C|E"
[2] "A|D > B|F"
[3] "A|F > C|F > B|E"
[4] "A|E"
[5] "A|E > B|F > B|D > C|D"
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%2f53380384%2fconcatenate-columns-based-on-separator-in-order%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
You are looking for:
mapply(paste,strsplit(col1,' > '),strsplit(col2,' > '),sep='|',collapse=' > ')
[1] "A|D > B|E > C|E > B|E > B|F > A|F > B|D > A|F > C|E"
[2] "A|D > B|F"
[3] "A|F > C|F > B|E"
[4] "A|E"
[5] "A|E > B|F > B|D > C|D"
add a comment |
You are looking for:
mapply(paste,strsplit(col1,' > '),strsplit(col2,' > '),sep='|',collapse=' > ')
[1] "A|D > B|E > C|E > B|E > B|F > A|F > B|D > A|F > C|E"
[2] "A|D > B|F"
[3] "A|F > C|F > B|E"
[4] "A|E"
[5] "A|E > B|F > B|D > C|D"
add a comment |
You are looking for:
mapply(paste,strsplit(col1,' > '),strsplit(col2,' > '),sep='|',collapse=' > ')
[1] "A|D > B|E > C|E > B|E > B|F > A|F > B|D > A|F > C|E"
[2] "A|D > B|F"
[3] "A|F > C|F > B|E"
[4] "A|E"
[5] "A|E > B|F > B|D > C|D"
You are looking for:
mapply(paste,strsplit(col1,' > '),strsplit(col2,' > '),sep='|',collapse=' > ')
[1] "A|D > B|E > C|E > B|E > B|F > A|F > B|D > A|F > C|E"
[2] "A|D > B|F"
[3] "A|F > C|F > B|E"
[4] "A|E"
[5] "A|E > B|F > B|D > C|D"
answered Nov 19 '18 at 18:19
OnyambuOnyambu
15.8k1521
15.8k1521
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%2f53380384%2fconcatenate-columns-based-on-separator-in-order%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
My original try comprised of calculating the max number of separators and using that in a split function. df$pathlength <- stringr::str_count(df$col1, ">") + 1 stringr::str_split_fixed(df$col1,">",max(df$pathlength)) stringr::str_split_fixed(df$col2,">",max(df$pathlength)) This gives 2 data frames with equal number of columns, however, I am not sure how to join the 2 to keep the relationship between each original combination.
– ericbrownaustin
Nov 19 '18 at 18:27