Functions of esProc/R/Python/Perl in Structured Data Process by Comparison:Chapter 7. Inserting, Adding and Removing Column

Uncategorized 678 0

 

esProc

tbl.derive(col1)         // Add a column col1 to the Table sequence

esProc is unable to execute some operations such as deleting the column from the existing Table sequence or inserting the column into the existing Table sequence, however, it is very easy to create a new Table sequence. In fact, the action for creating a new table sequence has the same process as that for adding and deleting the existing Table sequence.

tbl.new(col1,col2,col3)             //Only generate sequences for three columns, col1,col2,col3, delete other columns

    tbl.new(col1,col5,col2,col3)    //Generate the Table sequence of col5 where a new column is inserted

Perl

No concept of the column is defined in the 2D array. When performing column operations, you will end up in trouble since the actions for loop row-by-row and additional delete are required.

The sample code is as follows:

#!perl

    @carts=([173744,”aaa”,14],

                       [173745,”ccc”,18],

                       [173746,”bbb”,13],);

    foreach(@carts){     #Repeat the operation, and insert element 3 in between Column 2 and 3

         $_=[ (@$_[0..1]), 3, (@$_[2..scalar(@$_)-1]) ];                        #Note that here $_ is the array pointer, only when it is prefixed with @ can it indicate the array itself, so that its sub-array is written as @$_[n..m] in order to get a sub-array

    }

    foreach(@carts){     # Repeat the operation for each record, and append element 9 to the end

         push $_,9;        # here $_ is not prefixed with @, which is a little hard to understand

    }

    foreach(@carts){     #Repeat the operation for each record, and delete the third element

         splice ($_, 2, 1);       

    }

Python

Since no concept of the column is defined in the 2D array, the column operation still needs to be repeated row by row, so does the modification operation. However, what is more convenient than Perl comes here that it provides insert and del functions, which allows you to perform Insert and Delete operations on the element of an array in an easy way.

    #coding=gbk 

    carts=[[173744,”aaa”,14],

         [173745,”ccc”,18],

         [173746,”bbb”,13]]        

    for i in range(0,len(carts)):       # Insert the column

         carts[i].insert(2, 3) 

    for i in range(0,len(carts)):       # Append the column

         carts[i].append(9)   

    for i in range(0,len(carts)):       # Delete the column

         del carts[i][2]           

R

    #Add a column to the data frame, which is named score with the column values, 12, 13, 23

    tbl$score<-c(12,13,23)   

Or

    tbl<-cbind(tbl,score=c(12,13,23)) 

    tbl[,-n]                         # Delete Column n of tbl

    tbl$col2=NULL          #Delete that column named col2

FAVOR (0)
Leave a Reply
Cancel
Icon

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

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