Merge two tables in R









up vote
-1
down vote

favorite












I have two tables, which have the same column names, number of rows and columns:



ID Q1 Q2
1 aa bb
2 cc 12
3 q z


ID Q1 Q2
1 A B
2 p l_
3 23 r


I would like to have in the next table merged those above columns with both context and separated by n, like this:



 ID Q1 Q2
1 aa bb
A B
2 cc 12
p l_
3 q z
23 r


I have tried, merge, rbind, cbind, but I didn't get any good results.
Any ides how to do this?



Required result:
enter image description here










share|improve this question























  • What determines which of the two rows actually gets assigned an ID value, and which row does not?
    – Tim Biegeleisen
    yesterday










  • What do you want in the blank spaces where ID was duplicated? Do you want ID to be a character column and have an empty string there? You can't have empty numeric values - NA is another possibility,
    – Spacedman
    yesterday










  • short answer - rbind them and then fixup any of ID that are duplicated(d$ID)
    – Spacedman
    yesterday










  • Are you trying to get some sort of printed output?
    – Spacedman
    yesterday










  • I would like to get the table with the same number of rows, so for example for ID: 1, I get one merged cell Q1: contained: 'aa' and A etc.
    – K.M
    yesterday














up vote
-1
down vote

favorite












I have two tables, which have the same column names, number of rows and columns:



ID Q1 Q2
1 aa bb
2 cc 12
3 q z


ID Q1 Q2
1 A B
2 p l_
3 23 r


I would like to have in the next table merged those above columns with both context and separated by n, like this:



 ID Q1 Q2
1 aa bb
A B
2 cc 12
p l_
3 q z
23 r


I have tried, merge, rbind, cbind, but I didn't get any good results.
Any ides how to do this?



Required result:
enter image description here










share|improve this question























  • What determines which of the two rows actually gets assigned an ID value, and which row does not?
    – Tim Biegeleisen
    yesterday










  • What do you want in the blank spaces where ID was duplicated? Do you want ID to be a character column and have an empty string there? You can't have empty numeric values - NA is another possibility,
    – Spacedman
    yesterday










  • short answer - rbind them and then fixup any of ID that are duplicated(d$ID)
    – Spacedman
    yesterday










  • Are you trying to get some sort of printed output?
    – Spacedman
    yesterday










  • I would like to get the table with the same number of rows, so for example for ID: 1, I get one merged cell Q1: contained: 'aa' and A etc.
    – K.M
    yesterday












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I have two tables, which have the same column names, number of rows and columns:



ID Q1 Q2
1 aa bb
2 cc 12
3 q z


ID Q1 Q2
1 A B
2 p l_
3 23 r


I would like to have in the next table merged those above columns with both context and separated by n, like this:



 ID Q1 Q2
1 aa bb
A B
2 cc 12
p l_
3 q z
23 r


I have tried, merge, rbind, cbind, but I didn't get any good results.
Any ides how to do this?



Required result:
enter image description here










share|improve this question















I have two tables, which have the same column names, number of rows and columns:



ID Q1 Q2
1 aa bb
2 cc 12
3 q z


ID Q1 Q2
1 A B
2 p l_
3 23 r


I would like to have in the next table merged those above columns with both context and separated by n, like this:



 ID Q1 Q2
1 aa bb
A B
2 cc 12
p l_
3 q z
23 r


I have tried, merge, rbind, cbind, but I didn't get any good results.
Any ides how to do this?



Required result:
enter image description here







r merge rbind






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday

























asked yesterday









K.M

93




