Functions of esProc/R/Python/Perl in Structured Data Process by Comparison:Chapter 6.Modifying Field Value

Uncategorized 785 0

esProc

tbl.modify(2,col1*5:col1,col2+3:col2)       //field value col1 recorded in Row 2 of Table Sequence tbl is changed to col1*5, and field value col2 to col2+3

tbl.modify(2:5,col1*5:col1,col2+3:col2)          //Subsequent 5 Rows are modified from Row 2 in a consecutive way

//Modify the two Table sequences in the same dimension synchronously

A1=db.query(“select id,name,salary from salary order by id”)

A2=db.query(“select id,bonus from bonus order by id”)

A1.modify(1:A2,salary+bonus:salary)               //A1 and A2 are sorted by id field, and bonus field value of A2 is added to the salary field of A1 in sequence

Perl

    # In Perl, it is more convenient to modify the element value for 1D array, and you can directly assign a value, such as:

    @a=(1,2,3);

    $a[2]=$a[2]*2;                   # Multiply the third element value by 2

    Foreach (@a){           #Loop through the array @a, and multiply each element value by 2; $_ indicates the current element

             $_=$_*2;

    }

    map {$_=$_*2}@a;          # It is also allowed to use a loop function map to multiply any element value by 2

The syntax for Loop function map is more convenient, but it only provides a loop variable $_ used to indicate the current element. No loop sequence number can be available. A variablecumulative sum must be otherwise defined if you want to perform filter operation depending on member sequence numbers, which will get you into a same awkward situation as that caused in writing the loop statements.

# 2D arrays come here as follows:

    my @table=([1,”Zhang San”,3000,33],

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

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

                                );

    $table[1][2]=$table[1][2]*2;    # Multiply the values of Column 3 in Row 2 of 2D array @table by 2

    $table[1][3]=$table[1][3]+2;    #If the values of two fields are modified synchronously, two statements must be needed

    # If you want to modify several rows continuously, only the loop statement is written as below:

    for $i(1..5){       #Modify a queue of rows from Row 2 to Row 6

             $table[$i][2]=$table[$i][2]*2; 

    $table[$i][3]=$table[$i][3]+2;

    }

 

    # If the two 2D tables in the same dimension and with same sequence will be modified, for example:

    my @salary=([1,”Zhang San”,3000,33],

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

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

                                );

    my @bonus=([1,750],

                                [2,800],

                                [3,950]

                                );

    foreach $i(0..$#salary){   #Repeat the operation for each record, and add the values of Column 2 in the bonus to Column 3 of salary

             $salary[$i][2]=$salary[$i][2]+$bonus[$i][1];

    }

Python

    # coding=gbk 

    a=[1,2,3,4]

    a[2]=10              # Modify the value of individual element in 1D array

 

    for i in range(0,len(a)):              #Modify the values of all the elements in 1D array

         a[i]=a[i]*3

 

    data = [   [121.1, 0.02, 0.02],

                       [121.1, 0.05, 0.05],

                       [122.1, 0.56, 0.6],

                       [122.1, 3.79, 4.04],

                       [123.1, 93.75, 100.0],

                       [123.1, 0.01, 0.01],

                       [124.1, 0.01, 0.01],

                       [124.1, 1.01, 1.08],

                       [124.1, 0.11, 0.11],

                       [124.1, 0.05, 0.06],

                       [125.1, 0.39, 0.41],]

    data[2][1]=5                                          #Modify the value of individual element in 2D array

    for i in range(0,len(data)):                 #Modify the values of a column in 2D array

       data[i][1]=data[i][1]*3

 

#Two 2D arrays are modified synchronously as below

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

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

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

    bonus=[[1,750],

                       [2,800],

                       [3,950]]

    for i in range(0,len(salary)):     #Repeat the above operation for each record, the values of Column 2 in the bonus are added to Column 3 of salary

             salary[i][2]= salary[i][2]+ bonus[i][1];

Loop functions for array-modification is unavailable in Python, and you need to write a loop program to achieve this action; column-based operations for 2D array can be achieved only by writing the loop codes.

R

    tbl$c2=tbl$c2*5       #The value of Column c2 is modified as original value times 5

    tbl [tbl$c1<5,]$c2=tbl[tbl$c1<5,]$c2+5            #The record field c2, for which c1<5 is true, is modified as original value plus 5

    tbl [c(1,2,4),]$c2=tbl[c(1,2,4),]$c2+5                #The field values of Rows 1, 2 and 4 are modified

Synchronous modification for two data frames is not directly supported, which needs to be achieved by coding.

FAVOR (0)
Leave a Reply
Cancel
Icon

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

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