R Error Dim(X) Must Have a Positive Length

dim(X) must have a positive length when applying function in data frame

It happens because R coerces last_visit[,2] to a dimensionless vector, whereas apply expects the object to have some dimensions. You can prevent the coercion by adding drop=F to your command, i.e.:

apply(last_visit[,2,drop=F], 1, best_recom)

Another way would be just to use lapply or sapply on the vector:

lapply(last_visit[,2], best_recom)

r Error dim(X) must have a positive length?

To expand on joran's comments, consider:

> is.vector(state.x77[,"Population"])
[1] TRUE
> is.matrix(state.x77[,"Population"])
[1] FALSE

So, your Population data is now no diferent from any other vector, like 1:10, which has neither columns or rows to apply against. It is just a series of numbers with no more advanced structure or dimension. E.g.

> apply(1:10,2,mean)
Error in apply(1:10, 2, mean) : dim(X) must have a positive length

Which means you can just use the mean function directly against the matrix subset which you have selected: E.g.:

> mean(1:10)
[1] 5.5
> mean(state.x77[,"Population"])
[1] 4246.42

To explain 'atomic' vector more, see the R FAQ again (and this gets a bit complex, so hold on to your hat)...

R has six basic (‘atomic’) vector types: logical, integer, real,
complex, string (or character) and raw.
http://cran.r-project.org/doc/manuals/r-release/R-lang.html#Vector-objects

So atomic in this instance is referring to vectors as the basic building blocks of R objects (like atoms make up everything in the real world).

If you read R's inline help by entering ?"$" as a command, you will find it says:

‘$’ is only valid for recursive objects, and is only
discussed in the section below on recursive objects.

Since vectors (like 1:10) are basic building blocks ("atomic"), with no recursive sub-elements, trying to use $ to access parts of them will not work.

Since your matrix (statex.77) is essentially just a vector with some dimensions, like:

> str(matrix(1:10,nrow=2))
int [1:2, 1:5] 1 2 3 4 5 6 7 8 9 10

...you also can't use $ to access sub-parts.

> state.x77$Population
Error in state.x77$Population : $ operator is invalid for atomic vectors

But you can access subparts using [ and names like so:

> state.x77[,"Population"]
Alabama Alaska Arizona...
3615 365 2212...

How ti fix error dim(X) must have a positive length R

I'm going to guess you just want match(tmpselected, tmpmydata_genes) and don't need to use apply as match is already vectorized.

Error in apply(a, 3, function(x) { : dim(X) must have a positive length, apply does not work with list

The reason would be that a is a list of arrays, and we need to pass through the list of arrays with a[[i]]. Similarly, create the output list as the same length of 'a' and then assign the output from each iteration to the ith list element

a <- vector('list', 2)
lower_tris <- vector('list', 2)
list_of_pairs <- vector('list', 2)

set.seed(47)
for( i in seq_along(a)){
a[[i]] <- array(rnorm(5 * 5 * 2), c(5, 5, 2))
lower_tris[[i]] <- apply(a[[i]], 3, function(x){x[lower.tri(x)]})
list_of_pairs[[i]] <- split(lower_tris[[i]], seq(nrow(lower_tris[[i]])))
}

If we are using lapply, then we can directly use the same code as in the OP's post

set.seed(47)
out <- lapply(1:2, function(i) {
a <- array(rnorm(5 * 5 * 2), c(5, 5, 2))
lower_tris <- apply(a, 3, function(x) x[lower.tri(x)])
split(lower_tris, seq(nrow(lower_tris))) })

identical(list_of_pairs, out)
#[1] TRUE


Related Topics



Leave a reply



Submit