93











  • What determines which of the two rows actually gets assigned an ID value, and which row does not?
    – Tim Biegeleisen
    yesterday










  • What do you want in the blank spaces where ID was duplicated? Do you want ID to be a character column and have an empty string there? You can't have empty numeric values - NA is another possibility,
    – Spacedman
    yesterday










  • short answer - rbind them and then fixup any of ID that are duplicated(d$ID)
    – Spacedman
    yesterday










  • Are you trying to get some sort of printed output?
    – Spacedman
    yesterday










  • I would like to get the table with the same number of rows, so for example for ID: 1, I get one merged cell Q1: contained: 'aa' and A etc.
    – K.M
    yesterday
















  • What determines which of the two rows actually gets assigned an ID value, and which row does not?
    – Tim Biegeleisen
    yesterday










  • What do you want in the blank spaces where ID was duplicated? Do you want ID to be a character column and have an empty string there? You can't have empty numeric values - NA is another possibility,
    – Spacedman
    yesterday










  • short answer - rbind them and then fixup any of ID that are duplicated(d$ID)
    – Spacedman
    yesterday










  • Are you trying to get some sort of printed output?
    – Spacedman
    yesterday










  • I would like to get the table with the same number of rows, so for example for ID: 1, I get one merged cell Q1: contained: 'aa' and A etc.
    – K.M
    yesterday















What determines which of the two rows actually gets assigned an ID value, and which row does not?
– Tim Biegeleisen
yesterday




What determines which of the two rows actually gets assigned an ID value, and which row does not?
– Tim Biegeleisen
yesterday












What do you want in the blank spaces where ID was duplicated? Do you want ID to be a character column and have an empty string there? You can't have empty numeric values - NA is another possibility,
– Spacedman
yesterday




What do you want in the blank spaces where ID was duplicated? Do you want ID to be a character column and have an empty string there? You can't have empty numeric values - NA is another possibility,
– Spacedman
yesterday












short answer - rbind them and then fixup any of ID that are duplicated(d$ID)
– Spacedman
yesterday




short answer - rbind them and then fixup any of ID that are duplicated(d$ID)
– Spacedman
yesterday












Are you trying to get some sort of printed output?
– Spacedman
yesterday




Are you trying to get some sort of printed output?
– Spacedman
yesterday












I would like to get the table with the same number of rows, so for example for ID: 1, I get one merged cell Q1: contained: 'aa' and A etc.
– K.M
yesterday




I would like to get the table with the same number of rows, so for example for ID: 1, I get one merged cell Q1: contained: 'aa' and A etc.
– K.M
yesterday












1 Answer
1






active

oldest

votes

















up vote
0
down vote













Sample data:



> d1
ID Q1 Q2
1 1 aa bb
2 2 cc 12
3 3 q z
> d2
ID Q1 Q2
1 1 A B
2 2 p l_
3 3 23 r


row-bind:



> d = rbind(d1, d2)


sort by ID:



> d = d[order(d$ID),]


replace duplicated ID by something, eg NA:



> d$ID[duplicated(d$ID)]=NA


job done:



> d
ID Q1 Q2
1 1 aa bb
4 NA A B
2 2 cc 12
5 NA p l_
3 3 q z
6 NA 23 r
>


If you want to create merged cells with n separating them, then this:



> cbind(ID=d1$ID, setNames(do.call(cbind.data.frame,lapply(c("Q1","Q2"), function(n)paste(d1[[n]],d2[[n]],sep="n"))),c("Q1","Q2")))
ID Q1 Q2
1 1 aanA bbnB
2 2 ccnp 12nl_
3 3 qn23 znr





share|improve this answer






















  • But I would like to keep the number of rows. Attached image shows the required result
    – K.M
    yesterday










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',
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%2f53224361%2fmerge-two-tables-in-r%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













Sample data:



> d1
ID Q1 Q2
1 1 aa bb
2 2 cc 12
3 3 q z
> d2
ID Q1 Q2
1 1 A B
2 2 p l_
3 3 23 r


row-bind:



> d = rbind(d1, d2)


sort by ID:



> d = d[order(d$ID),]


replace duplicated ID by something, eg NA:



> d$ID[duplicated(d$ID)]=NA


job done:



> d
ID Q1 Q2
1 1 aa bb
4 NA A B
2 2 cc 12
5 NA p l_
3 3 q z
6 NA 23 r
>


If you want to create merged cells with n separating them, then this:



