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
r merge rbind
|
show 1 more comment
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
r merge rbind
What determines which of the two rows actually gets assigned anID
value, and which row does not?
– Tim Biegeleisen
Nov 9 at 10:57
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
Nov 9 at 10:57
short answer - rbind them and then fixup any of ID that areduplicated(d$ID)
– Spacedman
Nov 9 at 10:58
Are you trying to get some sort of printed output?
– Spacedman
Nov 9 at 11:02
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
Nov 9 at 11:04
|
show 1 more comment
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
r merge rbind
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
r merge rbind
edited Nov 9 at 11:13
asked Nov 9 at 10:55
K.M
93
93
What determines which of the two rows actually gets assigned anID
value, and which row does not?
– Tim Biegeleisen
Nov 9 at 10:57
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
Nov 9 at 10:57
short answer - rbind them and then fixup any of ID that areduplicated(d$ID)
– Spacedman
Nov 9 at 10:58
Are you trying to get some sort of printed output?
– Spacedman
Nov 9 at 11:02
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
Nov 9 at 11:04
|
show 1 more comment
What determines which of the two rows actually gets assigned anID
value, and which row does not?
– Tim Biegeleisen
Nov 9 at 10:57
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
Nov 9 at 10:57
short answer - rbind them and then fixup any of ID that areduplicated(d$ID)
– Spacedman
Nov 9 at 10:58
Are you trying to get some sort of printed output?
– Spacedman
Nov 9 at 11:02
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
Nov 9 at 11:04
What determines which of the two rows actually gets assigned an
ID
value, and which row does not?– Tim Biegeleisen
Nov 9 at 10:57
What determines which of the two rows actually gets assigned an
ID
value, and which row does not?– Tim Biegeleisen
Nov 9 at 10:57
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
Nov 9 at 10:57
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
Nov 9 at 10:57
short answer - rbind them and then fixup any of ID that are
duplicated(d$ID)
– Spacedman
Nov 9 at 10:58
short answer - rbind them and then fixup any of ID that are
duplicated(d$ID)
– Spacedman
Nov 9 at 10:58
Are you trying to get some sort of printed output?
– Spacedman
Nov 9 at 11:02
Are you trying to get some sort of printed output?
– Spacedman
Nov 9 at 11:02
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
Nov 9 at 11:04
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
Nov 9 at 11:04
|
show 1 more comment
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
But I would like to keep the number of rows. Attached image shows the required result
– K.M
Nov 9 at 11:27
add a comment |
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
But I would like to keep the number of rows. Attached image shows the required result
– K.M
Nov 9 at 11:27
add a comment |
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
But I would like to keep the number of rows. Attached image shows the required result
– K.M
Nov 9 at 11:27
add a comment |
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
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
edited Nov 9 at 11:57
answered Nov 9 at 11:01
Spacedman
71.3k993164
71.3k993164
But I would like to keep the number of rows. Attached image shows the required result
– K.M
Nov 9 at 11:27
add a comment |
But I would like to keep the number of rows. Attached image shows the required result
– K.M
Nov 9 at 11:27
But I would like to keep the number of rows. Attached image shows the required result
– K.M
Nov 9 at 11:27
But I would like to keep the number of rows. Attached image shows the required result
– K.M
Nov 9 at 11:27
add a comment |
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%2f53224361%2fmerge-two-tables-in-r%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
What determines which of the two rows actually gets assigned an
ID
value, and which row does not?– Tim Biegeleisen
Nov 9 at 10:57
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
Nov 9 at 10:57
short answer - rbind them and then fixup any of ID that are
duplicated(d$ID)
– Spacedman
Nov 9 at 10:58
Are you trying to get some sort of printed output?
– Spacedman
Nov 9 at 11:02
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
Nov 9 at 11:04