0
votes

Hello I'm new for R and can help me.

error massage "Error in head(T[rowss, (i + 1)]) - head(T[rowss, i]) : non-numeric argument to binary operator"

code bellow here

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 is a table read from excel sheet and i also using class function for make sure all are numeric and all are numeric expect T and using class function for T show class(T) [1] "tbl_df" "tbl" "data.frame"

please Help

2
To make this reproducible, could you try dput(head(name_of_data_frame)) and edit your question with the output? name_of_data_frame should be whatever you store the result for read_excel...I would recommend avoid using T (used for TRUE) and call it something else...and how are you intending to call your function fun?Ben

2 Answers

0
votes

This error occurs when you addition, division, or a similar mathematical operator with a non-numeric variables. In your case, it seems that either head(T[rowss, (i + 1)]) or head(T[rowss, i]) is non-numeric. Have a look at the data frame T using e.g. str(T) to see which of you variables that aren't numeric (e.g. those that are character variables).

0
votes

Thanks for all who helped me. i find the error for (i in 3:columns-1) rewrite for (i in 4:columns-1) and its work