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;








2















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










share|improve this question
























  • 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


















2















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










share|improve this question
























  • 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














2












2








2








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










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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


















  • 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

















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













1 Answer
1






active

oldest

votes


















3














  1. 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


  1. Works with data.frame and matrix

 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))





share|improve this answer




















  • 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 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











  • @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










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
);



);













draft saved

draft discarded


















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









3














  1. 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


  1. Works with data.frame and matrix

 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))





share|improve this answer




















  • 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 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











  • @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















3














  1. 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


  1. Works with data.frame and matrix

 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))





share|improve this answer




















  • 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 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











  • @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













3












3








3







  1. 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


  1. Works with data.frame and matrix

 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))





share|improve this answer















  1. 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


  1. Works with data.frame and matrix

 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))






share|improve this answer














share|improve this answer



share|improve this answer








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 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











  • @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












  • 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 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











  • @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







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








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.



















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?