比较两个数据帧中的元素

时间:2019-12-04 20:42:34

标签: r dataframe dplyr stringr

我想比较两个分别来自data.framedf1的{​​{1}}中的元素。我想从他们那里构建一个名为df2的新data.frame。如果元素相等,则out中的元素为out,否则为1

MWE

0

预期输出

set.seed(1)
df1 <- data.frame(Q1 = sample(letters[1:5], 2, replace = TRUE), 
                 Q2 = sample(letters[1:5], 2, replace = TRUE))    
set.seed(2)
df2 <-  data.frame(Q1 = sample(letters[1:5], 2, replace = TRUE), 
                   Q2 = sample(letters[1:5], 2, replace = TRUE))

1 个答案:

答案 0 :(得分:2)

如果在创建$(document).ready(function(){ $("#submit-trial").click(function(event){ let cmpn = $("#company").val(); let eml = $("#email").val(); let phn = $("#phone").val(); let nt = $("#note").val(); let prsn = $("#person").val(); $.post("/trial-submission", { company: cmpn, person: prsn, phone: phn, email: eml, note: nt }) .done(function(){ $("#company").val(""); $("#email").val(""); $("#phone").val(""); $("#note").val(""); $("#person").val(""); alert("success"); }) .fail(function(){ alert("error"); }) .always(function(){ alert("finished"); }); }); }); -app.post("/trial-submission", function(req, res){ const data = req.body; let transporter = nodemailer.createTransport({ service: "mail", auth: { user: "send@mail.com", pass: "password" } }); let mailOptions = { from:'send@mail.com', to:'receive@mail.com', subject: 'testing', html:` Company: ${data.company}<br> Contact person: ${data.person}<br> Phone: ${data.phone}<br> E-mail: ${data.email}<br> Note: ${data.note} ` }; transporter.sendMail(mailOptions, function(err, data){ if(err){ console.log('Error Occurs', err); } else { console.log('Email Sent!'); } }); }); 的同时使用stringsAsFactors = FALSE创建数据集,则会造成困难,因为属性会带来比较困难)

data.frame

或者如果是factor,则用+(df1 == df2) 转换为factor

character

或利用type.convert hack方式更改为+(type.convert(df1, as.is = TRUE) == type.convert(df2, as.is = TRUE))

matrix