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

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현