> cbind(ID=d1$ID, setNames(do.call(cbind.data.frame,lapply(c("Q1","Q2"), function(n)paste(d1[[n]],d2[[n]],sep="n"))),c("Q1","Q2")))
ID Q1 Q2
1 1 aanA bbnB
2 2 ccnp 12nl_
3 3 qn23 znr





share|improve this answer






















  • But I would like to keep the number of rows. Attached image shows the required result
    – K.M
    yesterday














up vote
0
down vote













Sample data:



> d1
ID Q1 Q2
1 1 aa bb
2 2 cc 12
3 3 q z
> d2
ID Q1 Q2
1 1 A B
2 2 p l_
3 3 23 r


row-bind:



> d = rbind(d1, d2)


sort by ID:



> d = d[order(d$ID),]


replace duplicated ID by something, eg NA:



> d$ID[duplicated(d$ID)]=NA


job done:



> d
ID Q1 Q2
1 1 aa bb
4 NA A B
2 2 cc 12
5 NA p l_
3 3 q z
6 NA 23 r
>


If you want to create merged cells with n separating them, then this:



> cbind(ID=d1$ID, setNames(do.call(cbind.data.frame,lapply(c("Q1","Q2"), function(n)paste(d1[[n]],d2[[n]],sep="n"))),c("Q1","Q2")))
ID Q1 Q2
1 1 aanA bbnB
2 2 ccnp 12nl_
3 3 qn23 znr





share|improve this answer






















  • But I would like to keep the number of rows. Attached image shows the required result
    – K.M
    yesterday












up vote
0
down vote










up vote
0
down vote









Sample data:



> d1
ID Q1 Q2
1 1 aa bb
2 2 cc 12
3 3 q z
> d2
ID Q1 Q2
1 1 A B
2 2 p l_
3 3 23 r


row-bind:



> d = rbind(d1, d2)


sort by ID:



> d = d[order(d$ID),]


replace duplicated ID by something, eg NA:



> d$ID[duplicated(d$ID)]=NA


job done:



> d
ID Q1 Q2
1 1 aa bb
4 NA A B
2 2 cc 12
5 NA p l_
3 3 q z
6 NA 23 r
>


If you want to create merged cells with n separating them, then this:



> cbind(ID=d1$ID, setNames(do.call(cbind.data.frame,lapply(c("Q1","Q2"), function(n)paste(d1[[n]],d2[[n]],sep="n"))),c("Q1","Q2")))
ID Q1 Q2
1 1 aanA bbnB
2 2 ccnp 12nl_
3 3 qn23 znr





share|improve this answer














Sample data:



> d1
ID Q1 Q2
1 1 aa bb
2 2 cc 12
3 3 q z
> d2
ID Q1 Q2
1 1 A B
2 2 p l_
3 3 23 r


row-bind:



> d = rbind(d1, d2)


sort by ID:



> d = d[order(d$ID),]


replace duplicated ID by something, eg NA:



> d$ID[duplicated(d$ID)]=NA


job done:



> d
ID Q1 Q2
1 1 aa bb
4 NA A B
2 2 cc 12
5 NA p l_
3 3 q z
6 NA 23 r
>


If you want to create merged cells with n separating them, then this:



> cbind(ID=d1$ID, setNames(do.call(cbind.data.frame,lapply(c("Q1","Q2"), function(n)paste(d1[[n]],d2[[n]],sep="n"))),c("Q1","Q2")))
ID Q1 Q2
1 1 aanA bbnB
2 2 ccnp 12nl_
3 3 qn23 znr






share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday

























answered yesterday









Spacedman

71.1k990164




71.1k990164











  • But I would like to keep the number of rows. Attached image shows the required result
    – K.M
    yesterday
















  • But I would like to keep the number of rows. Attached image shows the required result
    – K.M
    yesterday















But I would like to keep the number of rows. Attached image shows the required result
– K.M
yesterday




But I would like to keep the number of rows. Attached image shows the required result
– K.M
yesterday

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224361%2fmerge-two-tables-in-r%23new-answer', 'question_page');

);

Post as a guest














































































Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo