Dear all, I try to merge two columns consisting of characters using the 'coalesce' function from dplyr package. However, two data still have not merged, data no. 124 1nd 143. Any help is very much appreciated. I provide the data as follows.> dput(dtpaigd)c("C+", "B+", "C+", "B+", "C+", "A-", "A ", "B+", "A-", "C ", "B+", "A-", "B+", "B ", "B ", "B ", "D ", "B+", "B ", "B ", "C+", "B ", "A ", "A-", "B+", "A-", "A ", "B ", "A ", "C ", "B ", "A+", "C ", "B ", "B+", "C+", "B ", "B ", "C+", "B ", "B+", "B ", "A-", "B+", "B ", "A-", "C+", "A ", "A-", "C+", "C+", "B ", "B+", "A-", "A-", "B ", "E ", "A-", "B+", "B ", "A ", "B+", "A-", "A-", "A ", "B+", "A-", "A-", "A-", "B+", "C ", "A-", "A ", "A-", "A ", "A-", "A-", "A ", "A ", "B+", "B ", "A+", "B ", "B ", "B+", "A-", "B+", "B+", "A-", "A-", "B+", "A-", "A-", "A-", "A-", "A-", "A ", "B+", "B ", "A-", "A-", "A-", "A ", "A-", "A-", "B+", "A-", "A ", "B+", "B+", "A-", "B+", "A-", "A ", "B+", "A ", "D ", "A+", "B ", "B+", "A+", "B+", "B ", "", "A ", "A-", "B ", "A ", "B+", "E ", "B ", "B ", "A ", "A-", "B ", "A ", "C+", "B ", "A ", "B+", "B+", "A ", "", "B ")> dput(dtpmgd)c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "A-", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "A ", "") dt_comb1gd <- coalesce(dtpaigd, dtpmgd)> dput(dt_comb1gd)c("C+", "B+", "C+", "B+", "C+", "A-", "A ", "B+", "A-", "C ", "B+", "A-", "B+", "B ", "B ", "B ", "D ", "B+", "B ", "B ", "C+", "B ", "A ", "A-", "B+", "A-", "A ", "B ", "A ", "C ", "B ", "A+", "C ", "B ", "B+", "C+", "B ", "B ", "C+", "B ", "B+", "B ", "A-", "B+", "B ", "A-", "C+", "A ", "A-", "C+", "C+", "B ", "B+", "A-", "A-", "B ", "E ", "A-", "B+", "B ", "A ", "B+", "A-", "A-", "A ", "B+", "A-", "A-", "A-", "B+", "C ", "A-", "A ", "A-", "A ", "A-", "A-", "A ", "A ", "B+", "B ", "A+", "B ", "B ", "B+", "A-", "B+", "B+", "A-", "A-", "B+", "A-", "A-", "A-", "A-", "A-", "A ", "B+", "B ", "A-", "A-", "A-", "A ", "A-", "A-", "B+", "A-", "A ", "B+", "B+", "A-", "B+", "A-", "A ", "B+", "A ", "D ", "A+", "B ", "B+", "A+", "B+", "B ", "", "A ", "A-", "B ", "A ", "B+", "E ", "B ", "B ", "A ", "A-", "B ", "A ", "C+", "B ", "A ", "B+", "B+", "A ", "", "B ") *Thank you and best regards.* *RoslinaZakaria* -- _* *_ _**_* *__ _*"Think Green. Keep it on the screen. If printing is necessary, please print it on both sides." *_ *The information contained in this e-mail message and any accompanying files is or may be confidential. If you are not the intended recipient, any use, dissemination, reliance, forwarding, printing or copying of this e-mail or any attached files is unauthorized. This e-mail is subject to copyright. No part of it should be reproduced, adapted or communicated without the written consent of the copyright owner. If you have received this e-mail in error please advise the sender immediately by return e-mail or telephone and delete all copies. UMP does not guarantee the accuracy or completeness of any information contained in this e-mail or attached files. Internet communications are not secure, therefore UMP does not accept legal responsibility for the contents of this message or attached files.* *Universiti Malaysia Pahang <http://www.ump.edu.my>* [[alternative HTML version deleted]]
On 18/11/2021 09:02, ROSLINAZAIRIMAH BINTI ZAKARIA . wrote:> Dear all, > > I try to merge two columns consisting of characters using the 'coalesce' > function from dplyr package. However, two data still have not merged, data > no. 124 1nd 143. Any help is very much appreciated. I provide the data as > follows.Those two indicies are "" in the dtpaigd list. So they're skipped. That's what coalesce does. If you want to push the values from dtpmgd into dtpaigd, wherever dtpaigd is an empty string, maybe this will help: dt_combined <- ifelse(dtpaigd == "", dtpmgd, dtpaigd)>> dput(dtpaigd) > c("C+", "B+", "C+", "B+", "C+", "A-", "A ", "B+", "A-", "C ", > "B+", "A-", "B+", "B ", "B ", "B ", "D ", "B+", "B ", "B ", "C+", > "B ", "A ", "A-", "B+", "A-", "A ", "B ", "A ", "C ", "B ", "A+", > "C ", "B ", "B+", "C+", "B ", "B ", "C+", "B ", "B+", "B ", "A-", > "B+", "B ", "A-", "C+", "A ", "A-", "C+", "C+", "B ", "B+", "A-", > "A-", "B ", "E ", "A-", "B+", "B ", "A ", "B+", "A-", "A-", "A ", > "B+", "A-", "A-", "A-", "B+", "C ", "A-", "A ", "A-", "A ", "A-", > "A-", "A ", "A ", "B+", "B ", "A+", "B ", "B ", "B+", "A-", "B+", > "B+", "A-", "A-", "B+", "A-", "A-", "A-", "A-", "A-", "A ", "B+", > "B ", "A-", "A-", "A-", "A ", "A-", "A-", "B+", "A-", "A ", "B+", > "B+", "A-", "B+", "A-", "A ", "B+", "A ", "D ", "A+", "B ", "B+", > "A+", "B+", "B ", "", "A ", "A-", "B ", "A ", "B+", "E ", "B ", > "B ", "A ", "A-", "B ", "A ", "C+", "B ", "A ", "B+", "B+", "A ", > "", "B ") >> dput(dtpmgd) > c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", > "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", > "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", > "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", > "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", > "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", > "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", > "", "", "", "", "", "", "", "", "", "", "", "", "A-", "", "", > "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", > "A ", "") > > dt_comb1gd <- coalesce(dtpaigd, dtpmgd) >> dput(dt_comb1gd) > c("C+", "B+", "C+", "B+", "C+", "A-", "A ", "B+", "A-", "C ", > "B+", "A-", "B+", "B ", "B ", "B ", "D ", "B+", "B ", "B ", "C+", > "B ", "A ", "A-", "B+", "A-", "A ", "B ", "A ", "C ", "B ", "A+", > "C ", "B ", "B+", "C+", "B ", "B ", "C+", "B ", "B+", "B ", "A-", > "B+", "B ", "A-", "C+", "A ", "A-", "C+", "C+", "B ", "B+", "A-", > "A-", "B ", "E ", "A-", "B+", "B ", "A ", "B+", "A-", "A-", "A ", > "B+", "A-", "A-", "A-", "B+", "C ", "A-", "A ", "A-", "A ", "A-", > "A-", "A ", "A ", "B+", "B ", "A+", "B ", "B ", "B+", "A-", "B+", > "B+", "A-", "A-", "B+", "A-", "A-", "A-", "A-", "A-", "A ", "B+", > "B ", "A-", "A-", "A-", "A ", "A-", "A-", "B+", "A-", "A ", "B+", > "B+", "A-", "B+", "A-", "A ", "B+", "A ", "D ", "A+", "B ", "B+", > "A+", "B+", "B ", "", "A ", "A-", "B ", "A ", "B+", "E ", "B ", > "B ", "A ", "A-", "B ", "A ", "C+", "B ", "A ", "B+", "B+", "A ", > "", "B ") > > *Thank you and best regards.* > > *RoslinaZakaria* >-- Micha Silver Ben Gurion Univ. Sde Boker, Remote Sensing Lab cell: +972-523-665918
Hello, Use an index giving the "" positions. i <- nchar(dtpmgd) == 0L dtpmgd[i] <- dtpaigd[i] Or, in one line, dtpmgd[nchar(dtpmgd) == 0L] <- dtpaigd[nchar(dtpmgd) == 0L] Hope this helps, Rui Barradas ?s 07:02 de 18/11/21, ROSLINAZAIRIMAH BINTI ZAKARIA . escreveu:> Dear all, > > I try to merge two columns consisting of characters using the 'coalesce' > function from dplyr package. However, two data still have not merged, data > no. 124 1nd 143. Any help is very much appreciated. I provide the data as > follows. > >> dput(dtpaigd) > c("C+", "B+", "C+", "B+", "C+", "A-", "A ", "B+", "A-", "C ", > "B+", "A-", "B+", "B ", "B ", "B ", "D ", "B+", "B ", "B ", "C+", > "B ", "A ", "A-", "B+", "A-", "A ", "B ", "A ", "C ", "B ", "A+", > "C ", "B ", "B+", "C+", "B ", "B ", "C+", "B ", "B+", "B ", "A-", > "B+", "B ", "A-", "C+", "A ", "A-", "C+", "C+", "B ", "B+", "A-", > "A-", "B ", "E ", "A-", "B+", "B ", "A ", "B+", "A-", "A-", "A ", > "B+", "A-", "A-", "A-", "B+", "C ", "A-", "A ", "A-", "A ", "A-", > "A-", "A ", "A ", "B+", "B ", "A+", "B ", "B ", "B+", "A-", "B+", > "B+", "A-", "A-", "B+", "A-", "A-", "A-", "A-", "A-", "A ", "B+", > "B ", "A-", "A-", "A-", "A ", "A-", "A-", "B+", "A-", "A ", "B+", > "B+", "A-", "B+", "A-", "A ", "B+", "A ", "D ", "A+", "B ", "B+", > "A+", "B+", "B ", "", "A ", "A-", "B ", "A ", "B+", "E ", "B ", > "B ", "A ", "A-", "B ", "A ", "C+", "B ", "A ", "B+", "B+", "A ", > "", "B ") >> dput(dtpmgd) > c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", > "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", > "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", > "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", > "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", > "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", > "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", > "", "", "", "", "", "", "", "", "", "", "", "", "A-", "", "", > "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", > "A ", "") > > dt_comb1gd <- coalesce(dtpaigd, dtpmgd) >> dput(dt_comb1gd) > c("C+", "B+", "C+", "B+", "C+", "A-", "A ", "B+", "A-", "C ", > "B+", "A-", "B+", "B ", "B ", "B ", "D ", "B+", "B ", "B ", "C+", > "B ", "A ", "A-", "B+", "A-", "A ", "B ", "A ", "C ", "B ", "A+", > "C ", "B ", "B+", "C+", "B ", "B ", "C+", "B ", "B+", "B ", "A-", > "B+", "B ", "A-", "C+", "A ", "A-", "C+", "C+", "B ", "B+", "A-", > "A-", "B ", "E ", "A-", "B+", "B ", "A ", "B+", "A-", "A-", "A ", > "B+", "A-", "A-", "A-", "B+", "C ", "A-", "A ", "A-", "A ", "A-", > "A-", "A ", "A ", "B+", "B ", "A+", "B ", "B ", "B+", "A-", "B+", > "B+", "A-", "A-", "B+", "A-", "A-", "A-", "A-", "A-", "A ", "B+", > "B ", "A-", "A-", "A-", "A ", "A-", "A-", "B+", "A-", "A ", "B+", > "B+", "A-", "B+", "A-", "A ", "B+", "A ", "D ", "A+", "B ", "B+", > "A+", "B+", "B ", "", "A ", "A-", "B ", "A ", "B+", "E ", "B ", > "B ", "A ", "A-", "B ", "A ", "C+", "B ", "A ", "B+", "B+", "A ", > "", "B ") > > *Thank you and best regards.* > > *RoslinaZakaria* >
Hi RosalinaZakaria, Talk about using a sledgehammer to crack a nut. In your example the two objects are character vectors. How about: dt_comb1gd <-paste0(dtpaigd,dtpmgd) Jim On Fri, Nov 19, 2021 at 2:15 AM ROSLINAZAIRIMAH BINTI ZAKARIA . <roslinazairimah at ump.edu.my> wrote:> > Dear all, > > I try to merge two columns consisting of characters using the 'coalesce' > function from dplyr package. However, two data still have not merged, data > no. 124 1nd 143. Any help is very much appreciated. I provide the data as > follows. > > > dput(dtpaigd) > c("C+", "B+", "C+", "B+", "C+", "A-", "A ", "B+", "A-", "C ", > "B+", "A-", "B+", "B ", "B ", "B ", "D ", "B+", "B ", "B ", "C+", > "B ", "A ", "A-", "B+", "A-", "A ", "B ", "A ", "C ", "B ", "A+", > "C ", "B ", "B+", "C+", "B ", "B ", "C+", "B ", "B+", "B ", "A-", > "B+", "B ", "A-", "C+", "A ", "A-", "C+", "C+", "B ", "B+", "A-", > "A-", "B ", "E ", "A-", "B+", "B ", "A ", "B+", "A-", "A-", "A ", > "B+", "A-", "A-", "A-", "B+", "C ", "A-", "A ", "A-", "A ", "A-", > "A-", "A ", "A ", "B+", "B ", "A+", "B ", "B ", "B+", "A-", "B+", > "B+", "A-", "A-", "B+", "A-", "A-", "A-", "A-", "A-", "A ", "B+", > "B ", "A-", "A-", "A-", "A ", "A-", "A-", "B+", "A-", "A ", "B+", > "B+", "A-", "B+", "A-", "A ", "B+", "A ", "D ", "A+", "B ", "B+", > "A+", "B+", "B ", "", "A ", "A-", "B ", "A ", "B+", "E ", "B ", > "B ", "A ", "A-", "B ", "A ", "C+", "B ", "A ", "B+", "B+", "A ", > "", "B ") > > dput(dtpmgd) > c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", > "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", > "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", > "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", > "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", > "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", > "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", > "", "", "", "", "", "", "", "", "", "", "", "", "A-", "", "", > "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", > "A ", "") > > dt_comb1gd <- coalesce(dtpaigd, dtpmgd) > > dput(dt_comb1gd) > c("C+", "B+", "C+", "B+", "C+", "A-", "A ", "B+", "A-", "C ", > "B+", "A-", "B+", "B ", "B ", "B ", "D ", "B+", "B ", "B ", "C+", > "B ", "A ", "A-", "B+", "A-", "A ", "B ", "A ", "C ", "B ", "A+", > "C ", "B ", "B+", "C+", "B ", "B ", "C+", "B ", "B+", "B ", "A-", > "B+", "B ", "A-", "C+", "A ", "A-", "C+", "C+", "B ", "B+", "A-", > "A-", "B ", "E ", "A-", "B+", "B ", "A ", "B+", "A-", "A-", "A ", > "B+", "A-", "A-", "A-", "B+", "C ", "A-", "A ", "A-", "A ", "A-", > "A-", "A ", "A ", "B+", "B ", "A+", "B ", "B ", "B+", "A-", "B+", > "B+", "A-", "A-", "B+", "A-", "A-", "A-", "A-", "A-", "A ", "B+", > "B ", "A-", "A-", "A-", "A ", "A-", "A-", "B+", "A-", "A ", "B+", > "B+", "A-", "B+", "A-", "A ", "B+", "A ", "D ", "A+", "B ", "B+", > "A+", "B+", "B ", "", "A ", "A-", "B ", "A ", "B+", "E ", "B ", > "B ", "A ", "A-", "B ", "A ", "C+", "B ", "A ", "B+", "B+", "A ", > "", "B ") > > *Thank you and best regards.* > > *RoslinaZakaria* > > -- > _* > *_ > _**_* > *__ > _*"Think Green. Keep it on the screen. > If printing is > necessary, please print it on both sides." > *_ > > *The information contained > in this e-mail message and any accompanying files is or may be > confidential. If you are not the intended recipient, any use, > dissemination, reliance, forwarding, printing or copying of this e-mail or > any attached files is unauthorized. This e-mail is subject to copyright. No > part of it should be reproduced, adapted or communicated without the > written consent of the copyright owner. If you have received this e-mail in > error please advise the sender immediately by return e-mail or telephone > and delete all copies. UMP does not guarantee the accuracy or completeness > of any information contained in this e-mail or attached files. Internet > communications are not secure, therefore UMP does not accept legal > responsibility for the contents of this message or attached files.* > *Universiti Malaysia Pahang <http://www.ump.edu.my>* > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.