楼主: 殷红已
13520 6

[学习分享] 不说人话的R报错信息 [推广有奖]

  • 0关注
  • 9粉丝

等待验证会员

大专生

3%

还不是VIP/贵宾

-

威望
0
论坛币
129 个
通用积分
0
学术水平
18 点
热心指数
24 点
信用等级
18 点
经验
617 点
帖子
19
精华
0
在线时间
20 小时
注册时间
2014-1-22
最后登录
2017-1-5

楼主
殷红已 发表于 2014-5-30 16:27:40 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
此文为译文,谨此来纪念那些被R不知所云的稀奇古怪的报错折磨过的凄凉岁月…

Translating Weird R Errors
January 20, 2013
By Slawa Rokicki

原文写的很风趣,时间所限我就简单的翻译一下了。



1. 其实我只是拼错了变量名…

运行这段代码:
prob1<-as.data.frame(cbind(c(1,2,3),c(5,4,3))) colnames(prob1)<-c(“Education”,”Ethnicity”) table(prob1$education, prob1$Ethnicity)
1
2
3

prob1<-as.data.frame(cbind(c(1,2,3),c(5,4,3)))
colnames(prob1)<-c(“Education”,”Ethnicity”)
table(prob1$education, prob1$Ethnicity)

然后R会报错:
all arguments must have the same length
1

all arguments must have the same length

莫名其妙有木有?其实正确的应该是:
table(prob1$Education, prob1$Ethnicity)
1

table(prob1$Education, prob1$Ethnicity)

我只是忘了大写了…囧。

2. 我只是调用了不存在的变量….

比如我运行:
prob1$gender_recode <-as.numeric(prob1$Gender==2)
1

prob1$gender_recode <-as.numeric(prob1$Gender==2)

然后就会报错:
replacement has 0 rows, data has 3
1

replacement has 0 rows, data has 3

但是这样就没问题:
prob1$Educ_recode<-as.numeric(prob1$Education==2)
1

prob1$Educ_recode<-as.numeric(prob1$Education==2)

原因只是gender这个变量不存在….你就不能直接告诉我找不到变量么?

3. 找不到变量?

我这次确保Education是有的,但是居然还是报错?
nrow(prob1[prob1$Education!=1])
1

nrow(prob1[prob1$Education!=1])

报错:
undefined columns selected
1

undefined columns selected

而人家只是少打了一个逗号而已嘛…
nrow(prob1[prob1$Education!=1,])
1

nrow(prob1[prob1$Education!=1,])

哎,你就不能直接报语法错误嘛!

原文附下:

I love R. I think it’s intuitive and clever and overall a great language. But I do get really annoyed sometimes at the completely ridiculous, cryptic error messages it often gives me. This post will go over some of those seemingly nonsensical errors so you don’t have to go crazy trying to find the bug in your code.

1. all arguments must have the same length

To start with, I just make up some quick data:
prob1<-as.data.frame(cbind(c(1,2,3),c(5,4,3))) colnames(prob1)<-c(“Education”,”Ethnicity”)
1
2

prob1<-as.data.frame(cbind(c(1,2,3),c(5,4,3)))
colnames(prob1)<-c(“Education”,”Ethnicity”)

And now I just want to do a simple table but I get this error:

all arguments must have the same length

What the heck. I look back at my dataset and make sure that both those variables are the same length, which they do. The problem here is that I misspelled “Education”. There’s a missing “a” in there and instead of telling me that I referenced a variable that doesn’t exist, R bizarrely tells me to check the length of my variables. Remember: Anytime you get an error, check to make sure you’ve spelled everything right.

If I do this, everything works out great:
table(prob1$Education, prob1$Ethnicity)
1

table(prob1$Education, prob1$Ethnicity)

2. replacement has 0 rows, data has 3

A very similar problem, with a very different error message. Let’s say I forgot what columns were in my prob1 data and I thought I had a Sex indicator in there. So I try to recode it like this:

This error message is also pretty unhelpful. The syntax is totally correct; the problem is that I just don’t have a variable named Sex in my dataset. If I do this instead to recode education, a variable that exists, everything is fine:
prob1$Educ_recode<-as.numeric(prob1$Education==2)
1

prob1$Educ_recode<-as.numeric(prob1$Education==2)

3. undefined columns selected

Ironically, the error we so badly wanted before comes up but for a completely different reason. See if you can find the problem here. I’ll take that same little dataset and I just want to know how many rows there are in which Education is not equal to 1.

So, if I want to know the number of rows of the dataframe prob1, I do:

nrow(prob1)

and if I want to know how many have a value of Education not equal to 1, I do the following (incorrectly) and get an error:

Now I check my variable name and I’ve definitely spelled Education right this time. The problem, actually, is not that I have referenced a column that doesn’t exist but I’ve messed up the syntax to the nrow() function, in that I haven’t defined what columns I want to subset. When I do,
prob1[prob1$Education!=1]
1

prob1[prob1$Education!=1]

this doesn’t make any sense, because I’m saying to subset prob1 but to do this I have to specify which rows I want and which columns I want. This just lists one condition in the brackets and it’s unclear whether it’s for the rows or columns. See my post on subsetting for more details on this.

If I do it the following way, all is good since I’m saying to subset prob1 with only rows with education !=1 and all columns:
nrow(prob1[prob1$Education!=1,])
1

nrow(prob1[prob1$Education!=1,])

本文固定链接: http://www.ppvke.com/Blog/archives/4286
转载请注明: PPV课 小编 2014年05月27日 于 大数据 V博 发表
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Translating January Errors Trans latin 信息

已有 1 人评分经验 论坛币 收起 理由
ltx5151 + 80 + 40 精彩帖子

总评分: 经验 + 80  论坛币 + 40   查看全部评分

沙发
panxinfeng 发表于 2014-5-31 00:05:26
有意思!

藤椅
淘宝网橙迷橙橙 发表于 2014-5-31 21:31:35
不做死就不会死。这说明,养成好的输码习惯以及高度的细心是多么重要。。。
首先是尽可能减少自己的错误,不要希望软件来帮你纠正错误。。。

板凳
complicated 在职认证  发表于 2014-6-3 09:38:50
哈哈,R的报错有时候真是没法看
密码被盗??

报纸
asdasdaseee 发表于 2014-6-3 13:25:49
有意思 哈哈

地板
马甲1号 发表于 2014-6-3 22:43:47
多用traceback().
归欤!归欤!吾党之小子狂简,斐然成章,不知所以裁之。

7
ltx5151 发表于 2014-6-4 09:43:49
其实,报错这个东西很难做,因为有正确的报错信息,说明写程序的人预计到了你出现的错误。但是其实不太可能把用户的所有使用方式的考虑到,因此报错信息有时不知所云。尤其是开源软件,大部分的函数包是由一两个人编写的,就不要太强求啦。

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-29 20:14