![](/img/trans.png)
[英]how to solve error : Error in storage.mode(x) <- "double" : 'list' object cannot be coerced to type 'double'
[英]Error in diag(csvfiles[, x:ncol(csvfiles)]) : (list) object cannot be coerced to type 'double'
例如,我編寫了以下代碼來查找由 5 個或更多 1 連接的 1 的數量,
1 1 1 0 0 1
1 1 1 1 1 1
0 0 0 or another example 1 1 1
上面例子中間的 1 有 5 個或更多的 1 與之相連。
但我得到了錯誤......
diag(csvfiles[, x:ncol(csvfiles)]) 中的錯誤:(list) object 不能被強制輸入“double”
誰能幫我確定下面的 function 有什么問題?
csvs <- list.files(pattern="*.csv")
csvfiles <- lapply(csvs, read.table)
connected_5_or_more <- function (csvfiles) {
consecs <- c(lapply(1:ncol(csvfiles), function(x) csvfiles[, x]),
lapply(1:nrow(csvfiles), function(x) csvfiles[x, ]),
lapply(1:(ncol(csvfiles)-1), function(x) diag(csvfiles[, x:ncol(csvfiles)])),
lapply(1:(nrow(csvfiles)-1), function(x)
diag(csvfiles[x:nrow(csvfiles), ]))[-1]
)
sum(sapply(connected_5_or_more, function(x) with(rle(x), sum(lengths[values == 1] >= 5))))
}
neigh5 <- lapply(csvfiles, connected_5_or_more)
我在下面包含了一個可重現的示例:
set.seed(30948)
mat <- matrix(rbinom(10 * 5, 1, 0.5), ncol = 20, nrow = 20)
mat
connected_5_or_more <- function (mat) {
consecs <- c(lapply(1:ncol(mat), function(x) mat[, x]),
lapply(1:nrow(mat), function(x) mat[x, ]),
lapply(1:(ncol(mat)-1), function(x) diag(mat[, x:ncol(mat)])),
lapply(1:(nrow(mat)-1), function(x)
diag(mat[x:nrow(mat), ]))[-1]
)
sum(sapply(connected_5_or_more, function(x) with(rle(x), sum(lengths[values == 1] >= 5))))
}
neigh5 <- lapply(mat, connected_5_or_more)
更新:獲取長度為 0 的錯誤參數。
connected_5_or_more <- function (files) {
consecs <- c(lapply(1:ncol(files), function(x) files[, x]),
lapply(1:nrow(files), function(x) files[x, ]),
lapply(1:(ncol(files)-1), function(x) diag(files[,
x:ncol(files)])),
lapply(1:(nrow(files)-1), function(x)
diag(files[x:nrow(files), ]))[-1]
)
sum(sapply(connected_5_or_more, function(x) with(rle(x),
sum(lengths[values == 1] >= 5))))
}
neigh5 <- lapply(files, connected_5_or_more(files))
在您的代碼中,您使用參數mat
定義 function connected_5_or_more
並且在調用它時,您沒有傳遞任何內容,只是調用connected_5_or_more
,這是錯誤的語法。 您應該將 function connected_5_or_more
稱為connected_5_or_more(mat)
。
但是您的代碼仍然會產生錯誤:
set.seed(30948)
mat <- matrix(rbinom(10 * 5, 1, 0.5), ncol = 20, nrow = 20)
mat
connected_5_or_more <- function (mat) {
consecs <- c(lapply(1:ncol(mat), function(x) mat[, x]),
lapply(1:nrow(mat), function(x) mat[x, ]),
lapply(1:(ncol(mat)-1), function(x) diag(mat[, x:ncol(mat)])),
lapply(1:(nrow(mat)-1), function(x)
diag(mat[x:nrow(mat), ]))[-1]
)
sum(sapply(connected_5_or_more, function(x) with(rle(x), sum(lengths[values == 1] >= 5))))
}
neigh5 <- lapply(mat, connected_5_or_more(mat))
我的系統上的錯誤回溯:
Error in rle(x) : 'x' must be a vector of an atomic type
9.
stop("'x' must be a vector of an atomic type")
8.
rle(x)
7.
with(rle(x), sum(lengths[values == 1] >= 5))
6.
FUN(X[[i]], ...)
5.
lapply(X = X, FUN = FUN, ...)
4.
sapply(connected_5_or_more, function(x) with(rle(x), sum(lengths[values ==
1] >= 5)))
3.
connected_5_or_more(mat)
2.
match.fun(FUN)
1.
lapply(mat, connected_5_or_more(mat))
那么,您是否嘗試在代碼中使用某種遞歸,請解釋一下。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.