i have list consists on multiple vectors sort in descending order , obtain sorted index of possitions based on vector values.
a <- c(1) b <- c(9) c <- c(6) d <- c(11) w <- list(a,b,c,d) # if sort(w) error in sort.int(x, na.last = na.last, decreasing = decreasing, ...) : 'x' must atomic # convert matrix as.matrix(w) [,1] [1,] 1 [2,] 9 [3,] 6 [4,] 11 when sort on matrix not work on data frame sort(as.matrix(w)) error in sort.int(x, na.last = na.last, decreasing = decreasing, ...) : 'x' must atomic sort(as.data.frame(w)) x1 x6 x9 x11 1 1 6 9 11 sort(which(as.matrix(w))) error in sort(which(as.matrix(w))) : error in evaluating argument 'x' in selecting method function 'sort': error in which(as.matrix(w)) : argument 'which' not logical which(sort(as.matrix(w))) error in sort.int(x, na.last = na.last, decreasing = decreasing, ...) : 'x' must atomic. would happen know if there way sort in descending order list of vectors , obtain sorted index based on vector values obtain like.
4,2,3,1 thank you
if want have list of vectors (of different lengths or whatever), can use lapply function combined order function, code below:
<- c(runif(5,0,2)) b <- c(runif(7,0,2)) c <- c(runif(9,0,2)) d <- c(runif(11,0,2)) x <- list(a,b,c,d) lapply(x,order, decreasing = t) for "x" list containing following values:
[[1]] [1] 1.0223396 0.4150902 0.4573163 [[2]] [1] 1.19142399 1.14974440 0.15412876 0.07108116 1.28559098 [[3]] [1] 1.857230 1.196185 1.121801 1.052055 1.970190 1.015284 1.365576 [[4]] [1] 1.2030824 0.4777374 0.5163319 1.4586192 ... gives list of ordered indexes each vector in new list of same length, looks this.
[[1]] [1] 2 3 1 [[2]] [1] 4 3 2 1 5 [[3]] [1] 6 4 3 2 7 1 5 [[4]] [1] 2 3 1 4 hope helps.
Comments
Post a Comment