2012年10月11日木曜日

test

R page

R page

Vectors

Vectors are one-dimensional arrays that can hold numeric data, character data, or logical data.

Matrices

A matrix is a two-dimentional array where each element has the same mode(numeric, character, or logical).

Arrays

Arrays are similar to matrices but can have more than two dimentions.

Data frames

A data frames is more general than a matrix in that different columns can contain different modes of data (numeric, character, etc).

Factors

As you've seen, variables can be described as nominal, ordinal, or continuous.

Lists

Lists are the most complex of the R data types. Basically, a list is an ordered collection of objects (components).

2012年10月7日日曜日

Cleaning data

"H22.1.4" have to be converted to like this, "2010.1.4"


Then, turn the expression like "2010.1.4" into date value using "as.Date()" function

I wanna turn "H21" into "2009", "1.4" into "01.04" for as.Date() conversion.

So, what should I do?

Using Python

I try to do my mission by using Python.

My Python program should read the file ("something.csv" file suppose) and convert texts of date in the file into ideal way of expression.

2012年9月28日金曜日

Introducing the os Modules

Introducing the os Modules

Tools in the os Modules

Table 2-1. Commonly used os module tools
Tasks Tools
Shell variables os.environ
Running programs os.system, os.popen, os.execv, os.spawnv
Spawning processes os.fork, os.pipe, os.waitpid, os.kill
Descriptor files, locks os.open, os.read, os.write
File processing os.remove, os.rename, os.mkfifo, os.mkdir, os.rmdir
Administrative tools os.getcwd, os.chdir, os.chmod, os.getpid, os.listdir, os.access
Portability tools os.sep, os.pathsep, os.curdir, os.path.split, os.path.join
Pathname tools os.path.exists('path'), os.path.isdir('path'), os.path.getsize('path')

 

Administrative Tools

>>> os.getpid()
7980
>>> os.getcwd()
'C:\\PP4thEd\\Examples\\PP4E\\System'

>>> os.chdir(r'C:\Users')
>>> os.getcwd()
'C:\\Users'

 

Portability Constants

>>> os.pathsep, os.sep, os.pardir, os.curdir, os.linesep
(';', '\\', '..', '.', '\r\n')

 

Common os.path Tools

>>> os.path.isdir(r'C:\Users'), os.path.isfile(r'C:\Users')
(True, False)
>>> os.path.isdir(r'C:\config.sys'), os.path.isfile(r'C:\config.sys')
(False, True)
>>> os.path.isdir('nonesuch'), os.path.isfile('nonesuch')
(False, False)

>>> os.path.exists(r'c:\Users\Brian')
False
>>> os.path.exists(r'c:\Users\Default')
True
>>> os.path.getsize(r'C:\autoexec.bat')
24
 
>>> os.path.split(r'C:\temp\data.txt')
('C:\\temp', 'data.txt')

>>> os.path.join(r'C:\temp', 'output.txt')
'C:\\temp\\output.txt'

>>> name = r'C:\temp\data.txt'                            # Windows paths
>>> os.path.dirname(name), os.path.basename(name)
('C:\\temp', 'data.txt')

>>> name = '/home/lutz/temp/data.txt'                     # Unix-style paths
>>> os.path.dirname(name), os.path.basename(name)
('/home/lutz/temp', 'data.txt')

>>> os.path.splitext(r'C:\PP4thEd\Examples\PP4E\PyDemos.pyw')
('C:\\PP4thEd\\Examples\\PP4E\\PyDemos', '.pyw') 

 

Running Shell Commands from Script

Other os module Exports

Introducing the sys Module

Introducing the sys Modeule

Platforms and Versions

C:\...\PP4E\System> python
>>> import sys
>>> sys.platform, sys.maxsize, sys.version
('win32', 2147483647, '3.1.1 (r311:74483, Aug 17 2009, 17:02:12) ...more deleted...')

>>> if sys.platform[:3] == 'win': print('hello windows')
...
hello windows

 

The Module Search Path

>>> sys.path
['', 'C:\\PP4thEd\\Examples', ...plus standard library paths deleted... ]
>>> sys.path.append(r'C:\mydir')
>>> sys.path
['', 'C:\\PP4thEd\\Examples', ...more deleted..., 'C:\\mydir'] 

The Loaded Modules Table

>>> sys.modules
{'reprlib': <module 'reprlib' from 'c:\python31\lib\reprlib.py'>, ...more deleted...

>>> list(sys.modules.keys())
 ['reprlib', 'heapq', '__future__', 'sre_compile', '_collections', 'locale', '_sre',
'functools', 'encodings', 'site', 'operator', 'io', '__main__', ...more deleted... ]

>>> sys
<module 'sys' (built-in)>
>>> sys.modules['sys']
<module 'sys' (built-in)>

Exception Details

>>> import traceback, sys
>>> def grail(x):
...     raise TypeError('already got one')
...
>>> try:
...     grail('arthur')
... except:
...     exc_info = sys.exc_info()
...     print(exc_info[0])
...     print(exc_info[1])
...     traceback.print_tb(exc_info[2])
...
<class 'TypeError'>
already got one
  File "<stdin>", line 2, in <module>
  File "<stdin>", line 2, in grail

 

Programming Python

2 System Programming

  • System scripting overview
  • Intruducing sys Module
  • Introducing os Module

System Scripting Overview

  • dir, dir(sys), dir(os), dir(os.path)
  • import, import sys, import os
  • sys.__doc__, print(sys.__doc__)
  • help(sys)
  • >>> line = 'aaa\nbbb\nccc\n' >>> line.split('\n') ['aaa', 'bbb', 'ccc', ''] >>> line.splitlines() ['aaa', 'bbb', 'ccc']
  • String Method Basics
    • mystr.find('XXX'), >>> mystr.find('SPAM') # return first offset 3
    • mystr.replace('AA', 'XXX'), >>> mystr.replace('aa', 'SPAM') # global replacement 'xxSPAMxxSPAM'
    • 'XXX' in mystr, >>> 'SPAM' in mystr # substring search/test True