How to write few values in the same cell in R?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I need to compare table1
with 1 row and table2
with 2 rows.
In row1 both tables contains the same values but they are randomized and row2 in table2
contains a few different values for the same value in row1.
So I need to write a few values in the same cell.
This code running but it is incorrect because if values in row2 of table2
are different for the same value in row1 table1
, it just rewrites it with the last value it founds. But I need both.
for (i in 1:nrow(table1)){
for (a in 1:nrow(table2)){
if(table1$row1[i] == table2$row1[a]){
table1$row2[i] <- paste(table2$row2[a], collapse = ", ")
}
}
}
Data example:
table1 ↓
row1
1 c43bdf23a60
2 164389427682
3 12398826693
4 48492266219
5 150403618359
6 150403618759
table2 ↓
row1 row2
1 32ba31ce836 21315839
2 7ccaba4ce5d 50729458
3 c43bdf23a60 32204599
4 f66cc27fd46 198217504
5 c43bdf23a60 852948367
6 c43bdf23a60 81423505
any help is appreciated...
r for-loop
|
show 5 more comments
I need to compare table1
with 1 row and table2
with 2 rows.
In row1 both tables contains the same values but they are randomized and row2 in table2
contains a few different values for the same value in row1.
So I need to write a few values in the same cell.
This code running but it is incorrect because if values in row2 of table2
are different for the same value in row1 table1
, it just rewrites it with the last value it founds. But I need both.
for (i in 1:nrow(table1)){
for (a in 1:nrow(table2)){
if(table1$row1[i] == table2$row1[a]){
table1$row2[i] <- paste(table2$row2[a], collapse = ", ")
}
}
}
Data example:
table1 ↓
row1
1 c43bdf23a60
2 164389427682
3 12398826693
4 48492266219
5 150403618359
6 150403618759
table2 ↓
row1 row2
1 32ba31ce836 21315839
2 7ccaba4ce5d 50729458
3 c43bdf23a60 32204599
4 f66cc27fd46 198217504
5 c43bdf23a60 852948367
6 c43bdf23a60 81423505
any help is appreciated...
r for-loop
6
but without a decent Minimal, Complete, and Verifiable example, your question is un-answerable
– Vivek Kalyanarangan
Jan 3 at 12:51
Welcome to SO! Please, try to add some data to make your example reproducible, and also an explicit desired output made from the data posted.
– s_t
Jan 3 at 12:52
Can you post sample data? Please edit the question with the output ofdput(table1)
. And the same fortable2
.
– Rui Barradas
Jan 3 at 12:53
it is hard to figure out what you want, but seems thatifelse
function should give what you want. example :table1$new_row <- ifelse(table1$row1 == table2$row1,paste(table2$row2[a], collapse = ", "),table1$row1)
– mukund
Jan 3 at 12:54
Also, if it is one time job to replace such values you can edit the dataframe manually usingfix()
function which opens up dataframe in popup window.
– mukund
Jan 3 at 12:58
|
show 5 more comments
I need to compare table1
with 1 row and table2
with 2 rows.
In row1 both tables contains the same values but they are randomized and row2 in table2
contains a few different values for the same value in row1.
So I need to write a few values in the same cell.
This code running but it is incorrect because if values in row2 of table2
are different for the same value in row1 table1
, it just rewrites it with the last value it founds. But I need both.
for (i in 1:nrow(table1)){
for (a in 1:nrow(table2)){
if(table1$row1[i] == table2$row1[a]){
table1$row2[i] <- paste(table2$row2[a], collapse = ", ")
}
}
}
Data example:
table1 ↓
row1
1 c43bdf23a60
2 164389427682
3 12398826693
4 48492266219
5 150403618359
6 150403618759
table2 ↓
row1 row2
1 32ba31ce836 21315839
2 7ccaba4ce5d 50729458
3 c43bdf23a60 32204599
4 f66cc27fd46 198217504
5 c43bdf23a60 852948367
6 c43bdf23a60 81423505
any help is appreciated...
r for-loop
I need to compare table1
with 1 row and table2
with 2 rows.
In row1 both tables contains the same values but they are randomized and row2 in table2
contains a few different values for the same value in row1.
So I need to write a few values in the same cell.
This code running but it is incorrect because if values in row2 of table2
are different for the same value in row1 table1
, it just rewrites it with the last value it founds. But I need both.
for (i in 1:nrow(table1)){
for (a in 1:nrow(table2)){
if(table1$row1[i] == table2$row1[a]){
table1$row2[i] <- paste(table2$row2[a], collapse = ", ")
}
}
}
Data example:
table1 ↓
row1
1 c43bdf23a60
2 164389427682
3 12398826693
4 48492266219
5 150403618359
6 150403618759
table2 ↓
row1 row2
1 32ba31ce836 21315839
2 7ccaba4ce5d 50729458
3 c43bdf23a60 32204599
4 f66cc27fd46 198217504
5 c43bdf23a60 852948367
6 c43bdf23a60 81423505
any help is appreciated...
r for-loop
r for-loop
edited Jan 3 at 14:00
Richard
asked Jan 3 at 12:45
RichardRichard
64
64
6
but without a decent Minimal, Complete, and Verifiable example, your question is un-answerable
– Vivek Kalyanarangan
Jan 3 at 12:51
Welcome to SO! Please, try to add some data to make your example reproducible, and also an explicit desired output made from the data posted.
– s_t
Jan 3 at 12:52
Can you post sample data? Please edit the question with the output ofdput(table1)
. And the same fortable2
.
– Rui Barradas
Jan 3 at 12:53
it is hard to figure out what you want, but seems thatifelse
function should give what you want. example :table1$new_row <- ifelse(table1$row1 == table2$row1,paste(table2$row2[a], collapse = ", "),table1$row1)
– mukund
Jan 3 at 12:54
Also, if it is one time job to replace such values you can edit the dataframe manually usingfix()
function which opens up dataframe in popup window.
– mukund
Jan 3 at 12:58
|
show 5 more comments
6
but without a decent Minimal, Complete, and Verifiable example, your question is un-answerable
– Vivek Kalyanarangan
Jan 3 at 12:51
Welcome to SO! Please, try to add some data to make your example reproducible, and also an explicit desired output made from the data posted.
– s_t
Jan 3 at 12:52
Can you post sample data? Please edit the question with the output ofdput(table1)
. And the same fortable2
.
– Rui Barradas
Jan 3 at 12:53
it is hard to figure out what you want, but seems thatifelse
function should give what you want. example :table1$new_row <- ifelse(table1$row1 == table2$row1,paste(table2$row2[a], collapse = ", "),table1$row1)
– mukund
Jan 3 at 12:54
Also, if it is one time job to replace such values you can edit the dataframe manually usingfix()
function which opens up dataframe in popup window.
– mukund
Jan 3 at 12:58
6
6
but without a decent Minimal, Complete, and Verifiable example, your question is un-answerable
– Vivek Kalyanarangan
Jan 3 at 12:51
but without a decent Minimal, Complete, and Verifiable example, your question is un-answerable
– Vivek Kalyanarangan
Jan 3 at 12:51
Welcome to SO! Please, try to add some data to make your example reproducible, and also an explicit desired output made from the data posted.
– s_t
Jan 3 at 12:52
Welcome to SO! Please, try to add some data to make your example reproducible, and also an explicit desired output made from the data posted.
– s_t
Jan 3 at 12:52
Can you post sample data? Please edit the question with the output of
dput(table1)
. And the same for table2
.– Rui Barradas
Jan 3 at 12:53
Can you post sample data? Please edit the question with the output of
dput(table1)
. And the same for table2
.– Rui Barradas
Jan 3 at 12:53
it is hard to figure out what you want, but seems that
ifelse
function should give what you want. example : table1$new_row <- ifelse(table1$row1 == table2$row1,paste(table2$row2[a], collapse = ", "),table1$row1)
– mukund
Jan 3 at 12:54
it is hard to figure out what you want, but seems that
ifelse
function should give what you want. example : table1$new_row <- ifelse(table1$row1 == table2$row1,paste(table2$row2[a], collapse = ", "),table1$row1)
– mukund
Jan 3 at 12:54
Also, if it is one time job to replace such values you can edit the dataframe manually using
fix()
function which opens up dataframe in popup window.– mukund
Jan 3 at 12:58
Also, if it is one time job to replace such values you can edit the dataframe manually using
fix()
function which opens up dataframe in popup window.– mukund
Jan 3 at 12:58
|
show 5 more comments
2 Answers
2
active
oldest
votes
First using lapply
to build a list with matched table2$row2
results pasted into string
lst <- lapply(table1$row1, function(x) c(row1 = x, row2 = paste(table2[table2$row1 == x, ]$row2, collapse=", ")))
Converting list
into a data.frame
as.data.frame(do.call(rbind, lst))
row1 row2
1 c43bdf23a60 32204599, 852948367, 81423505
2 164389427682
3 12398826693
4 48492266219
5 150403618359
6 150403618759
This is building a new data.frame
and not merging with table1
(not clear if needed).
Modifying original code to insert pasted values into table1
for (i in seq_len(nrow(table1))){
table1$row2[i] <- paste(table2[table2$row1 == table1$row1[i], ]$row2, collapse = ", ")
}
Getting error - "setting levels for factors are different"
– Richard
Jan 4 at 9:57
@Richard make sure that yourrow1
in both tables is of typecharacter
, notfactor
.
– Tino
Jan 4 at 11:26
@manotheshark brilliant - it works! Thank you so much!
– Richard
Jan 4 at 11:46
add a comment |
Maybe this is what you want: First, collect all values from row2
for unique values in row1
of table2
, then merge with table1
:
merge(
x = table1,
y = aggregate(row2~row1, data = table2, FUN = paste0, collapse = ","),
all.x = T,
sort = F
)
It wouldn't work that way because if i will collect all values from row2 for unique values in row1 of table2 it would not paste all results, but just rewrite existing results in the same cell. Then aggregate is not needed.
– Richard
Jan 4 at 11:17
You might have had the same problems here with your input data (factor instead of character). Because usingmerge
andaggregate
makes it robust, hence you need both:aggregate
groups byrow1
and collapses allrow2
-values together as needed.merge
finally assures that all values fromtable1
occur, even if they don't have arow2
-value intable2
.
– Tino
Jan 4 at 12:08
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%2f54022571%2fhow-to-write-few-values-in-the-same-cell-in-r%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
First using lapply
to build a list with matched table2$row2
results pasted into string
lst <- lapply(table1$row1, function(x) c(row1 = x, row2 = paste(table2[table2$row1 == x, ]$row2, collapse=", ")))
Converting list
into a data.frame
as.data.frame(do.call(rbind, lst))
row1 row2
1 c43bdf23a60 32204599, 852948367, 81423505
2 164389427682
3 12398826693
4 48492266219
5 150403618359
6 150403618759
This is building a new data.frame
and not merging with table1
(not clear if needed).
Modifying original code to insert pasted values into table1
for (i in seq_len(nrow(table1))){
table1$row2[i] <- paste(table2[table2$row1 == table1$row1[i], ]$row2, collapse = ", ")
}
Getting error - "setting levels for factors are different"
– Richard
Jan 4 at 9:57
@Richard make sure that yourrow1
in both tables is of typecharacter
, notfactor
.
– Tino
Jan 4 at 11:26
@manotheshark brilliant - it works! Thank you so much!
– Richard
Jan 4 at 11:46
add a comment |
First using lapply
to build a list with matched table2$row2
results pasted into string
lst <- lapply(table1$row1, function(x) c(row1 = x, row2 = paste(table2[table2$row1 == x, ]$row2, collapse=", ")))
Converting list
into a data.frame
as.data.frame(do.call(rbind, lst))
row1 row2
1 c43bdf23a60 32204599, 852948367, 81423505
2 164389427682
3 12398826693
4 48492266219
5 150403618359
6 150403618759
This is building a new data.frame
and not merging with table1
(not clear if needed).
Modifying original code to insert pasted values into table1
for (i in seq_len(nrow(table1))){
table1$row2[i] <- paste(table2[table2$row1 == table1$row1[i], ]$row2, collapse = ", ")
}
Getting error - "setting levels for factors are different"
– Richard
Jan 4 at 9:57
@Richard make sure that yourrow1
in both tables is of typecharacter
, notfactor
.
– Tino
Jan 4 at 11:26
@manotheshark brilliant - it works! Thank you so much!
– Richard
Jan 4 at 11:46
add a comment |
First using lapply
to build a list with matched table2$row2
results pasted into string
lst <- lapply(table1$row1, function(x) c(row1 = x, row2 = paste(table2[table2$row1 == x, ]$row2, collapse=", ")))
Converting list
into a data.frame
as.data.frame(do.call(rbind, lst))
row1 row2
1 c43bdf23a60 32204599, 852948367, 81423505
2 164389427682
3 12398826693
4 48492266219
5 150403618359
6 150403618759
This is building a new data.frame
and not merging with table1
(not clear if needed).
Modifying original code to insert pasted values into table1
for (i in seq_len(nrow(table1))){
table1$row2[i] <- paste(table2[table2$row1 == table1$row1[i], ]$row2, collapse = ", ")
}
First using lapply
to build a list with matched table2$row2
results pasted into string
lst <- lapply(table1$row1, function(x) c(row1 = x, row2 = paste(table2[table2$row1 == x, ]$row2, collapse=", ")))
Converting list
into a data.frame
as.data.frame(do.call(rbind, lst))
row1 row2
1 c43bdf23a60 32204599, 852948367, 81423505
2 164389427682
3 12398826693
4 48492266219
5 150403618359
6 150403618759
This is building a new data.frame
and not merging with table1
(not clear if needed).
Modifying original code to insert pasted values into table1
for (i in seq_len(nrow(table1))){
table1$row2[i] <- paste(table2[table2$row1 == table1$row1[i], ]$row2, collapse = ", ")
}
edited Jan 3 at 14:59
answered Jan 3 at 14:19
manothesharkmanotheshark
2,4411028
2,4411028
Getting error - "setting levels for factors are different"
– Richard
Jan 4 at 9:57
@Richard make sure that yourrow1
in both tables is of typecharacter
, notfactor
.
– Tino
Jan 4 at 11:26
@manotheshark brilliant - it works! Thank you so much!
– Richard
Jan 4 at 11:46
add a comment |
Getting error - "setting levels for factors are different"
– Richard
Jan 4 at 9:57
@Richard make sure that yourrow1
in both tables is of typecharacter
, notfactor
.
– Tino
Jan 4 at 11:26
@manotheshark brilliant - it works! Thank you so much!
– Richard
Jan 4 at 11:46
Getting error - "setting levels for factors are different"
– Richard
Jan 4 at 9:57
Getting error - "setting levels for factors are different"
– Richard
Jan 4 at 9:57
@Richard make sure that your
row1
in both tables is of type character
, not factor
.– Tino
Jan 4 at 11:26
@Richard make sure that your
row1
in both tables is of type character
, not factor
.– Tino
Jan 4 at 11:26
@manotheshark brilliant - it works! Thank you so much!
– Richard
Jan 4 at 11:46
@manotheshark brilliant - it works! Thank you so much!
– Richard
Jan 4 at 11:46
add a comment |
Maybe this is what you want: First, collect all values from row2
for unique values in row1
of table2
, then merge with table1
:
merge(
x = table1,
y = aggregate(row2~row1, data = table2, FUN = paste0, collapse = ","),
all.x = T,
sort = F
)
It wouldn't work that way because if i will collect all values from row2 for unique values in row1 of table2 it would not paste all results, but just rewrite existing results in the same cell. Then aggregate is not needed.
– Richard
Jan 4 at 11:17
You might have had the same problems here with your input data (factor instead of character). Because usingmerge
andaggregate
makes it robust, hence you need both:aggregate
groups byrow1
and collapses allrow2
-values together as needed.merge
finally assures that all values fromtable1
occur, even if they don't have arow2
-value intable2
.
– Tino
Jan 4 at 12:08
add a comment |
Maybe this is what you want: First, collect all values from row2
for unique values in row1
of table2
, then merge with table1
:
merge(
x = table1,
y = aggregate(row2~row1, data = table2, FUN = paste0, collapse = ","),
all.x = T,
sort = F
)
It wouldn't work that way because if i will collect all values from row2 for unique values in row1 of table2 it would not paste all results, but just rewrite existing results in the same cell. Then aggregate is not needed.
– Richard
Jan 4 at 11:17
You might have had the same problems here with your input data (factor instead of character). Because usingmerge
andaggregate
makes it robust, hence you need both:aggregate
groups byrow1
and collapses allrow2
-values together as needed.merge
finally assures that all values fromtable1
occur, even if they don't have arow2
-value intable2
.
– Tino
Jan 4 at 12:08
add a comment |
Maybe this is what you want: First, collect all values from row2
for unique values in row1
of table2
, then merge with table1
:
merge(
x = table1,
y = aggregate(row2~row1, data = table2, FUN = paste0, collapse = ","),
all.x = T,
sort = F
)
Maybe this is what you want: First, collect all values from row2
for unique values in row1
of table2
, then merge with table1
:
merge(
x = table1,
y = aggregate(row2~row1, data = table2, FUN = paste0, collapse = ","),
all.x = T,
sort = F
)
edited Jan 4 at 11:59
answered Jan 3 at 15:17
TinoTino
1,707813
1,707813
It wouldn't work that way because if i will collect all values from row2 for unique values in row1 of table2 it would not paste all results, but just rewrite existing results in the same cell. Then aggregate is not needed.
– Richard
Jan 4 at 11:17
You might have had the same problems here with your input data (factor instead of character). Because usingmerge
andaggregate
makes it robust, hence you need both:aggregate
groups byrow1
and collapses allrow2
-values together as needed.merge
finally assures that all values fromtable1
occur, even if they don't have arow2
-value intable2
.
– Tino
Jan 4 at 12:08
add a comment |
It wouldn't work that way because if i will collect all values from row2 for unique values in row1 of table2 it would not paste all results, but just rewrite existing results in the same cell. Then aggregate is not needed.
– Richard
Jan 4 at 11:17
You might have had the same problems here with your input data (factor instead of character). Because usingmerge
andaggregate
makes it robust, hence you need both:aggregate
groups byrow1
and collapses allrow2
-values together as needed.merge
finally assures that all values fromtable1
occur, even if they don't have arow2
-value intable2
.
– Tino
Jan 4 at 12:08
It wouldn't work that way because if i will collect all values from row2 for unique values in row1 of table2 it would not paste all results, but just rewrite existing results in the same cell. Then aggregate is not needed.
– Richard
Jan 4 at 11:17
It wouldn't work that way because if i will collect all values from row2 for unique values in row1 of table2 it would not paste all results, but just rewrite existing results in the same cell. Then aggregate is not needed.
– Richard
Jan 4 at 11:17
You might have had the same problems here with your input data (factor instead of character). Because using
merge
and aggregate
makes it robust, hence you need both: aggregate
groups by row1
and collapses all row2
-values together as needed. merge
finally assures that all values from table1
occur, even if they don't have a row2
-value in table2
.– Tino
Jan 4 at 12:08
You might have had the same problems here with your input data (factor instead of character). Because using
merge
and aggregate
makes it robust, hence you need both: aggregate
groups by row1
and collapses all row2
-values together as needed. merge
finally assures that all values from table1
occur, even if they don't have a row2
-value in table2
.– Tino
Jan 4 at 12:08
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%2f54022571%2fhow-to-write-few-values-in-the-same-cell-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
6
but without a decent Minimal, Complete, and Verifiable example, your question is un-answerable
– Vivek Kalyanarangan
Jan 3 at 12:51
Welcome to SO! Please, try to add some data to make your example reproducible, and also an explicit desired output made from the data posted.
– s_t
Jan 3 at 12:52
Can you post sample data? Please edit the question with the output of
dput(table1)
. And the same fortable2
.– Rui Barradas
Jan 3 at 12:53
it is hard to figure out what you want, but seems that
ifelse
function should give what you want. example :table1$new_row <- ifelse(table1$row1 == table2$row1,paste(table2$row2[a], collapse = ", "),table1$row1)
– mukund
Jan 3 at 12:54
Also, if it is one time job to replace such values you can edit the dataframe manually using
fix()
function which opens up dataframe in popup window.– mukund
Jan 3 at 12:58