在R中使用mean()时出错:Ops.factor(obs,pred)中的错误:因素的级别集不同

时间:2019-04-08 16:11:31

标签: r mean

我正在使用如下所示的数据集:

> head(test_accuracy)
        observed predicted new_predicted
1 Moving/Feeding  Foraging      Standing
2       Standing  Foraging      Standing
3       Standing  Foraging      Standing
4       Standing  Foraging      Standing
5       Standing  Foraging      Standing
6       Standing  Foraging      Standing

我的问题很简单。我想通过比较test_accuracy$observedtest_accuracy$new_predicted之间的匹配百分比来计算分类的准确性。我只是使用下面的代码,但出现相关错误:

> head(test_accuracy)
        observed predicted new_predicted
1 Moving/Feeding  Foraging      Standing
2       Standing  Foraging      Standing
3       Standing  Foraging      Standing
4       Standing  Foraging      Standing
5       Standing  Foraging      Standing
6       Standing  Foraging      Standing
> obs<-as.factor(test_accuracy$observed)
> pred<-as.factor(test_accuracy$new_predicted)
> mean(obs == pred)
Error in Ops.factor(obs, pred) : level sets of factors are different

有人可以让我知道我做错了什么吗?我可以在下面上传一个dput()示例:

> dput(test_accuracy)
structure(list(observed = c("Moving/Feeding", "Standing", "Standing", 
"Standing", "Standing", "Standing", "Moving/Feeding", "Moving/Feeding", 
"Moving/Feeding", "Standing", "Standing", "Feeding/Moving", "Standing", 
"Standing", "Moving/Feeding", "Feeding/Moving", "Feeding/Moving", 
"Feeding/Moving", "Standing", "Standing", "Standing", "Standing", 
"Moving/Feeding", "Moving/Feeding", "Standing", "Moving/Feeding", 
"Moving/Feeding", "Moving/Feeding", "Moving/Feeding", "Moving/Feeding", 
"Standing", "Standing", "Standing", "Moving/Feeding", "Standing", 
"Standing", "Standing", "Standing", "Moving/Feeding", "Standing", 
"Moving/Feeding", "Moving/Feeding", "Moving/Feeding", "Standing", 
"Standing", "Moving/Feeding", "Moving/Feeding", "Moving/Feeding", 
"Moving/Feeding", "Moving/Feeding", "Standing", "Standing", "Standing", 
"Feeding/Moving", "Feeding/Moving", "Feeding/Moving", "Moving/Feeding", 
"Feeding/Moving", "Feeding/Moving", "Feeding/Moving", "Feeding/Moving", 
"Moving/Feeding", "Moving/Feeding", "Moving/Feeding", "Standing", 
"Standing", "Standing", "Standing", "Feeding/Moving", "Standing", 
"Standing", "Standing", "Standing", "Standing", "Moving/Feeding", 
"Standing", "Moving/Feeding", "Standing", "Standing", "Feeding/Moving", 
"Feeding/Moving", "Standing", "Moving/Feeding", "Moving/Feeding", 
"Standing", "Moving/Feeding", "Feeding/Moving", "Moving/Feeding", 
"Moving/Feeding", "Feeding/Moving", "Feeding/Moving", "Feeding/Moving", 
"Feeding/Moving", "Feeding/Moving", "Feeding/Moving", "Standing", 
"Standing", "Moving/Feeding", "Feeding/Moving", "Feeding/Moving", 
"Feeding/Moving", "Feeding/Moving", "Feeding/Moving", "Feeding/Moving", 
"Feeding/Moving", "Feeding/Moving", "Feeding/Moving", "Feeding/Moving", 
"Feeding/Moving", "Feeding/Moving", "Feeding/Moving", "Feeding/Moving", 
"Feeding/Moving", "Feeding/Moving", "Moving/Feeding", "Moving/Feeding", 
"Feeding/Moving", "Moving/Feeding", "Moving/Feeding", "Moving/Feeding", 
"Moving/Feeding", "Standing", "Moving/Feeding", "Moving/Feeding", 
"Moving/Feeding", "Standing", "Standing", "Moving/Feeding", "Moving/Feeding", 
"Moving/Feeding", "Moving/Feeding", "Moving/Feeding", "Moving/Feeding", 
"Moving/Feeding", "Standing", "Standing", "Feeding/Moving", "Feeding/Moving", 
"Feeding/Moving", "Feeding/Moving", "Feeding/Moving", "Feeding/Moving", 
"Feeding/Moving", "Feeding/Moving", "Feeding/Moving", "Feeding/Moving", 
"Feeding/Moving", "Feeding/Moving", "Feeding/Moving", "Feeding/Moving", 
"Standing", "Moving/Feeding", "Standing", "Moving/Feeding", "Moving/Feeding", 
"Moving/Feeding", "Moving/Feeding", "Standing", "Moving/Feeding", 
"Moving/Feeding", "Feeding/Moving", "Moving/Feeding", "Moving/Feeding", 
"Moving/Feeding", "Moving/Feeding", "Feeding/Moving", "Feeding/Moving", 
"Moving/Feeding", "Moving/Feeding", "Moving/Feeding", "Moving/Feeding", 
"Feeding/Moving", "Feeding/Moving", "Moving/Feeding", "Moving/Feeding", 
"Moving/Feeding", "Standing", "Feeding/Moving", "Feeding/Moving", 
"Feeding/Moving", "Feeding/Moving", "Feeding/Moving", "Feeding/Moving", 
"Feeding/Moving", "Feeding/Moving", "Feeding/Moving", "Feeding/Moving", 
"Feeding/Moving", "Moving/Feeding", "Standing", "Standing", "Moving/Feeding", 
"Moving/Feeding", "Moving/Feeding", "Moving/Feeding", "Moving/Feeding", 
"Moving/Feeding", "Moving/Feeding", "Moving/Feeding", "Moving/Feeding", 
"Moving/Feeding", "Moving/Feeding", "Moving/Feeding", "Moving/Feeding", 
"Standing", "Moving/Feeding", "Feeding/Moving", "Feeding/Moving", 
"Feeding/Moving", "Feeding/Moving", "Feeding/Moving", "Feeding/Moving", 
"Feeding/Moving", "Feeding/Moving", "Feeding/Moving"), predicted = structure(c(2L, 
2L, 2L, 2L, 2L, 2L, 3L, 3L, 2L, 3L, 2L, 1L, 2L, 2L, 3L, 3L, 3L, 
1L, 2L, 2L, 3L, 3L, 3L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 
3L, 2L, 2L, 2L, 2L, 3L, 2L, 3L, 3L, 3L, 2L, 3L, 2L, 3L, 3L, 3L, 
3L, 2L, 2L, 2L, 1L, 1L, 3L, 3L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 2L, 
2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 2L, 3L, 3L, 
2L, 2L, 3L, 2L, 3L, 3L, 2L, 3L, 1L, 3L, 1L, 1L, 3L, 1L, 2L, 2L, 
3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
3L, 2L, 3L, 1L, 3L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 2L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 2L, 3L, 2L, 1L, 
2L, 3L, 3L, 3L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 1L, 3L, 3L, 3L, 2L, 
1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 1L, 1L, 1L, 3L, 2L, 2L, 3L, 3L, 
2L, 3L, 3L, 3L, 3L, 2L, 3L, 2L, 2L, 3L, 2L, 2L, 3L, 1L, 3L, 1L, 
1L, 1L, 1L, 1L, 3L, 1L), .Label = c("Feeding", "Foraging", "Standing"
), class = "factor"), new_predicted = c("Standing", "Standing", 
"Standing", "Standing", "Standing", "Standing", "Moving/Feeding", 
"Moving/Feeding", "Standing", "Moving/Feeding", "Standing", "Standing", 
"Standing", "Standing", "Moving/Feeding", "Moving/Feeding", "Moving/Feeding", 
"Standing", "Standing", "Standing", "Moving/Feeding", "Moving/Feeding", 
"Moving/Feeding", "Standing", "Standing", "Standing", "Moving/Feeding", 
"Moving/Feeding", "Moving/Feeding", "Moving/Feeding", "Moving/Feeding", 
"Standing", "Standing", "Moving/Feeding", "Standing", "Standing", 
"Standing", "Standing", "Moving/Feeding", "Standing", "Moving/Feeding", 
"Moving/Feeding", "Moving/Feeding", "Standing", "Moving/Feeding", 
"Standing", "Moving/Feeding", "Moving/Feeding", "Moving/Feeding", 
"Moving/Feeding", "Standing", "Standing", "Standing", "Standing", 
"Standing", "Moving/Feeding", "Moving/Feeding", "Standing", "Standing", 
"Standing", "Moving/Feeding", "Moving/Feeding", "Moving/Feeding", 
"Moving/Feeding", "Standing", "Standing", "Standing", "Standing", 
"Standing", "Standing", "Standing", "Standing", "Standing", "Standing", 
"Moving/Feeding", "Standing", "Standing", "Standing", "Standing", 
"Moving/Feeding", "Moving/Feeding", "Standing", "Standing", "Moving/Feeding", 
"Standing", "Moving/Feeding", "Moving/Feeding", "Standing", "Moving/Feeding", 
"Standing", "Moving/Feeding", "Standing", "Standing", "Moving/Feeding", 
"Standing", "Standing", "Standing", "Moving/Feeding", "Moving/Feeding", 
"Standing", "Standing", "Standing", "Standing", "Standing", "Standing", 
"Standing", "Standing", "Standing", "Standing", "Standing", "Standing", 
"Standing", "Standing", "Moving/Feeding", "Standing", "Moving/Feeding", 
"Standing", "Moving/Feeding", "Standing", "Moving/Feeding", "Standing", 
"Standing", "Standing", "Standing", "Standing", "Standing", "Standing", 
"Moving/Feeding", "Moving/Feeding", "Moving/Feeding", "Moving/Feeding", 
"Moving/Feeding", "Moving/Feeding", "Moving/Feeding", "Moving/Feeding", 
"Standing", "Standing", "Moving/Feeding", "Standing", "Standing", 
"Standing", "Standing", "Standing", "Moving/Feeding", "Standing", 
"Standing", "Standing", "Standing", "Standing", "Standing", "Standing", 
"Standing", "Standing", "Standing", "Moving/Feeding", "Moving/Feeding", 
"Moving/Feeding", "Standing", "Moving/Feeding", "Standing", "Standing", 
"Standing", "Moving/Feeding", "Moving/Feeding", "Moving/Feeding", 
"Standing", "Standing", "Moving/Feeding", "Moving/Feeding", "Moving/Feeding", 
"Moving/Feeding", "Moving/Feeding", "Standing", "Moving/Feeding", 
"Moving/Feeding", "Moving/Feeding", "Standing", "Standing", "Standing", 
"Standing", "Standing", "Standing", "Standing", "Moving/Feeding", 
"Moving/Feeding", "Standing", "Standing", "Standing", "Moving/Feeding", 
"Standing", "Standing", "Moving/Feeding", "Moving/Feeding", "Standing", 
"Moving/Feeding", "Moving/Feeding", "Moving/Feeding", "Moving/Feeding", 
"Standing", "Moving/Feeding", "Standing", "Standing", "Moving/Feeding", 
"Standing", "Standing", "Moving/Feeding", "Standing", "Moving/Feeding", 
"Standing", "Standing", "Standing", "Standing", "Standing", "Moving/Feeding", 
"Standing")), class = "data.frame", row.names = c(NA, -215L))

感谢任何输入!

1 个答案:

答案 0 :(得分:3)

如何在不转换为因子的情况下进行以下操作

mean(test_accuracy$observed == test_accuracy$new_predicted)
# 0.5069767