Functions of esProc/R/Python/Perl in Structured Data Process by Comparison :Chapter 12.Inserting Records


insert(5,value1:col1,value2:col2,……)        //Insert a record preceding Row 5


In Perl, to insert an element into an array, it may be easy if this is at the beginning, where shift and unshift functions are available, for example:

  @array = (“one”,”two”,”three”);

  $m = shift (@array);            #$m Get “one”, now @array is (“two”, “three”)

  shift @array;                         #@array is (“three”)now

  shift @array;                         #@array is null now

  unshift(@array,5);               #@array is(5) now

  unshift @array,4;                 #@array is(4,5) now

To insert an element in the middle of an array, you will get into big trouble since no existing function is available. There are two alternative methods, take an example as below:


Insert element 3 in position 3 of @array to make the array change to @array=(1,2,3,4,5)

  1. Generate a new array

  @array = ( (@array[0..1]), 3, (@array[2..@array]) );

  1. Write a loop to shift the positions of the elements in turn

  for $i(@array..3){





It is much simpler to insert a record in Python than in Perl. Python provides an insert function and is as easy as esProc.

    a=[[1,”Zhang San”,3000,33],

         [2,”Li Si”,5000,45],

        [3,”Wang Wu”,6000,40]]

    a.insert(2,[4,”Liu Liu”,7000,43])   #Insert the record [4,”Liu Liu”,7000,43] preceding Position 2


It seems that in R, the two data frames can be merged only, while the insertion operation is disabled. Of course, a matrix list will be another matter if this is the case. Since a 2D table is achieved mainly by utilizing data frames, here is only to discuss the data frame.

Here, by merging the data frames, the record can be inserted indirectly, for example:

dd2 <- rbind(dd[1,], dd1,dd[2:3,])             #Indicate to insert dd1 in the middle of Row 1 and 2 of dd



About datathinker

a technical consultant on Database performance optimization, Database storage expansion, Off-database computation. personal blog at: datakeywrod, website: raqsoft
This entry was posted in Structured Data Process and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s