【问题标题】:"Non numeric argument to binary operator" Error In R“二元运算符的非数字参数”R中的错误
【发布时间】:2021-02-03 12:44:55
【问题描述】:

您好,我是 R 新手,可以帮助我。

错误消息“head(T[rowss, (i + 1)]) 中的错误 - head(T[rowss, i]) : 二元运算符的非数字参数”

代码如下

library("xlsx")  #import library
T <- read_excel("D:/Education/UOR Lecture/Project/4/NEW.xlsx")  #read xlsx file
View(T)
fun <- function(T,rowss) {
rows <- nrow(T)
columns <- ncol(T)
t <- 3
H <- 15.8
last <- 0
for (i in 3:columns-1){
k <- (head(T[rowss,(i+1)])-head(T[rowss,i]))/(head(T[rowss,(i+1)])*t*(H-head(T[rowss,(i+1)])))
last <- k+last
}
fun <- last/columns
return(fun)
}

T 是从 excel 表中读取的表格 我还使用类函数来确保所有都是数字并且所有都是数字期望 T 并使用类函数显示 T 显示 class(T) [1] "tbl_df" "tbl" "data.frame"

请帮忙

【问题讨论】:

  • 为了使这个可重现,您可以尝试dput(head(name_of_data_frame)) 并使用输出编辑您的问题吗? name_of_data_frame 应该是您为read_excel 存储结果的任何内容...我建议避免使用T(用于TRUE)并将其称为其他名称...您打算如何调用您的函数@ 987654328@?

标签: r


【解决方案1】:

此错误occurs when you addition, division, or a similar mathematical operator with a non-numeric variables。在您的情况下,似乎 head(T[rowss, (i + 1)])head(T[rowss, i]) 不是数字。使用例如查看数据框T str(T) 查看你们中哪些变量不是数字的(例如那些是 character 变量的变量)。

【讨论】:

  • 我尝试使用类函数,但都是数字,我使用 T 作为 excel 表到达 R
【解决方案2】:

感谢所有帮助过我的人。 我发现错误 for (i in 3:columns-1) 重写 for (i in 4:columns-1) 及其工作

【讨论】:

    猜你喜欢
    • 2016-11-15
    • 2019-05-03
    • 2016-11-14
    • 2021-08-18
    • 2021-03-24
    • 1970-01-01
    • 2021-03-24
    • 1970-01-01
    相关资源
    最近更新 更多