• Register
0 votes
454 views

Problem :

I am trying to coerce a list object to type 'double'

Please find below my code:

a <- structure(list(`X$Days` = c("10", "38", "66", "101", "129", "185", "283", 
                                 "374")), .Names = "X$Days")

Here a is like

$`X$Days`
[1] "10"  "38"  "66"  "101" "129" "185" "283" "374"

Here I am trying to coerce a to an array of numeric values, however the coercing functions are returning me following error:

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

Please let me know how can I resolve the issue.

6 5 3
7,540 points

3 Answers

0 votes

Solution :

If you are trying to convert all elements of a to the single numeric vector and the length(a) is greater than 1 it is OK even if it is of length 1, you can unlist the object first after that convert.

as.numeric(unlist(a))

# [1]  10  38  66 101 129 185 283 374

Remember that there are many quality controls here. Also, X$Days a mighty unique name.

 

OR

You can see the problems with some data as follows :

as.double(as.character("2.e")) # This results in 2

Another approach as follows:

get_numbers <- function(X) {
    X[toupper(X) != tolower(X)] <- NA
    return(as.double(as.character(X)))
}

 

OR

In your case the loop will also do the job as follows :

a <- array(0, dim=dim(X))
for (i in 1:ncol(X)) {a[,i] <- X[,i]}
9 7 4
38,600 points
0 votes

When the user tries to treat a list of numeric integers as numbers, then following error occurs;

“(list) object cannot be coerced to type double”.

Reason:

When confronted with a list that contains the strings that do not number, then the system throws an error for clarity in this regard. Now the question is Why the fixation on “double” as the target data type. This is because of the default data type that numbers mostly like. From a practical perspective, Some practical data type works for most common data sets.

Solution:

  • Use the unlist() function to convert your list into a single vector and feed the result as.numeric. This will create a list of numeric values for your inspection.
  • You can use the lappy function to apply as .numeric function to each element of the list.
  • You can use list sub_srtting to target specific elements of the list and feed these functions as.numeric function.

Example:

If you want to convert all the elements of a to a single numeric vector and the length of the vector is greater than 1, then unlist the object first and then convert.

If you apply list as multiple elements that need to be converted to numeric, then you can achieve this with;

lapply(a, as.numeric);

 

11 5 1
3,890 points
0 votes

Solution:

The input should be a matrix, not a list (or its special case data. frame)
Var <- read.table(text="
   D.Prime    T.statistics
1    1.7234e-01     4.926800
2    1.4399e-01     2.892000
3    1.4626e-01     2.642800
4    3.5147e-02     1.112400
5    5.8957e-02     2.723700
", header=TRUE)
# library(Hmisc)
rc <- rcorr(as.matrix(Var), type="pearson")
# from recommended package stats
ct <- cor.test(Var$D.Prime, Var$T.statistics, method = "pearson")
rc$P
                D.Prime T.statistics
D.Prime             NA    0.1101842
T.statistics 0.1101842           NA
ct$p.value
[1] 0.1101842

 

Note: You should clarify whic library you are using at this point. In this case Hmisc is a very popular one.

10 6 4
31,120 points

Related questions

0 votes
2 answers 524 views
524 views
Problem : I am trying to do some practice on the broom package in R. I ran the below code : kclusts <- data.frame(k=1:9) %>% group_by(k) %>% do(fit=kmeans(data.frame, .$k)) But I have received the below error: Error in as.vector(x, mode) : ... type 'any' I wanted to know whether I am missing something in my above string. Does anyone have the solution on this issue which I am facing currently?
asked Dec 21, 2019 alecxe 7.5k points
0 votes
2 answers 120 views
120 views
Problem : I am stuck because of following weired error error in sort.list(y) : 'x' must be atomic for 'sort.list' have you called 'sort' on a list?
asked Nov 15, 2019 peterlaw 6.9k points
0 votes
2 answers 1.7K views
1.7K views
Problem : Below is my code which is giving me the error. plot(x,y) yx.lm <- lm(y ~ x) lines(x, predict(yx.lm), col="red") Why am I facing following error? Error in plot.xy(xy.coords(x, y), type = type, ...) : plot.new has not been called yet
asked Dec 4, 2019 alecxe 7.5k points
0 votes
1 answer 176 views
176 views
Problem: I have only the fundamental knowledge related to R. I want to simulate the very basic moving average process in R which is as shown below for your reference: x_t = Z_(t-1)-0.4Z_(t-2)+Z_t. Please find below my code in R: z = rnorm(500, 0, 1) x ... was unable to find the exact solution for my above error. I am looking for some help from R expert if possible to fix my above R related error.
asked Aug 12 Raphael Pacheco 4.9k points