본문 바로가기
통계프로그램 비교 시리즈/오라클함수 비교(R & Python)

LENGTH 오라클 함수 [Oracle, Pandas, R Prog, Dplyr, Sqldf, Pandasql, Data.Table]

by 기서무나구물 2021. 11. 29.

포스팅 목차

    * 파이썬 & R 패키지 호출 및 예제 데이터 생성 링크


    [ LENGTH Oracle Function ]

     


    LENGTH 함수는 문자열의 길이를 반환한다.

     

     

     


    1. Oracle(오라클)

    사원이름(ename)의 문자열 길이를 측정하여 반환한다.

     

    Oracle Programming
    select EMPNO,
           ENAME,
           length(ename) ename_len 
    from   emp

     

    Results
    EMPNO	ENAME	ENAME_LEN
    ------------------------------
    7839	KING	4
    7698	BLAKE	5
    7782	CLARK	5
    7566	JONES	5
    7788	SCOTT	5
    7902	FORD	4
    7369	SMITH	5
    7499	ALLEN	5
    7521	WARD	4
    7654	MARTIN	6
    7844	TURNER	6
    7876	ADAMS	5
    7900	JAMES	5
    7934	MILLER	6

     


    2. Python Pandas(파이썬)

     

    len() 함수

    ename 변수의 길이를 반환한다.

     

    Python Programming
    import copy
    
    withmooc = copy.copy(emp)
    
    withmooc['ename'].str.len().head(7)

     

    Results
    0    5
    1    5
    2    4
    3    5
    4    6
    5    5
    6    5
    Name: ename, dtype: int64

     


    3. R Programming (R Package)

     

    stringr::str_length

    R Programming
    %%R
    
    withmooc <- emp
    
    sapply(withmooc['ename'],stringr::str_length )

     

    Results
          ename
     [1,]     5
     [2,]     5
     [3,]     4
     [4,]     5
     [5,]     6
     [6,]     5
     [7,]     5
     [8,]     5
     [9,]     4
    [10,]     6
    [11,]     5
    [12,]     5
    [13,]     4
    [14,]     6

     


    nchar() 함수

     

    R Programming
    %%R
    
    nchar(as.character(emp$ename))

     

    Results
     [1] 5 5 4 5 6 5 5 5 4 6 5 5 4 6

     

     


    4. R Dplyr Package

     

    stringr::str_length()

    R Programming
    %%R
    
    emp %>%
      dplyr::mutate(ename_len = stringr::str_length(ename)) %>%
      head(10)

     

    Results
    # A tibble: 10 x 9
       empno ename  job         mgr hiredate     sal  comm deptno ename_len
       <dbl> <chr>  <chr>     <dbl> <date>     <dbl> <dbl>  <dbl>     <int>
     1  7369 SMITH  CLERK      7902 1980-12-17   800    NA     20         5
     2  7499 ALLEN  SALESMAN   7698 1981-02-20  1600   300     30         5
     3  7521 WARD   SALESMAN   7698 1981-02-22  1250   500     30         4
     4  7566 JONES  MANAGER    7839 1981-04-02  2975    NA     20         5
     5  7654 MARTIN SALESMAN   7698 1981-09-28  1250  1400     30         6
     6  7698 BLAKE  MANAGER    7839 1981-03-01  2850    NA     30         5
     7  7782 CLARK  MANAGER    7839 1981-01-09  2450    NA     10         5
     8  7788 SCOTT  ANALYST    7566 1982-12-09  3000    NA     20         5
     9  7839 KING   PRESIDENT    NA 1981-11-17  5000    NA     10         4
    10  7844 TURNER SALESMAN   7698 1981-09-08  1500     0     30         6

     

     


    5. R sqldf Package

     

    length() 함수

    length 함수를 사용하여서 사원이름(ename)의 문자열 길이를 측정하여 반환한다.

     

    R Programming
    %%R
    
    sqldf(" select length(ename) ename_len from emp ")

     

    Results
       ename_len
    1          5
    2          5
    3          4
    4          5
    5          6
    6          5
    7          5
    8          5
    9          4
    10         6
    11         5
    12         5
    13         4
    14         6

     

     


    6. Python pandasql Package

     

    length() 함수

    length 함수를 사용하여서 사원이름(ename)의 문자열 길이를 측정하여 반환한다.

     

    Python Programming
    ps.sqldf(" select length(ename) ename_len from emp ").head(7)

     

    Results
    	ename_len
    0	5
    1	5
    2	4
    3	5
    4	6
    5	5
    6	5

     

     


    7. R data.table Package

     

    str_length() 함수

    R Programming
    %%R
    
    DT <- data.table(emp)
    
    DT[, 'tot_len' := str_length(ename)]

     

    Results
        empno  ename       job  mgr   hiredate  sal comm deptno tot_len
     1:  7369  SMITH     CLERK 7902 1980-12-17  800   NA     20       5
     2:  7499  ALLEN  SALESMAN 7698 1981-02-20 1600  300     30       5
     3:  7521   WARD  SALESMAN 7698 1981-02-22 1250  500     30       4
     4:  7566  JONES   MANAGER 7839 1981-04-02 2975   NA     20       5
     5:  7654 MARTIN  SALESMAN 7698 1981-09-28 1250 1400     30       6
     6:  7698  BLAKE   MANAGER 7839 1981-03-01 2850   NA     30       5
     7:  7782  CLARK   MANAGER 7839 1981-01-09 2450   NA     10       5
     8:  7788  SCOTT   ANALYST 7566 1982-12-09 3000   NA     20       5
     9:  7839   KING PRESIDENT   NA 1981-11-17 5000   NA     10       4
    10:  7844 TURNER  SALESMAN 7698 1981-09-08 1500    0     30       6
    11:  7876  ADAMS     CLERK 7788 1983-01-12 1100   NA     20       5
    12:  7900  JAMES     CLERK 7698 1981-12-03  950   NA     30       5
    13:  7902   FORD   ANALYST 7566 1981-12-03 3000   NA     20       4
    14:  7934 MILLER     CLERK 7782 1982-01-23 1300   NA     10       6

     


    8. Python DuckDB의 SQL

     

    Python Programming
    %%sql
      select empno,ename,
             length(ename) ename_len
      from   emp
      LIMIT  6

     

    Python Programming
    duckdb.sql(" select empno,ename,                 \
                        length(ename) ename_len      \
                 from   emp                          \
                 LIMIT  6 ").df()

     

     

    Results
       empno   ename  ename_len
    0   7369   SMITH          5
    1   7499   ALLEN          5
    2   7521    WARD          4
    3   7566   JONES          5
    4   7654  MARTIN          6
    5   7698   BLAKE          5

     

     


     

    [Oracle, Pandas, R Prog, Dplyr, Sqldf, Pandasql, Data.Table] 오라클 함수와 R & Python 비교 사전 목록 링크

     

    오라클 SQL 함수(Oracle SQL Function) 목록 리스트 링크

     

    [SQL, Pandas, R Prog, Dplyr, SQLDF, PANDASQL, DATA.TABLE] SQL EMP 예제로 만나는 테이블 데이터 처리 방법 리스트 링크 링크
    반응형

    댓글