您遇到的最大R-陷阱是什么? [英] What's the biggest R-gotcha you've run across?

查看:91
本文介绍了您遇到的最大R-陷阱是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一天您真的感到有些惊讶吗?我想我们都将从共享这些内容中受益.

Is there a certain R-gotcha that had you really surprised one day? I think we'd all gain from sharing these.

这是我的:在列表索引中,my.list[[1]]不是my.list[1].在R的早期就学到了这一点.

Here's mine: in list indexing, my.list[[1]] is not my.list[1]. Learned this in the early days of R.

推荐答案

删除数据框中的行将导致添加非唯一命名的行,这将导致错误:

> a<-data.frame(c(1,2,3,4),c(4,3,2,1))
> a<-a[-3,]
> a
  c.1..2..3..4. c.4..3..2..1.
1             1             4
2             2             3
4             4             1
> a[4,1]<-1
> a
Error in data.frame(c.1..2..3..4. = c("1", "2", "4", "1"), c.4..3..2..1. = c(" 4",  : 
  duplicate row.names: 4

所以这里发生的是:

  1. 创建了四行data.frame,所以行名是c(1,2,3,4)

  1. A four row data.frame is created, so the rownames are c(1,2,3,4)

第三行被删除,因此行名称为c(1,2,4)

The third row is deleted, so the rownames are c(1,2,4)

添加了第四行,R自动将行名设置为等于索引的索引,即4,因此行名为c(1,2,4,4).这是非法的,因为行名应该是唯一的.我不明白R为什么要允许这种行为.在我看来R应该提供唯一的行名.

A fourth row is added, and R automatically sets the row name equal to the index i.e. 4, so the row names are c(1,2,4,4). This is illegal because row names should be unique. I don't see why this type of behavior should be allowed by R. It seems to me that R should provide a unique row name.

这篇关于您遇到的最大R-陷阱是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