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

esProc 757 0

esProc

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

Perl

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:

    e.g. @array=(1,2,4,5)

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]) );

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

    for $i(@array..3){

    $array[$i]=$array[$i-1];

    }

    $array[2]=3;

Python

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 .

R

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

esProc_r_perl_python_12

FAVOR (0)
Leave a Reply
Cancel
Icon

Hi,You need to fill in the Username and Email!

  • Username (*)
  • Email (*)
  • Website