Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.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级添加到数据帧,为什么?_R_Rbind_Levels - Fatal编程技术网

r级添加到数据帧,为什么?

r级添加到数据帧,为什么?,r,rbind,levels,R,Rbind,Levels,这篇文章是为了更好地理解R中的“级别”是如何工作的。事实上,其他答案并不能完全解释(参见示例) 考虑下面的简短脚本,其中我计算随机数据帧df的每一列的RMSE,并将该值存储为新数据帧bestcombo df = as.data.frame(matrix(rbinom(10*1000, 1, .5), nrow = 10, ncol=5)) #generate empty dataframe and assign col names bestcombo = data.frame(matrix(n

这篇文章是为了更好地理解R中的“级别”是如何工作的。事实上,其他答案并不能完全解释(参见示例)

考虑下面的简短脚本,其中我计算随机数据帧
df
的每一列的RMSE,并将该值存储为新数据帧
bestcombo

df = as.data.frame(matrix(rbinom(10*1000, 1, .5), nrow = 10, ncol=5))

#generate empty dataframe and assign col names
bestcombo = data.frame(matrix(ncol = 2, nrow = 0))
colnames(bestcombo) = c("RMSE", "Row Number")

#for each col of df calculate RMSE and store together with col name
for (i in 1:5){
  RMSE = sqrt(mean(df[,i] ^ 2))
  row_num = i

  row = as.data.frame(cbind( RMSE, toString(row_num) ))
  colnames(row) = c("RMSE", "Row Number")
  bestcombo = rbind(bestcombo, row)
}
问题是“级别”是生成的。为什么?

bestcombo$RMSE
             RMSE              RMSE              RMSE              RMSE              RMSE 
0.547722557505166 0.774596669241483 0.707106781186548 0.836660026534076 0.707106781186548 
Levels: 0.547722557505166 0.774596669241483 0.707106781186548 0.836660026534076

bestcombo$RMSE[1]
             RMSE 
0.547722557505166 
Levels: 0.547722557505166 0.774596669241483 0.707106781186548 0.836660026534076
为什么会发生这种情况以及如何避免?这是因为错误地使用了rbind()

这也产生了其他问题。例如,order函数不起作用

bestcombo[order(bestcombo$RMSE),]

               RMSE Random Vector
1 0.547722557505166             1
2 0.774596669241483             2
3 0.707106781186548             3
5 0.707106781186548             5
4 0.836660026534076             4

你想要更像这样的东西:

#for each col of df calculate RMSE and store together with col name
for (i in 1:5){
    RMSE = sqrt(mean(df[,i] ^ 2))
    row_num = i

    row = data.frame(RMSE = RMSE, `Row Number` = as.character(row_num) )
    #colnames(row) = c("RMSE", "Row Number")
    bestcombo = rbind(bestcombo, row)
}
或者,如果确实要在第二行中添加列名,请执行以下操作:

for (i in 1:5){
    RMSE = sqrt(mean(df[,i] ^ 2))
    row_num = i

    row = data.frame(RMSE,as.character(row_num) )
    colnames(row) = c("RMSE", "Row Number")
    bestcombo = rbind(bestcombo, row)
}

为了完整起见,我要补充一点,虽然这不是您问题的重点,但这样一次按
rbind
ind行增长数据帧,一旦数据帧变得相当大,就会开始产生显著的速度损失。

您想要更像这样的东西:

#for each col of df calculate RMSE and store together with col name
for (i in 1:5){
    RMSE = sqrt(mean(df[,i] ^ 2))
    row_num = i

    row = data.frame(RMSE = RMSE, `Row Number` = as.character(row_num) )
    #colnames(row) = c("RMSE", "Row Number")
    bestcombo = rbind(bestcombo, row)
}
或者,如果确实要在第二行中添加列名,请执行以下操作:

for (i in 1:5){
    RMSE = sqrt(mean(df[,i] ^ 2))
    row_num = i

    row = data.frame(RMSE,as.character(row_num) )
    colnames(row) = c("RMSE", "Row Number")
    bestcombo = rbind(bestcombo, row)
}

为了完整性起见,我要补充一点,虽然这不是您问题的重点,但这样一次按
rbind
ind行增长数据帧,一旦数据帧变得相当大,就会开始产生显著的速度损失。

因此:
as.data.frame(cbind(RMSE,toString(row_num)))
这是创建数据帧的一种常见习惯用法,但并不可取。只需使用
data.frame()
cbind
是将内容强制为单个类型,即字符。此外,您确实希望将
用作.character
,而不是
toString
。如果你阅读文档的话,这个名字有点误导人。谢谢。但是,即使从as.data.frame中删除“as”,这些“级别”仍然存在。而且排序仍然不起作用。有关更多上下文,请参见下面的答案。因为:
as.data.frame(cbind(RMSE,toString(row_num))
,这是创建数据帧的一种常见习惯用法,非常不可取。只需使用
data.frame()
cbind
是将内容强制为单个类型,即字符。此外,您确实希望将
用作.character
,而不是
toString
。如果你阅读文档的话,这个名字有点误导人。谢谢。但是,即使从as.data.frame中删除“as”,这些“级别”仍然存在。而且排序仍然不起作用。有关更多上下文,请参阅下面的答案。