How to add columns that have same nameHow to sort a dataframe by multiple column(s)Combine two data frames by rows (rbind) when they have different sets of columnsHow to drop columns by name in a data frameHow to reshape data from long to wide formatHow to make a great R reproducible exampleMatching a column from a data frame on the columns of another data frame and if they match add a new columnChanging Column Names in a List of Data Frames in RR-finding unmatched column names of data framesMatch row names and column names to values in another data framesubset by the first part of column names
Why hasn't the U.S. government paid war reparations to any country it attacked?
Bob's unnecessary trip to the shops
How does one stock fund's charge of 1% more in operating expenses than another fund lower expected returns by 10%?
Supporting developers who insist on using their pet language
Are lithium batteries allowed in the International Space Station?
Why limit to revolvers?
Why is dry soil hydrophobic? Bad gardener paradox
Find the wrong number in the given series: 6, 12, 21, 36, 56, 81?
P-MOSFET failing
Players of unusual orchestral instruments
Should you avoid redundant information after dialogue?
Is purchasing foreign currency before going abroad a losing proposition?
Can I intentionally omit previous work experience or pretend it doesn't exist when applying for jobs?
Was the Ford Model T black because of the speed black paint dries?
Extract an attribute value from XML
How can I deal with a player trying to insert real-world mythology into my homebrew setting?
Do native speakers use ZVE or CPU?
Cubic programming and beyond?
What is temperature on a quantum level?
Why does the trade federation become so alarmed upon learning the ambassadors are Jedi Knights?
How is the idea of "X comes a distant third" commonly expressed in Russian?
Modeling, view and projection transformation using vector and point in homogenous form
Too many spies!
Can you negate disadvantage on throwing a net by using the Lunging Attack maneuver of the Battle Master fighter?
How to add columns that have same name
How to sort a dataframe by multiple column(s)Combine two data frames by rows (rbind) when they have different sets of columnsHow to drop columns by name in a data frameHow to reshape data from long to wide formatHow to make a great R reproducible exampleMatching a column from a data frame on the columns of another data frame and if they match add a new columnChanging Column Names in a List of Data Frames in RR-finding unmatched column names of data framesMatch row names and column names to values in another data framesubset by the first part of column names
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have large datasets, that is two data frame. and want to add value that has the same column name in the other one data frame. how do I set the code?
df1
a b c
0 0 0
0 0 0
df2
a c d
1 1 0
0 1 0
what I expected is:
a b c
1 0 1
0 0 1
it means I'm in charge to stay with colnames df1 but the value is in df2. thanks for the help. have a good day
r multiple-columns
add a comment |
I have large datasets, that is two data frame. and want to add value that has the same column name in the other one data frame. how do I set the code?
df1
a b c
0 0 0
0 0 0
df2
a c d
1 1 0
0 1 0
what I expected is:
a b c
1 0 1
0 0 1
it means I'm in charge to stay with colnames df1 but the value is in df2. thanks for the help. have a good day
r multiple-columns
What exactly do you mean by "add"?
– NelsonGon
Mar 26 at 3:21
sorry yes my grammar is not good, sorry :D. I mean how to addvalue
when it has the same column names. but it has to be arranged likedf1
thecolnames
has to beorder
likedf1
but thevalue
is like thedf2
– Gadis Wahyu
Mar 26 at 4:45
add a comment |
I have large datasets, that is two data frame. and want to add value that has the same column name in the other one data frame. how do I set the code?
df1
a b c
0 0 0
0 0 0
df2
a c d
1 1 0
0 1 0
what I expected is:
a b c
1 0 1
0 0 1
it means I'm in charge to stay with colnames df1 but the value is in df2. thanks for the help. have a good day
r multiple-columns
I have large datasets, that is two data frame. and want to add value that has the same column name in the other one data frame. how do I set the code?
df1
a b c
0 0 0
0 0 0
df2
a c d
1 1 0
0 1 0
what I expected is:
a b c
1 0 1
0 0 1
it means I'm in charge to stay with colnames df1 but the value is in df2. thanks for the help. have a good day
r multiple-columns
r multiple-columns
edited Mar 26 at 3:31
Gadis Wahyu
asked Mar 26 at 3:17
Gadis WahyuGadis Wahyu
134 bronze badges
134 bronze badges
What exactly do you mean by "add"?
– NelsonGon
Mar 26 at 3:21
sorry yes my grammar is not good, sorry :D. I mean how to addvalue
when it has the same column names. but it has to be arranged likedf1
thecolnames
has to beorder
likedf1
but thevalue
is like thedf2
– Gadis Wahyu
Mar 26 at 4:45
add a comment |
What exactly do you mean by "add"?
– NelsonGon
Mar 26 at 3:21
sorry yes my grammar is not good, sorry :D. I mean how to addvalue
when it has the same column names. but it has to be arranged likedf1
thecolnames
has to beorder
likedf1
but thevalue
is like thedf2
– Gadis Wahyu
Mar 26 at 4:45
What exactly do you mean by "add"?
– NelsonGon
Mar 26 at 3:21
What exactly do you mean by "add"?
– NelsonGon
Mar 26 at 3:21
sorry yes my grammar is not good, sorry :D. I mean how to add
value
when it has the same column names. but it has to be arranged like df1
the colnames
has to be order
like df1
but the value
is like the df2
– Gadis Wahyu
Mar 26 at 4:45
sorry yes my grammar is not good, sorry :D. I mean how to add
value
when it has the same column names. but it has to be arranged like df1
the colnames
has to be order
like df1
but the value
is like the df2
– Gadis Wahyu
Mar 26 at 4:45
add a comment |
1 Answer
1
active
oldest
votes
- Works with
data.frame
data.frame(lapply(X = split.default(x = cbind(df1, df2),
f = c(names(df1), names(df2))),
FUN = rowSums))[names(df1)]
# a b c
#1 1 0 1
#2 0 0 1
- Works with
data.frame
andmatrix
nm = intersect(colnames(df1), colnames(df2))
nm1 = colnames(df1)[!colnames(df1) %in% nm]
m = cbind(df1[, nm1, drop = FALSE], df1[, nm, drop = FALSE] + df2[, nm, drop = FALSE])
colnames(m) = c(nm1, nm)
m[,colnames(df1)]
# a b c
#1 1 0 1
#2 0 0 1
#DATA
df1 = structure(list(a = c(0L, 0L), b = c(0L, 0L), c = c(0L, 0L)),
class = "data.frame",
row.names = c(NA, -2L))
df2 = structure(list(a = 1:0, c = c(1L, 1L), d = c(0L, 0L)),
class = "data.frame",
row.names = c(NA, -2L))
1
A different spin on the second option -replace(df1, nm, df1[nm] + df2[nm])
– thelatemail
Mar 26 at 3:30
thanks but i had to correct it firstnm = intersect(colnames(df1),colnames(df2))
– Gadis Wahyu
Mar 26 at 4:35
@thelatemail is not working with replace because it turns out to NA value, it is for vector, what is the function if it matrix?
– Gadis Wahyu
Mar 26 at 4:43
@d.b yes i got it incsv
files so could you fix it? i would glad
– Gadis Wahyu
Mar 26 at 4:44
@d.b yes it's work thanks btw, but I have to arranged the colnames like df1
– Gadis Wahyu
Mar 26 at 5:17
|
show 1 more 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%2f55349343%2fhow-to-add-columns-that-have-same-name%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
- Works with
data.frame
data.frame(lapply(X = split.default(x = cbind(df1, df2),
f = c(names(df1), names(df2))),
FUN = rowSums))[names(df1)]
# a b c
#1 1 0 1
#2 0 0 1
- Works with
data.frame
andmatrix
nm = intersect(colnames(df1), colnames(df2))
nm1 = colnames(df1)[!colnames(df1) %in% nm]
m = cbind(df1[, nm1, drop = FALSE], df1[, nm, drop = FALSE] + df2[, nm, drop = FALSE])
colnames(m) = c(nm1, nm)
m[,colnames(df1)]
# a b c
#1 1 0 1
#2 0 0 1
#DATA
df1 = structure(list(a = c(0L, 0L), b = c(0L, 0L), c = c(0L, 0L)),
class = "data.frame",
row.names = c(NA, -2L))
df2 = structure(list(a = 1:0, c = c(1L, 1L), d = c(0L, 0L)),
class = "data.frame",
row.names = c(NA, -2L))
1
A different spin on the second option -replace(df1, nm, df1[nm] + df2[nm])
– thelatemail
Mar 26 at 3:30
thanks but i had to correct it firstnm = intersect(colnames(df1),colnames(df2))
– Gadis Wahyu
Mar 26 at 4:35
@thelatemail is not working with replace because it turns out to NA value, it is for vector, what is the function if it matrix?
– Gadis Wahyu
Mar 26 at 4:43
@d.b yes i got it incsv
files so could you fix it? i would glad
– Gadis Wahyu
Mar 26 at 4:44
@d.b yes it's work thanks btw, but I have to arranged the colnames like df1
– Gadis Wahyu
Mar 26 at 5:17
|
show 1 more comment
- Works with
data.frame
data.frame(lapply(X = split.default(x = cbind(df1, df2),
f = c(names(df1), names(df2))),
FUN = rowSums))[names(df1)]
# a b c
#1 1 0 1
#2 0 0 1
- Works with
data.frame
andmatrix
nm = intersect(colnames(df1), colnames(df2))
nm1 = colnames(df1)[!colnames(df1) %in% nm]
m = cbind(df1[, nm1, drop = FALSE], df1[, nm, drop = FALSE] + df2[, nm, drop = FALSE])
colnames(m) = c(nm1, nm)
m[,colnames(df1)]
# a b c
#1 1 0 1
#2 0 0 1
#DATA
df1 = structure(list(a = c(0L, 0L), b = c(0L, 0L), c = c(0L, 0L)),
class = "data.frame",
row.names = c(NA, -2L))
df2 = structure(list(a = 1:0, c = c(1L, 1L), d = c(0L, 0L)),
class = "data.frame",
row.names = c(NA, -2L))
1
A different spin on the second option -replace(df1, nm, df1[nm] + df2[nm])
– thelatemail
Mar 26 at 3:30
thanks but i had to correct it firstnm = intersect(colnames(df1),colnames(df2))
– Gadis Wahyu
Mar 26 at 4:35
@thelatemail is not working with replace because it turns out to NA value, it is for vector, what is the function if it matrix?
– Gadis Wahyu
Mar 26 at 4:43
@d.b yes i got it incsv
files so could you fix it? i would glad
– Gadis Wahyu
Mar 26 at 4:44
@d.b yes it's work thanks btw, but I have to arranged the colnames like df1
– Gadis Wahyu
Mar 26 at 5:17
|
show 1 more comment
- Works with
data.frame
data.frame(lapply(X = split.default(x = cbind(df1, df2),
f = c(names(df1), names(df2))),
FUN = rowSums))[names(df1)]
# a b c
#1 1 0 1
#2 0 0 1
- Works with
data.frame
andmatrix
nm = intersect(colnames(df1), colnames(df2))
nm1 = colnames(df1)[!colnames(df1) %in% nm]
m = cbind(df1[, nm1, drop = FALSE], df1[, nm, drop = FALSE] + df2[, nm, drop = FALSE])
colnames(m) = c(nm1, nm)
m[,colnames(df1)]
# a b c
#1 1 0 1
#2 0 0 1
#DATA
df1 = structure(list(a = c(0L, 0L), b = c(0L, 0L), c = c(0L, 0L)),
class = "data.frame",
row.names = c(NA, -2L))
df2 = structure(list(a = 1:0, c = c(1L, 1L), d = c(0L, 0L)),
class = "data.frame",
row.names = c(NA, -2L))
- Works with
data.frame
data.frame(lapply(X = split.default(x = cbind(df1, df2),
f = c(names(df1), names(df2))),
FUN = rowSums))[names(df1)]
# a b c
#1 1 0 1
#2 0 0 1
- Works with
data.frame
andmatrix
nm = intersect(colnames(df1), colnames(df2))
nm1 = colnames(df1)[!colnames(df1) %in% nm]
m = cbind(df1[, nm1, drop = FALSE], df1[, nm, drop = FALSE] + df2[, nm, drop = FALSE])
colnames(m) = c(nm1, nm)
m[,colnames(df1)]
# a b c
#1 1 0 1
#2 0 0 1
#DATA
df1 = structure(list(a = c(0L, 0L), b = c(0L, 0L), c = c(0L, 0L)),
class = "data.frame",
row.names = c(NA, -2L))
df2 = structure(list(a = 1:0, c = c(1L, 1L), d = c(0L, 0L)),
class = "data.frame",
row.names = c(NA, -2L))
edited Mar 26 at 15:40
answered Mar 26 at 3:21
d.bd.b
21.2k4 gold badges19 silver badges50 bronze badges
21.2k4 gold badges19 silver badges50 bronze badges
1
A different spin on the second option -replace(df1, nm, df1[nm] + df2[nm])
– thelatemail
Mar 26 at 3:30
thanks but i had to correct it firstnm = intersect(colnames(df1),colnames(df2))
– Gadis Wahyu
Mar 26 at 4:35
@thelatemail is not working with replace because it turns out to NA value, it is for vector, what is the function if it matrix?
– Gadis Wahyu
Mar 26 at 4:43
@d.b yes i got it incsv
files so could you fix it? i would glad
– Gadis Wahyu
Mar 26 at 4:44
@d.b yes it's work thanks btw, but I have to arranged the colnames like df1
– Gadis Wahyu
Mar 26 at 5:17
|
show 1 more comment
1
A different spin on the second option -replace(df1, nm, df1[nm] + df2[nm])
– thelatemail
Mar 26 at 3:30
thanks but i had to correct it firstnm = intersect(colnames(df1),colnames(df2))
– Gadis Wahyu
Mar 26 at 4:35
@thelatemail is not working with replace because it turns out to NA value, it is for vector, what is the function if it matrix?
– Gadis Wahyu
Mar 26 at 4:43
@d.b yes i got it incsv
files so could you fix it? i would glad
– Gadis Wahyu
Mar 26 at 4:44
@d.b yes it's work thanks btw, but I have to arranged the colnames like df1
– Gadis Wahyu
Mar 26 at 5:17
1
1
A different spin on the second option -
replace(df1, nm, df1[nm] + df2[nm])
– thelatemail
Mar 26 at 3:30
A different spin on the second option -
replace(df1, nm, df1[nm] + df2[nm])
– thelatemail
Mar 26 at 3:30
thanks but i had to correct it first
nm = intersect(colnames(df1),colnames(df2))
– Gadis Wahyu
Mar 26 at 4:35
thanks but i had to correct it first
nm = intersect(colnames(df1),colnames(df2))
– Gadis Wahyu
Mar 26 at 4:35
@thelatemail is not working with replace because it turns out to NA value, it is for vector, what is the function if it matrix?
– Gadis Wahyu
Mar 26 at 4:43
@thelatemail is not working with replace because it turns out to NA value, it is for vector, what is the function if it matrix?
– Gadis Wahyu
Mar 26 at 4:43
@d.b yes i got it in
csv
files so could you fix it? i would glad– Gadis Wahyu
Mar 26 at 4:44
@d.b yes i got it in
csv
files so could you fix it? i would glad– Gadis Wahyu
Mar 26 at 4:44
@d.b yes it's work thanks btw, but I have to arranged the colnames like df1
– Gadis Wahyu
Mar 26 at 5:17
@d.b yes it's work thanks btw, but I have to arranged the colnames like df1
– Gadis Wahyu
Mar 26 at 5:17
|
show 1 more comment
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
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%2f55349343%2fhow-to-add-columns-that-have-same-name%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 exactly do you mean by "add"?
– NelsonGon
Mar 26 at 3:21
sorry yes my grammar is not good, sorry :D. I mean how to add
value
when it has the same column names. but it has to be arranged likedf1
thecolnames
has to beorder
likedf1
but thevalue
is like thedf2
– Gadis Wahyu
Mar 26 at 4:45