df [,1:3]与df [,i:i + 2]之间的差异,i = 1

时间:2015-11-11 19:26:24

标签: r

我有一个数据框df1,如下所示。当我print(df1[,1:3])时,我得到了前3列,这是我的期望。但是当我使用i=1; print( df1[,i:i+2])时,我只能获得第3列。有人知道原因吗?

t.x    t.y t.Freq prop.row.x prop.row.y prop.row.Freq prop.col.x
1 Female   None     25     Female       None     0.4237288     Female
2   Male   None     17       Male       None     0.6800000       Male
3 Female   Some     12     Female       Some     0.2033898     Female
4   Male   Some      2       Male       Some     0.0800000       Male
5 Female Marked     22     Female     Marked     0.3728814     Female
6   Male Marked      6       Male     Marked     0.2400000       Male

2 个答案:

答案 0 :(得分:1)

不同之处在于,2已逐元素地添加到i:i1i = 1),因为:有更高的+评估优先级高于> i = 1L > i:i + 2L [1] 3 > i:(i + 2L) [1] 1 2 3

要按正确的顺序进行评估,冒号运算符的第二个参数需要括号,即

<div ng-app="app1" ng-controller="mcontrol as ctrl">
<div ng-repeat="x in ctrl.list">
<span ng-bind="x.name"></span>
</div>
</div>

<script src="angular.js"></script>

<script>
angular.module("app1", [])
.controller("mcontrol", ["$http", function($http)
{
    $http.get("myfile.php").then(function(response)
    {
        this.list = response.data;
    },
    function(errorresponse)
    {
        console.error("Error");
    });
}]);

</script>

答案 1 :(得分:0)

R FAQrelated question

  

优先规则记录在?语法中,并查看R如何解释表达式       你可以看一下解析树

R> as.list(quote(-2^2))
[[1]]
`-`

[[2]]
2^2

所以你可以用你的表达来做到这一点:

>  as.list(quote(i:i+2))
[[1]]
`+`

[[2]]
i:i

[[3]]
[1] 2

我建议您查看?Syntax以更全面地了解R中运算符的优先级。