对于两个示例数据帧:
df1 <- structure(list(name = c("Katie", "Eve", "James", "Alexander",
"Mary", "Barrie", "Harry", "Sam"), postcode = c("CB12FR", "CB12FR",
"NE34TR", "DH34RL", "PE46YH", "IL57DS", "IP43WR", "IL45TR")), .Names = c("name",
"postcode"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-8L), spec = structure(list(cols = structure(list(name = structure(list(), class = c("collector_character",
"collector")), postcode = structure(list(), class = c("collector_character",
"collector"))), .Names = c("name", "postcode")), default = structure(list(), class = c("collector_guess",
"collector"))), .Names = c("cols", "default"), class = "col_spec"))
df2 <-structure(list(name = c("Katie", "James", "Alexander", "Lucie",
"Mary", "Barrie", "Claire", "Harry", "Clare", "Hannah", "Rob",
"Eve", "Sarah"), postcode = c("CB12FR", "NE34TR", "DH34RL", "DL56TH",
"PE46YH", "IL57DS", "RE35TP", "IP43WQ", "BH35OP", "CB12FR", "DL56TH",
"CB12FR", "IL45TR"), rating = c(1L, 1L, 1L, 2L, 3L, 1L, 4L, 2L,
2L, 3L, 1L, 4L, 2L)), .Names = c("name", "postcode", "rating"
), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-13L), spec = structure(list(cols = structure(list(name = structure(list(), class = c("collector_character",
"collector")), postcode = structure(list(), class = c("collector_character",
"collector")), rating = structure(list(), class = c("collector_integer",
"collector"))), .Names = c("name", "postcode", "rating")), default = structure(list(), class = c("collector_guess",
"collector"))), .Names = c("cols", "default"), class = "col_spec"))
我想在df1中增加一列,以提供df2中的评分。每种邮递区号可能有多个等级(这就是为什么直接合并不起作用的原因。
我只想在邮政编码和名称的前3个字符相同的情况下合并两个数据帧(假设它们在df1中是唯一的)。例如,如果有一个Katherine和Katie-(都具有相同的邮政编码),则这些不会合并
我很高兴在没有合并的地方留空白。
有什么想法吗?
答案 0 :(得分:2)
用多列进行简单连接不会解决您的问题吗?像
SP.SOD.executeFunc('clientpeoplepicker.js', 'SPClientPeoplePicker', function() {
var pickerDiv = $("[id^='Employee_x0020_Name'][title='Employee Name']");
var picker = SPClientPeoplePicker.SPClientPeoplePickerDict[pickerDiv[0].id];
picker.OnUserResolvedClientScript = function(peoplePickerId, selectedUsersInfo) {
//It will get the desired display name of the people from picker div, similarly any other property can be accessed via selectedUsersInfo
var empname = selectedUsersInfo[0].DisplayText;
console.log(empname);
}
});
如果列名不匹配的替代解决方案,
df<-merge(x=df1,y=df2,by=c('name','postcode'),all.x=T)