8571

Running Map reduces the dimensions of the matrices

Say I have three lists:

l_zero [[1]] [,1] [,2] [1,] 0 0 [2,] 0 0 [[2]] [,1] [,2] [1,] 0 0 [2,] 0 0 l_ind <- list(matrix(c(1,1), ncol = 2), matrix(c(1,1,1,2), ncol = 2)) l_ind [[1]] [,1] [,2] [1,] 1 1 [[2]] [,1] [,2] [1,] 1 1 [2,] 1 2 l_val <- list(5, c(4, 7)) l_val [[1]] [1] 5 [[2]] [1] 4 7

I would like to run Map over the three lists with the goal of replacing in l_zero the zeros with the coordinates in l_ind with the values from l_val. My attempt gives me the following:

Map(function(l_zero, l_ind, l_val) l_zero[l_ind] <- l_val, l_zero = l_zero, l_ind = l_ind, l_val = l_val) [[1]] [1] 5 [[2]] [1] 4 7

As you can see, the original dimensions of the matrices are reduced, but I would like to keep the dimensions of the matrices and just replace the values with the coordinates in l_ind. I tried l_zero[l_ind, drop = FALSE], but that didn't help either.

Can someone help me with this?

Answer1:

Here's a bit simpler method, The [<- replacement function can be used in Map()'s function argument. It takes three arguments, in order.

Map("[<-", l_zero, l_ind, l_val) # [[1]] # [,1] [,2] # [1,] 5 0 # [2,] 0 0 # # [[2]] # [,1] [,2] # [1,] 4 7 # [2,] 0 0

Answer2:

You need to return the modified value from your mapped function (see return(l_zero) below).

l_zero <- replicate(2,matrix(0,2,2),simplify=FALSE) l_ind <- list(matrix(c(1,1), ncol = 2), matrix(c(1,1,1,2), ncol = 2)) l_val <- list(5, c(4, 7)) ff <- function(l_zero, l_ind, l_val) { l_zero[l_ind] <- l_val return(l_zero) } Map(ff, l_zero = l_zero, l_ind = l_ind, l_val = l_val)

Results:

## [[1]] ## [,1] [,2] ## [1,] 5 0 ## [2,] 0 0 ## ## [[2]] ## [,1] [,2] ## [1,] 4 7 ## [2,] 0 0

Recommend

  • Precision loss numpy - mpmath
  • scipy.ndimage.zoom result depends on image size
  • calculate daily averages for 3d array
  • Can someone please explain how this implementation of bucket sort works?
  • Getting a line that has the coordinates defined by the mouse location
  • Replace any string in columns with 1
  • dc.js: Reducing rows in data table
  • Maven-Release-Plugin: Force to use specific version of scm provider
  • Objective C - Create a framework for my iphone apps?
  • drop duplicates pandas dataframe
  • LibGdx GLES2.0 cube texel stretching
  • How to package a jar and all dependencies within a new jar with maven
  • ASP.NET MVC2 Error: No parameterless constructor defined for this object
  • Receive list of elements in their visual order
  • Prolog Ambiguous Output
  • Mocha throws unexpected token error for ES6 object spread operator
  • How to request unsampled report from Google Analytics Management API using Google Apps Script?
  • Efficient User-Agent Regex to find Safari in Python
  • In matplotlib, how do you change the fontsize of a single figure?
  • Who propagate bugfixes across branches (corporate development)?
  • How to print columns containing value
  • xcode don't localize specific strings
  • gspread or such: help me get cell coordinates (not value)
  • Functions in global context
  • Typescript - Unable to get 'import' statement to function
  • Spark fat jar to run multiple versions on YARN
  • ilmerge with a PFX file
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • How to add a column to a Pandas dataframe made of arrays of the n-preceding values of another column
  • script to move all files from one location to another location
  • Running a C# exe file
  • Join two tables and save into third-sql
  • Convert array of 8 bytes to signed long in C++
  • Understanding cpu registers
  • Recursive/Hierarchical Query Using Postgres
  • Binding checkboxes to object values in AngularJs
  • Android Heatmap on canvas or ImageView
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?