How to interpret this error message: (list) object cannot be coerced to type 'double'

11,917

There is a hidden argument in square bracket indexing data.frames called drop, which defaults to TRUE, and says if you index one column, try to simplify the result to a vector. See ?'['.

However Hadley Wickham believes this this is unpredictable behaviour, so tbls enforce drop = FALSE.

If you wanted to keep using tbls and avoid converting to a data.frame. You could use dplyr::pull to extract a single column as a vector. i.e:

is.vector(data.frame(a = 1:10, b = letters[1:10])[, 1])
#> [1] TRUE
is.vector(data.frame(a = 1:10, b = letters[1:10])[, 1, drop = FALSE])
#> [1] FALSE
is.vector(dplyr::tibble(a = 1:10, b = letters[1:10])[, 1])
#> [1] FALSE
is.vector(dplyr::pull(dplyr::tibble(a = 1:10, b = letters[1:10]), 1))
#> [1] TRUE
Share:
11,917

Related videos on Youtube

Eva
Author by

Eva

Updated on June 04, 2022

Comments

  • Eva
    Eva almost 2 years

    I have a table: numTable that looks like this: enter image description here

    Now I want to find outiers for each of these columns. Please see my code below:

    for (i in names(numTable)) {
      #calculate mean and std for each column
      meanValue <- mean(numTable[,i], na.rm=TRUE)
      stdValue<-sd(numTable[,i],na.rm=TRUE)
      #Sum up number of outliers for each column
      print(paste("there are",sum(abs(numTable[,i]-meanValue)>3*stdValue,na.rm =  
    TRUE),"outliers in the column",i))
     } 
    

    But I get error message:

    Error in is.data.frame(x) : (list) object cannot be coerced to type 'double'
    

    I fixed this problem by adding numTable<-as.data.frame(numTable) at the beginning. Could you please tell me why I have to add this line for my code to work? Does it have something to do with difference between tbl and data.frame? Thanks.

    • acylam
      acylam over 6 years
      What is class(classTable) before you converted it to data.frame?
    • Eva
      Eva over 6 years
      I get this result [1] "tbl_df" "tbl" "data.frame" after I run class(numT). I am not sure if it is a tbl_df or tbl or data.frame.
    • acylam
      acylam over 6 years
      It is all of them. In this case, I think Hayden has the answer.