R:merge()中ALL和all之间的区别是什么?

时间:2017-04-12 07:47:02

标签: r merge uppercase lowercase

尝试https://stat.ethz.ch/R-manual/R-devel/library/base/html/merge.html中的示例。 然后我发现当我们将ALL = TRUE和all = TRUE设置为merge()参数时,我得到了不同的结果(一个有6行而另一个有7行)。 发生了什么?任何人都可以提供一些提示吗?感谢。

> authors <- data.frame(
+     surname = I(c("Tukey", "Venables", "Tierney", "Ripley", "McNeil")),
+     nationality = c("US", "Australia", "US", "UK", "Australia"),
+     deceased = c("yes", rep("no", 4)))
> books <- data.frame(
+     name = I(c("Tukey", "Venables", "Tierney",
+                "Ripley", "Ripley", "McNeil", "R Core")),
+     title = c("Exploratory Data Analysis",
+               "Modern Applied Statistics ...",
+               "LISP-STAT",
+               "Spatial Statistics", "Stochastic Simulation",
+               "Interactive Data Analysis",
+               "An Introduction to R"),
+     other.author = c(NA, "Ripley", NA, NA, NA, NA,
+                      "Venables & Smith"))

> m1<-merge(authors, books, by.x = "surname", by.y = "name", ALL = TRUE)
> m1
   surname nationality deceased                         title other.author
1   McNeil   Australia       no     Interactive Data Analysis         <NA>
2   Ripley          UK       no            Spatial Statistics         <NA>
3   Ripley          UK       no         Stochastic Simulation         <NA>
4  Tierney          US       no                     LISP-STAT         <NA>
5    Tukey          US      yes     Exploratory Data Analysis         <NA>
6 Venables   Australia       no Modern Applied Statistics ...       Ripley
> m2<-merge(authors, books, by.x = "surname", by.y = "name", all = TRUE)
> m2
   surname nationality deceased                         title     other.author
1   McNeil   Australia       no     Interactive Data Analysis             <NA>
2   R Core        <NA>     <NA>          An Introduction to R Venables & Smith
3   Ripley          UK       no            Spatial Statistics             <NA>
4   Ripley          UK       no         Stochastic Simulation             <NA>
5  Tierney          US       no                     LISP-STAT             <NA>
6    Tukey          US      yes     Exploratory Data Analysis             <NA>
7 Venables   Australia       no Modern Applied Statistics ...           Ripley

1 个答案:

答案 0 :(得分:2)

您观察此行为的原因是ALL不是merge的有效参数(它是小写all)。如果指定ALL = TRUEmerge不会抛出错误,因为它允许通过...添加其他参数。在您的情况下,参数被忽略。因此,当您设置ALL = TRUE时,merge只使用默认值all = FALSE