- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to add a new value to each element of list in R?
Suppose we have a list that contain two elements and we get a new value for both of these elements then the problem of adding those values to the original list arises. This can be done with the help of mapply function. We can append the new values in the original easily but a vector of the new values needs to be created first.
Example1
List1<−list(x1=rnorm(50),x2=rnorm(50)) new<−list(0.34,0.37)
Adding new values to List1 −
Example
List1<−mapply(append,List1,new,SIMPLIFY=FALSE) List1
Output
$x1 [1] −0.21433819 0.34745333 −0.91392492 −1.18555411 −0.99932134 −0.57548735 [7] 1.10651668 0.48454439 −0.99232515 −0.99568362 0.23641052 2.67800406 [13] −0.75788305 1.19569169 −1.15838659 0.73623778 −0.17743427 1.30542832 [19] −1.76846085 −0.59177306 0.17272266 −0.63534109 −0.23215985 −1.67575615 [25] 0.59034063 2.52226746 0.40437735 −0.06360738 −0.53810661 −0.04455344 [31] 0.71511225 −0.35993230 1.29632787 0.13729957 −0.52563320 0.46982761 [37] 0.12234186 −1.23848802 1.29268576 −0.05836899 −0.66205174 −0.07619300 [43] −1.04585532 3.67861858 0.73080674 −1.36358925 −0.78699970 0.41224880 [49] 0.86365984 1.78372714 0.34000000 $x2 [1] −1.008028153 0.753706027 0.339136404 0.394850535 0.822800452 [6] 1.131092940 −0.044501179 −0.247914864 −0.350871896 0.013754292 [11] 0.002728324 0.776690869 −1.002677732 −1.236941825 0.599039509 [16] 0.532717545 0.262159708 −0.373315320 −1.609664791 −2.034626869 [21] 0.563657076 0.440259691 1.690137148 −0.937983631 −1.661222786 [26] 0.325805813 0.111868681 2.189960485 1.200441769 −0.178213068 [31] −0.628382611 −1.777857157 0.893458151 1.009731364 −0.657754484 [36] 0.788648791 −1.343786681 0.993323519 0.021705024 −0.719988792 [41] 0.458443258 −0.624894337 1.466201931 −0.320907500 −1.327935744 [46] 1.397215872 0.352245992 −0.278273447 0.864011297 −0.979787012 [51] 0.370000000
Example2
List2<−list(y1=rpois(99,10),y2=rpois(99,10)) new<−list(5,3)
Adding new values to List2 −
Example
List2<−mapply(append,List2,new,SIMPLIFY=FALSE) List2
Output
$y1 [1] 9 19 3 6 10 7 6 8 10 3 6 10 11 7 9 11 13 8 10 8 8 11 10 13 9 [26] 15 5 10 11 10 15 14 5 10 5 7 9 14 10 14 9 11 13 13 12 8 9 13 13 7 [51] 13 9 9 9 13 10 9 13 10 8 9 11 7 8 4 9 11 8 14 8 4 8 11 13 15 [76] 9 8 7 13 10 13 7 13 10 5 8 8 11 7 4 7 15 7 5 11 9 10 13 11 5 $y2 [1] 5 10 8 8 5 14 13 5 7 15 14 12 8 6 15 8 9 6 9 9 14 9 7 9 7 [26] 14 12 2 12 10 14 12 13 11 9 11 10 5 10 9 13 8 8 11 11 18 12 8 14 11 [51] 12 14 18 9 9 16 15 7 13 10 12 8 8 10 7 8 6 14 9 9 10 20 11 10 8 [76] 12 6 9 5 12 10 9 9 6 11 10 11 14 8 11 11 11 9 10 6 9 13 11 13 3
Example3
List3<−list(z1=runif(49,2,10),z2=runif(49,5,10)) new<−list(3.24542,7.21421)
Adding new values to List3 −
List3<−mapply(append,List3,new,SIMPLIFY=FALSE) List3
Output
$z1 [1] 7.291533 6.972070 3.586818 6.973179 5.138934 7.952336 3.354811 8.508972 [9] 9.436393 7.226165 4.316177 7.212349 2.944707 8.458374 5.844430 6.623924 [17] 8.705829 2.509108 9.074396 3.360153 9.034577 5.679748 9.096813 6.989252 [25] 3.814996 4.227093 8.546505 7.225875 3.246433 4.620028 7.876439 2.746531 [33] 4.588123 3.770730 7.954941 4.840489 5.309890 3.292286 6.110796 6.506668 [41] 9.662596 5.216663 8.697122 6.700522 3.520535 9.827375 8.321387 4.603970 [49] 5.582278 3.245420 $z2 [1] 7.360781 7.984698 9.110684 9.866965 7.821411 7.912576 8.132087 6.343781 [9] 8.635346 5.778531 9.980152 6.572830 5.289732 7.245998 6.681521 8.756547 [17] 9.282966 5.567102 7.823283 9.973521 7.758861 5.727483 9.618834 5.911291 [25] 7.229435 8.783410 7.815542 7.840888 8.514780 7.798654 7.415919 8.948754 [33] 7.046777 7.855829 6.973687 6.351384 9.041160 9.727344 7.731367 7.100542 [41] 6.987909 7.940805 9.893710 7.400530 9.528001 6.247381 8.108900 8.553423 [49] 9.856233 7.214210
Example4
List4<−list(a1=sample(0:9,49,replace=TRUE),a2=sample(0:9,49,replace=TRUE)) new<−list(5,2)
Adding new values to List4 −
List4<−mapply(append,List4,new,SIMPLIFY=FALSE) List4
Output
$a1 [1] 2 4 7 0 8 7 8 8 3 5 9 4 6 6 6 5 4 9 7 1 0 7 8 3 3 6 7 2 3 2 2 5 2 4 7 4 2 0 [39] 7 9 3 3 4 6 7 3 4 0 9 5 $a2 [1] 5 7 4 8 9 4 1 8 5 6 9 0 4 2 7 2 6 2 0 1 6 7 3 9 6 8 2 5 4 1 2 3 7 3 5 4 5 1 [39] 5 7 9 9 3 6 4 4 1 8 4 2
- Related Articles
- How to add a new column to an R data frame with largest value in each row?
- How to add a new list element under a using jQuery?
- Add a scalar value to each element and return a new masked array in NumPy
- How to add a value to a particular matrix element in R?
- How to check which list element contains a particular value in R?
- How to add a new column to a matrix in R?
- How to add or multiply each element of a matrix to the corresponding element of another matrix in R, if these matrices are stored as a list?
- Map to add String value to each element in Java
- How to add a new element to HTML DOM in JavaScript?
- How to add a new value to a collection in Laravel?
- How to add a new element in the XML using PowerShell?
- How to check if a list element is greater than a certain value in R?
- How to save list where each element contains equal number of values to a text file in R?
- How to add a vector to each row of a matrix in R?
- How to extract first n values from each element in an R list?

Advertisements