R是什么意思 - 个人喜好,命名约定或更多? [英] What does the dot mean in R – personal preference, naming convention or more?
问题描述
我(可能)没有引用所有其他变量,如 var1〜。
这里。
我再次指向 plyr
,并查看了 mlply
,并想知道为什么参数用引导点像这样:
I am (probably) NOT referring to the "all other variables" meaning like var1~.
here.
I was pointed to plyr
once again and looked into mlply
and wondered why parameters are defined with leading dot like this:
function (.data, .fun = NULL, ..., .expand = TRUE, .progress = "none",
.parallel = FALSE)
{
if (is.matrix(.data) & !is.list(.data))
.data <- .matrix_to_df(.data)
f <- splat(.fun)
alply(.data = .data, .margins = 1, .fun = f, ..., .expand = .expand,
.progress = .progress, .parallel = .parallel)
}
<environment: namespace:plyr>
这有什么用?它只是个人偏好,命名约定还是更多?通常R是如此的功能,我错过了一个很久以前做过的伎俩。
What's the use of that? Is it just personal preference, naming convention or more? Often R is so functional that I miss a trick that's long been done before.
推荐答案
函数名称中的点可以表示以下任何内容:
A dot in function name can mean any of the following:
- 根本没有
- 方法和类之间的分隔符
- 隐藏函数名
-
- nothing at all
- a separator between method and class in S3 methods
- to hide the function name
data.frame
中的点不分隔 data
从
框架
,而不是直观的。
The dot in data.frame
doesn't separate data
from frame
, other than visually.
plot
是通用S3方法的一个示例。因此 plot.lm
和 plot.glm
是调用或
plot(glm(...))
plot
is one example of a generic S3 method. Thus plot.lm
and plot.glm
are the underlying function definitions that are used when calling plot(lm(...))
or plot(glm(...))
在编写包时,在函数名中使用前导点有时很有用,因为这些函数在一般视图中有些隐藏。
When writing packages, it is sometimes useful to use leading dots in function names because these functions are somewhat hidden from general view. Functions that are meant to be purely internal to a package sometimes use this.
在这种情况下,有些隐藏只是意味着变量(或函数)不会通常在使用 ls()
列出对象时显示。要强制 ls
显示这些变量,请使用 ls(all.names = TRUE)
。通过使用点作为变量的第一个字母,您可以更改变量本身的范围。例如:
In this context, "somewhat hidden" simply means that the variable (or function) won't normally show up when you list object with ls()
. To force ls
to show these variables, use ls(all.names=TRUE)
. By using a dot as first letter of a variable, you change the scope of the variable itself. For example:
x <- 3
.x <- 4
ls()
[1] "x"
ls(all.names=TRUE)
[1] ".x" "x"
x
[1] 3
.x
[1] 4
< h2> 4。其他可能的原因
在Hadley的 plyr 包,他使用约定在函数名中使用前导点。这是一种尝试并确保在解析变量名称时,将值解析为用户变量而不是内部函数变量的机制。
4. Other possible reasons
In Hadley's plyr package, he uses the convention to use leading dots in function names. This as a mechanism to try and ensure that when resolving variable names, the values resolve to the user variables rather than internal function variables.
这种不同用途的混杂可能导致非常混乱的情况,因为这些不同的用途都可能混杂在同一个函数名称中。
This mishmash of different uses can lead to very confusing situations, because these different uses can all get mixed up in the same function name.
例如,要将 data.frame
转换为列表,您可以使用 as.list(.. )
For example, to convert a data.frame
to a list you use as.list(..)
as.list(iris)
在这种情况下, as.list
是一个S3通用方法, $ c> data.frame 。因此,S3函数称为 as.list.data.frame
:
In this case as.list
is a S3 generic method, and you are passing a data.frame
to it. Thus the S3 function is called as.list.data.frame
:
> as.list.data.frame
function (x, ...)
{
x <- unclass(x)
attr(x, "row.names") <- NULL
x
}
<environment: namespace:base>
对于真正壮观的东西,加载 data.table
package并查看函数 as.data.table.data.frame
:
And for something truly spectacular, load the data.table
package and look at the function as.data.table.data.frame
:
> library(data.table)
> methods(as.data.table)
[1] as.data.table.data.frame* as.data.table.data.table* as.data.table.matrix*
Non-visible functions are asterisked
> data.table:::as.data.table.data.frame
function (x, keep.rownames = FALSE)
{
if (keep.rownames)
return(data.table(rn = rownames(x), x, keep.rownames = FALSE))
attr(x, "row.names") = .set_row_names(nrow(x))
class(x) = c("data.table", "data.frame")
x
}
<environment: namespace:data.table>
这篇关于R是什么意思 - 个人喜好,命名约定或更多?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!