Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 我需要table()的输出为正方形(如果需要,用零填充)_R - Fatal编程技术网

R 我需要table()的输出为正方形(如果需要,用零填充)

R 我需要table()的输出为正方形(如果需要,用零填充),r,R,我在比较一些分类器。我的程序是用table命令计算混淆矩阵,然后从表中计算假阳性率和真阳性率。我编写的例程要求表格是正方形的。应该有一个简单的方法 我的设置: cm您应该能够将分类器$teacher和分类器$srAve转换为因子,但我只是在猜测,因为我不知道您的数据是什么样的 > x <- factor(sample(0:4,20,TRUE)) > y <- factor(sample(1:3,20,TRUE),levels=levels(x)) > z <-

我在比较一些分类器。我的程序是用table命令计算混淆矩阵,然后从表中计算假阳性率和真阳性率。我编写的例程要求表格是正方形的。应该有一个简单的方法

我的设置:


cm您应该能够将
分类器$teacher
分类器$srAve
转换为因子,但我只是在猜测,因为我不知道您的数据是什么样的

> x <- factor(sample(0:4,20,TRUE))
> y <- factor(sample(1:3,20,TRUE),levels=levels(x))
> z <- data.frame(x,y)
> table(z)
   y
x   0 1 2 3 4
  0 0 1 2 0 0
  1 0 1 0 1 0
  2 0 2 2 1 0
  3 0 3 3 2 0
  4 0 0 2 0 0
> z$y <- as.character(y)
> table(z)
   y
x   1 2 3
  0 1 2 0
  1 1 0 1
  2 2 2 1
  3 3 3 2
  4 0 2 0
>x y z表(z)
Y
x1234
0 0 1 2 0 0
1 0 1 0 1 0
2 0 2 2 1 0
3 0 3 3 2 0
4 0 0 2 0 0
>z$y表格(z)
Y
x123
0 1 2 0
1 1 0 1
2 2 2 1
3 3 3 2
4 0 2 0

您应该能够将
分类器$teacher
分类器$srAve
转换为因子,但我只是在猜测,因为我不知道您的数据是什么样的

> x <- factor(sample(0:4,20,TRUE))
> y <- factor(sample(1:3,20,TRUE),levels=levels(x))
> z <- data.frame(x,y)
> table(z)
   y
x   0 1 2 3 4
  0 0 1 2 0 0
  1 0 1 0 1 0
  2 0 2 2 1 0
  3 0 3 3 2 0
  4 0 0 2 0 0
> z$y <- as.character(y)
> table(z)
   y
x   1 2 3
  0 1 2 0
  1 1 0 1
  2 2 2 1
  3 3 3 2
  4 0 2 0
>x y z表(z)
Y
x1234
0 0 1 2 0 0
1 0 1 0 1 0
2 0 2 2 1 0
3 0 3 3 2 0
4 0 0 2 0 0
>z$y表格(z)
Y
x123
0 1 2 0
1 1 0 1
2 2 2 1
3 3 3 2
4 0 2 0

请提供一些示例数据好吗?您可以将表代码简化为
xtab(~teacher+srAve,subset(classifiers,problem==“problem27”))
。James,xtab函数要简单得多!(仍然不是正方形,但我喜欢!)请提供一些示例数据好吗?您可以将表代码简化为
xtab(~teacher+srAve,subset(classifiers,problem==“problem27”))
。James,xtab函数要简单得多!(仍然不是正方形,但我喜欢!)不知道哪些级别在
x
中,哪些级别在
y
中,您可以执行
levels(x)=排序(union(levels(x),levels(y))
levels(y)=levels(x)
,这很有意义。我试图将srAve和teacher转换为factors,但我没有将“额外”级别添加到srAve中。谢谢大家!不知道哪些级别在
x
中,哪些级别在
y
中,您可以执行有意义的
levels(x)=排序(union(levels(x),levels(y))
levels(y)=levels(x)
。我试图将srAve和teacher转换为factors,但我没有将“额外”级别添加到srAve中。谢谢大家!
> classifiers$teacher[which(classifiers$problem == 'problem27')]
[1] 0 0 1 2 2 2 0 0 0 0 0 2 0 0 2 0 4 3 0 0 2 2 0 2 0 0 2 2 1 0 0 2 1 0 1 2 0 0
[39] 0 1 0 0 1
> classifiers$srAve[which(classifiers$problem == 'problem27')]
[1] 1 1 2 2 2 2 1 1 1 1 1 1 1 1 1 1 3 2 1 1 2 2 1 2 1 1 2 2 1 1 1 2 2 1 2 2 1 1
[39] 1 2 1 1 1
> x <- factor(sample(0:4,20,TRUE))
> y <- factor(sample(1:3,20,TRUE),levels=levels(x))
> z <- data.frame(x,y)
> table(z)
   y
x   0 1 2 3 4
  0 0 1 2 0 0
  1 0 1 0 1 0
  2 0 2 2 1 0
  3 0 3 3 2 0
  4 0 0 2 0 0
> z$y <- as.character(y)
> table(z)
   y
x   1 2 3
  0 1 2 0
  1 1 0 1
  2 2 2 1
  3 3 3 2
  4 0 2 0