合并未按预期工作:内部联接未返回第1列中的所有匹配行

时间:2018-11-05 00:40:47

标签: r merge

我有两个数据集x和y,并且我试图将第一列从x匹配到y的第一列,但是merge没有输出正确数量的匹配项。

示例:

>x
species
Anisognathus_lacrymosus 
Anisognathus_notabilis 
Bangsia_melanochlamys 
Cnemathraupis_aureodorsalis
Catamenia_homochroa 
Chrysothlypis_salmoni 
Conirostrum_bicolor 
Conirostrum_rufum 

>y
species ln.mass
Acanthidops_bairdi  2.772588722
Anisognathus_igniventris    3.526360525
Anisognathus_lacrymosus 3.433987204
Anisognathus_melanogenys    3.713572067
Anisognathus_notabilis  3.663561646
Anisognathus_somptuosus 3.784189634
Bangsia_arcaei  3.616308761

输出:

> merge(x,y)
[1] species ln.mass
<0 rows> (or 0-length row.names)

所需的输出:

species ln.mass
Anisognathus_lacrymosus 3.433987204
Anisognathus_notabilis  3.663561646

2 个答案:

答案 0 :(得分:1)

我尝试使用matchdplyr包并获得了输出。

x<- data.frame(species=c(
"Anisognathus_lacrymosus",
"Anisognathus_notabilis", 
"Bangsia_melanochlamys",
"Cnemathraupis_aureodorsalis",
"Catamenia_homochroa",
"Chrysothlypis_salmoni",
"Conirostrum_bicolor",
"Conirostrum_rufum" ))
y<-data.frame(species =c(
              "Acanthidops_bairdi",  
              "Anisognathus_igniventris",
              "Anisognathus_lacrymosus",
              "Anisognathus_melanogenys",    
              "Anisognathus_notabilis",
              "Anisognathus_somptuosus", 
              "Bangsia_arcaei"),
              ln.mass=c(2.772588722,3.526360525,3.433987204,3.713572067,3.663561646,3.784189634,3.616308761))

library(dplyr)
z<-y[match(x$species,y$species),]%>%na.omit()

输出:

                  species  ln.mass
3 Anisognathus_lacrymosus 3.433987
5  Anisognathus_notabilis 3.663562

答案 1 :(得分:0)

您应该加入物种领域。

function ProfileContextProvider(props) {
  let [profile, profileR] = React.useReducer(reducer, initialState);

  const customDispatch= useCallback(async (action) => {
    switch (action.type) {
      case "reload": {
        const profileData = await reloadProfile();
        profileR({
          type: "profileReady",
          payload: profileData
        });
        break;
      }
      default:
        // Not a special case, dispatch the action
        profileR(action);
    }
  }, []); // The empty array causes this callback to only be created once per component instance

  return (
    <ProfileContext.Provider value={{ profile, profileR: customDispatch }}>
      {props.children}
    </ProfileContext.Provider>
  );
}

更新:我执行了相同的代码,并且得到了您期望的输出 enter image description here

检查文本中是否有空格。

merge(x,y, by="species")