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 to an array of numeric values, however, the coercing functions are returning me the following error:
Error: (list) object cannot be coerced to type 'double'
Please let me know how can I resolve the issue.
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]}
When the user tries to treat a list of numeric integers as numbers, then the 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 is 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 the list as multiple elements that need to be converted to numeric, then you can achieve this with;
lapply(a, as.numeric);
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 which library you are using at this point. In this case, Hmisc is a very popular one.
Top comments (0)