R Error In Apply Dimx Must Have A Positive Length
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and
R Apply Vector
policies of this site About Us Learn more about Stack Overflow the lapply sapply company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags
R Apply Function To Each Row
Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only error in match.fun(fun) : argument "fun" is missing, with no default takes a minute: Sign up r Error dim(X) must have a positive length? up vote 13 down vote favorite 1 I want to compute the mean of "Population" of built-in matrix state.x77. The codes are : apply(state.x77[,"Population"],2,FUN=mean) #Error in apply(state.x77[, "Population"], 2, FUN = mean) : # dim(X) must have a positive length how can I prevent this error? If I fun in r use $ sign apply(state.x77$Population,2,mean) # Error in state.x77$Population : $ operator is invalid for atomic vectors What is atomic vector? r share|improve this question edited Mar 2 at 11:26 m0nhawk 8,12972154 asked May 22 '13 at 2:35 time 2791512 3 But regardless, if you're just doing this for one column, why not just mean(state.x77[,"Population"])? –joran May 22 '13 at 2:37 @joran Thank you very much . both the reference FAQs and mean(state.x77[,"Population"]) are beneficial. –time May 22 '13 at 2:50 add a comment| 1 Answer 1 active oldest votes up vote 19 down vote accepted 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 y
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn
Tapply
more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up dim(X) must have a positive length in R up vote 0 down vote favorite I want to see how many positive and negative I have http://stackoverflow.com/questions/16682576/r-error-dimx-must-have-a-positive-length in a matrix I simply use the following command to get the negative values but I get error apply(m[,2], 2, function(x) length(x[x<0])) My matrix look like following and named m row.names logFC AveExpr t P.Value adj.P.Val B 1 AFFX 0.254330303 -0.0273913479 2.790941 0.005346628 0.9936675 -2.351052 2 AFFX- 0.144249680 -0.0081323315 2.744752 0.006154509 0.9936675 -2.470447 3 AFFX-T 0.129062121 -0.0072936248 2.730567 0.006423832 0.9936675 -2.506720 4 AFFy 0.105893838 -0.0084886157 2.672831 0.007632963 0.9936675 -2.652445 5 AFfm 0.146253131 -0.0123657559 2.617852 0.008970330 0.9936675 http://stackoverflow.com/questions/28581297/dimx-must-have-a-positive-length-in-r -2.788357 6 20uy 1.033582071 0.9319210383 2.476302 0.013425076 0.9936675 -3.125444 7 2196 0.099556431 -0.0061285974 2.451229 0.014392052 0.9936675 -3.183222 **Error in apply(m[, 2], 2, function(x) length(x[x < 0])) : dim(X) must have a positive length** r share|improve this question asked Feb 18 '15 at 10:29 user1267127 2 you are trying apply on a vector ie m[,2] is a vector. If you want to find this for each column in the whole dataset apply(df1[-1], 2, function(x) length(x[x<0])) assuming that the first column is rownames. –akrun Feb 18 '15 at 10:33 1 If you want to see a frequency of positive vs negatives, simply do table(m[-1] < 0) Or for a nicer output, could do setNames(table(m[-1] > 0), c("Negatives", "Positives")) –David Arenburg Feb 18 '15 at 11:04 thanks @akrun ! I liked your solution –user1267127 Feb 18 '15 at 12:19 Thanks @David Arenburg However, your solution give the positive values of all columns or negative values or all column while i am interested in the positive or negative values of a certain column –user1267127 Feb 18 '15 at 12:20 So just do table(m[, 2] < 0)? (if you want second column for example) –David Arenburg Feb 18 '15 at 12:52 | show 4 more comments 1 Answer 1 active oldest votes up vote 3 down vote accepted You can just type: sum(m<0
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers http://stackoverflow.com/questions/21389980/error-in-apply-dimx-must-have-a-positive-length or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Error in apply() : dim(X) must have a positive length up vote 0 down vote favorite The below code gives an error. What causes the error and what can I do to solve it? # error in Determine number of clusters wss <- (nrow(donnees.test$y - esvr1.pred) - 1) * sum(apply(donnees.test$y - esvr1.pred, 2, var)) for (i in 2:15) wss[i] <- sum(kmeans(donnees.test$y - esvr1.pred, centers=i)$withinss) plot(1:15, wss, type="b", xlab="Number of Clusters", ylab="Within groups sum of squares") # K-Means Cluster Analysis fit <- kmeans(donnees.test$y - esvr1.pred, 5) # 5 cluster solution # get cluster means aggregate(donnees.test$y - esvr1.pred, by=list(fit$cluster), FUN=mean) # append cluster assignment MainData <- data.frame(donnees.test$y - esvr1.pred, fit$cluster) The error : Error in apply(donnees.test$y - esvr1.pred, 2, r error in var) : dim(X) must have a positive length r k-means share|improve this question edited Jan 28 '14 at 10:40 tonytonov 12.6k123763 asked Jan 27 '14 at 19:35 user3235052 2127 donnees.test$y-esvr1.pred isn't a matrix, so it has no dimension attribute, just at the error message says. Why are you trying to calculate the variance of each individual element of a vector? –joran Jan 27 '14 at 19:43 So how can i calculate k-mean ? i searched for code and i saw this code just changed to mydata! –user3235052 Jan 27 '14 at 19:50 I fixed some of your formatting, but we don't know what donness.test or esvr1.pred are so the problem is not reproducible. –Brian Diggs Jan 27 '14 at 22:46 kmeans requires a matrix (or something that can be coerced to a matrix) as an input. You are not giving it a matrix. Because your code is not reproducible, I am voting to close. –Blue Magister Jan 27 '14 at 22:48 add a comment| active oldest votes Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook. Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post as a guest Name Email Post as a guest Name Email discard By posting your answer, you agree to the
be down. Please try the request again. Your cache administrator is webmaster. Generated Tue, 25 Oct 2016 09:57:44 GMT by s_nt6 (squid/3.5.20)