R似乎忽略了下划线后的部分变量名
R seems to ignore part of variable name after underscore
我遇到了一个奇怪的 R 问题。我有一个包含多个变量的数据框。我向包含下划线的此数据框添加一个变量,例如:
allres$tmp_weighted <- allres$day * allres$area
在我这样做之前,R 告诉我变量 allres$tmp
不存在(这是正确的)。但是,在我将 allres$tmp_weighted
添加到数据框并调用 allres$tmp
之后,我得到了 allres$tmp_weighted
的数据。似乎下划线后面的部分对 R 根本无关紧要。我用其他几个变量/名称尝试过它,它总是这样工作
我认为这不应该像这样工作?我在这里忽略了什么吗?下面我将一些代码与控制台的输出粘贴在一起。
# first check whether variable exists
allres_sw$Ndpsw
> NULL
#define new variable with underscore in variable name
allres_sw$Ndpsw_weighted <- allres_sw$Ndepswcrit * allres_sw$Area
#check again whether variable exists
allres_sw$Ndpsw
> [1] 17.96480 217.50240 44.84415 42.14560 0.00000 43.14444 53.98650 9.81939 0.00000 110.67720
# this is the output that I would expect from "Ndpsw_weighted" - and indeed do get
allres_sw$Ndpsw_weighted
> [1] 17.96480 217.50240 44.84415 42.14560 0.00000 43.14444 53.98650 9.81939 0.00000 110.67720
在您的 R 控制台中查看 ?`[`
或 ?`$`
。如果您查看 extract 函数的 name
参数,它指出在使用 $
运算符时名称部分匹配(与 `[[`
运算符相反,后者使用精确匹配基于 exact = TRUE
参数的匹配。
来自?`$`
A literal character string or a name (possibly backtick quoted). For extraction, this is normally (see under ‘Environments’) partially matched to the names of the object.
只是为了扩展 Wil 的回答... 来自 help('$')
:
x$name
name
A literal character string or a name (possibly backtick
quoted). For extraction, this is normally (see under
‘Environments’) partially matched to the names
of the object.
x$name
is equivalent to
x[["name", exact = FALSE]]
. Also, the partial matching
behavior of [[
can be controlled using the exact
argument.
exact
Controls possible partial matching of [[
when
extracting by a character vector (for most objects, but see under
‘Environments’). The default is no partial matching. Value
NA
allows partial matching but issues a warning when it
occurs. Value FALSE
allows partial matching without any
warning.
此处的关键词是部分匹配(参见pmatch
)。您现在会明白下划线没有什么特别之处 - 您可以将 allres_sw$Ndpsw_weighted
缩写为 allres_sw$Ndp
,前提是没有名称比 allres_sw$Ndepswcrit
.
更相似
我遇到了一个奇怪的 R 问题。我有一个包含多个变量的数据框。我向包含下划线的此数据框添加一个变量,例如:
allres$tmp_weighted <- allres$day * allres$area
在我这样做之前,R 告诉我变量 allres$tmp
不存在(这是正确的)。但是,在我将 allres$tmp_weighted
添加到数据框并调用 allres$tmp
之后,我得到了 allres$tmp_weighted
的数据。似乎下划线后面的部分对 R 根本无关紧要。我用其他几个变量/名称尝试过它,它总是这样工作
我认为这不应该像这样工作?我在这里忽略了什么吗?下面我将一些代码与控制台的输出粘贴在一起。
# first check whether variable exists
allres_sw$Ndpsw
> NULL
#define new variable with underscore in variable name
allres_sw$Ndpsw_weighted <- allres_sw$Ndepswcrit * allres_sw$Area
#check again whether variable exists
allres_sw$Ndpsw
> [1] 17.96480 217.50240 44.84415 42.14560 0.00000 43.14444 53.98650 9.81939 0.00000 110.67720
# this is the output that I would expect from "Ndpsw_weighted" - and indeed do get
allres_sw$Ndpsw_weighted
> [1] 17.96480 217.50240 44.84415 42.14560 0.00000 43.14444 53.98650 9.81939 0.00000 110.67720
在您的 R 控制台中查看 ?`[`
或 ?`$`
。如果您查看 extract 函数的 name
参数,它指出在使用 $
运算符时名称部分匹配(与 `[[`
运算符相反,后者使用精确匹配基于 exact = TRUE
参数的匹配。
来自?`$`
A literal character string or a name (possibly backtick quoted). For extraction, this is normally (see under ‘Environments’) partially matched to the names of the object.
只是为了扩展 Wil 的回答... 来自 help('$')
:
x$name
name
A literal character string or a name (possibly backtick quoted). For extraction, this is normally (see under ‘Environments’) partially matched to thenames
of the object.
x$name
is equivalent tox[["name", exact = FALSE]]
. Also, the partial matching behavior of[[
can be controlled using theexact
argument.
exact
Controls possible partial matching of[[
when extracting by a character vector (for most objects, but see under ‘Environments’). The default is no partial matching. ValueNA
allows partial matching but issues a warning when it occurs. ValueFALSE
allows partial matching without any warning.
此处的关键词是部分匹配(参见pmatch
)。您现在会明白下划线没有什么特别之处 - 您可以将 allres_sw$Ndpsw_weighted
缩写为 allres_sw$Ndp
,前提是没有名称比 allres_sw$Ndepswcrit
.