61484

Manipulating sub matrices in R

Question:

Nh<-matrix(c(17,26,30,17,23, 17 ,24, 23), nrow=2, ncol=4); Nh Sh<-matrix(c(8.290133, 6.241174, 6.096808, 7.4449672, 6.894924, 7.692115, 4.540521, 7.409122), nrow=2, ncol=4); Sh NhSh<-as.matrix(Nh*Sh); NhSh rh<-c( 0.70710678, 0.40824829, 0.28867513, 0.22360680, 0.18257419, 0.15430335, 0.13363062, 0.11785113, 0.10540926, 0.09534626); rh pv <- c() for (j in 1:2) { for (i in 1:4) { pv <- rbind(pv, NhSh[j,i]*rh) } } pv row.names(pv) <- rep(c(1:2), each = 4) lst<-lapply(split(seq_len(nrow(pv)), as.numeric(row.names(pv))), function(i) pv[i,]) data<-40 nlargest <- function(x, data) { res <- order(x)[seq_len(data)]; pos <- arrayInd(res, dim(x), useNames = TRUE); list(values = pv[res], position = pos) } out <- lapply(lst, nlargest, data = 40)

In continuation of above code Is there any brief way of repeating the following steps for each out$’k’$position for k in 1:2?

s1<-c(1,1,1,1); ch<-c(5,7,10,5); C<-150; a<-out$'1'$position for (j in a[40:1, "row"] ) { s1[j] <- s1[j]+1; cost1 <- sum(ch*s1); if (cost1>=C) break } s1; cost1 #Output [1] 5 6 6 5 # [1] 152

I have to get 2 values for 's' and 'cost' for out$k$position. I tried

mat = replicate (2,{x = matrix(data = rep(NA, 80), ncol = 2)}); mat for (k in 1:2) { mat[,,k]<-out$'k'$position } mat

<strong>Error</strong> in mat[, , k] <- out$k$position :number of items to replace is not a multiple of replacement length

for (k in 1:2) { for (j in mat[,,k][40:1] ) { s[j] <- s[j]+1 cost <- sum(ch*s) if (cost>=C) break } } s; cost

<strong>Error</strong> : Error in s[j] <- s[j] + 1 : NAs are not allowed in subscripted assignments

Please anyone help in resolving these errors.

Answer1:

We could apply the function directly by looping over the list. Note that each element of the list is a matrix

sapply(lst, is.matrix) # 1 2 #TRUE TRUE

so, there is no need to unlist and create a matrix

out <- lapply(lst, nlargest, data = 40)

-checking with the OP's results

out1 <- nlargest(sub1, 40) identical(out[[1]], out1) #[1] TRUE <h3>Update2</h3>

Based on the second update, we need to initialize 'cost' and 'sl' with the same length as 'k' elements. Here, we initialize 'sl' as a list of vectors

sl <- rep(list(c(1, 1, 1, 1)), 2) C <- 150 cost <- numeric(2) for (k in 1:2){ for (j in mat[,,k][40:1, 1] ) { sl[[k]][j] <- sl[[k]][j]+1 cost[k] <- sum(ch*sl[[k]]) if (cost[k] >=C) break } } sl #[[1]] #[1] 5 7 6 4 #[[2]] #[1] 6 5 5 7 cost #[1] 154 150

Recommend

  • Using new with fixed length array typedef
  • Increasing size of persistent disks on kubernetes
  • Sql query for Transpose of a dynamic Table
  • How get unique lines from a very large file in linux?
  • gcc module size limits
  • Kubernetes NFS PersistentVolumeClaim has status Pending
  • Need help converting PV formula to PHP
  • Manipulating sub matrices in R
  • calculate Future value in iPhone
  • using nfs network path as a kubernetes persistence volume
  • Match list to rows of matrix in R
  • Keras using Lambda layers error with K.ctc_decode
  • How to set an numpy array image on a QWidget using PyQt5
  • What is expected when I am told “Sort a singly linked list”
  • multiple backgroundworker queueing
  • Adding Boost recipe to Yocto SDK
  • head and tail doesn't take negative number as argument for data.table?
  • Improving performance of appending rows to a data.table
  • Creating table with different row dimensions
  • stack every n columns of a matrix without apply in R
  • Plot a table with R
  • Why are `colMeans()` and `rowMeans()` functions faster than using the mean function with `lapply()`?
  • Returning semi-unique values from a list
  • Merging a large number of files from one directory into a data frame in R
  • R Impute NA's by Linear Increase Depending on Time Interval
  • How to customize whisker lines on a geom_box plot differently than the lines of the box itself
  • addressing in assembler
  • Is mp4 stream able with ffserver?
  • rapply over a nested list in R
  • Python: Split a String Field into 3 Separate Fields using Lambda
  • Neo4j: Filter nodes based on aggregate function
  • how can I compare dates in array to find the earliest one?
  • Divide a $1 by 3 and adjusting 1 cent
  • What is corresponding c++ data type to SQL numeric(18,0) data type?
  • JSON encode and decode on PHP
  • How do I shift the decimal place in Python?
  • Installed module is empty
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • Unable to use reactive element in my shiny app