• Register
0 votes
3.6k views

Problem :

I have been facing below error message and traceback:

Error in FUN(X[[i]], ...) : 
only defined on the data frame with all numeric variables 
5 stop("only defined on the data frame with all numeric variables") 
4 FUN(X[[i]], ...) 
3 lapply(args, function(x) {
    x <- as.matrix(x)
    if (!is.numeric(x) && !is.complex(x)) 
top("only defined on the data frame with all numeric variables") ... 

My only guess is that it is the result of my function code, which is as below:

corr <- function(directory, threshold = 0) {
vect1 <- numeric()
files_list <- list.files(directory, full.names = TRUE)
for (j in 1:332) {
    data <- read.csv(files_list[j])
    good <- complete.cases(data)
    complete_data <- data[good,]
   mysulfate <- complete_data[,2]
    mynitrate <- complete_data[,3]
        if (sum(complete_data) >= threshold) {
            b <- cor(sulfate,nitrate)
            vect1 <- rbind(b)
        }
        else vect1 <- (numeric())
    }
    return(vect1)
}

From the above error message and the traceback I guess the error is occurring when my correlation is running on the mysulfate and mynitrate columns. When I tried to run my code on just the first file in my directory, it runs fine with no error messages at all. Any suggestion as to why above error is occurring, and also how to fix it would be helpful to me.

I have tried to coerce my dataset into being numeric as below -

complete_data <- as.numeric(data[good,])

But now I get the different error message back as below :

 "Error: (list) object cannot be coerced to type 'double'"

 

7.5k points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

You must be counting the number of rows in your good data only and do not try to sum an entire data frame asa below :

if (nrow(complete_data) >= threshold) {
    b <- cor(mysulfate,mynitrate)
    vect1 <- rbind(b)
}
38.6k points

Related questions

0 votes
1 answer 32 views
32 views
Problem: How do I solve this issue: Error in fun(x[[i]], ...) : only defined on a data frame with all numeric variables?
asked Jun 14 akhi 125k points
0 votes
1 answer 8 views
8 views
Problem: Need to facilitate with this : Only defined on a data frame with all numeric variables
asked Jun 25 Sawyer Parviz 168k points
0 votes
1 answer 53 views
53 views
Problem: This has been a pain for me for quite some time. Is there a solution? : Error in fun(x[[i]], ...) : subscript out of bounds .
asked May 20 Isac Christiaan 103k points
1 vote
2 answers 1.3k views
1.3k views
Problem : I am very new to R. I am facing a issue while entering CurrentDay. I had previously created it as below : Transaction <- function(PnL, Day)   results <- list(a = PnL, b = Day)  return(results) The above PnL and Day are both numeric values. Day ... /MA  return(MAValue) } Please guide me why am I receiving below error? Error in fun(left, right) : non-numeric argument to binary operator.
asked Apr 14, 2020 morrisBson 3.2k points
0 votes
1 answer 107 views
107 views
Problem: I tried to create a stacked bar plot in R, but unfortunately I have several problems. R gives me the following error: Error: data must be a data frame, or other object coercible by fortify(), not an S3 object with class Date I searched for a while, but I can't find ... .24896266 137 30.08.2018 4 0.36960986 138 31.08.2018 4 1.09979633 139 01.09.2018 4 0.48387097 140 02.09.2018 4 0.00000000
asked Apr 24 sumaiya simi 43.9k points
0 votes
1 answer 19 views
19 views
Problem: I have a data.frame whose class column is Factor. I'd like to convert it to numeric so that I can use a correlation matrix. How can I convert a Factor column to a numeric column?
asked Mar 30 ummesalma 29.1k points
0 votes
1 answer 106 views
106 views
Problem: How to fix this error > Error in `[.data.frame`(x, r, vars, drop = drop) : undefined columns selected
asked Apr 30 ummeshani 10.8k points
0 votes
1 answer 56 views
56 views
Problem: Any solution for this error: Error in aggregate.data.frame(as.data.frame(x), ...) : arguments must have same length
asked Mar 14 Wafa Abu Yousef 6.1k points
0 votes
2 answers 2.4k views
2.4k views
Problem : I am facing below error and I am unable to understand the meaning of the error. All my variable names are correct and there are no typing errors. Does anybody faced this issue before any solution on it? My erroneous code : datNewagg <- aggregate (dataNew, by = ... mean) It gives following error : Error in aggregate.data.frame(datNew, by = list("x", "y", : arguments must have same length
asked Dec 6, 2019 alecxe 7.5k points
0 votes
1 answer 1.3k views