# set work directory
setwd("w:/r")
# load odbc library
library(RODBC)
# create an DSN and use it
# myOdbc=odbcConnect("DSN")
orcl=odbcConnect("orcl")
# get data set
# myDataset=sqlFetch(myOdbc,"TableName")
my_data=sqlFetch(orcl,"DIM_RG")
# get columns' name
# names(myDataset)
names(my_data)
# get a column
# myDataset$colname
my_data$BK_REGION
# attach/detach variables in dataset
# attach(myDataset)
# detach(myDataset)
attach(my_data)
# show the unique value of variable
# unique(myVar)
unique(cust_data$SEX)
# select the record you want
cond=cust_data$SEX=="Male"
cust_data[cond,]
# sorting your data
sales_data[order(sales_data$FYEAR),]
sales_data[order(sales_data$FYEAR,sales_data$FSALES),]
sales_data[rev(order(sales_data$FYEAR,sales_data$FSALES)),]
sales_data[order(sales_data$FYEAR,order(sales_data$FSALES)),]
sales_data[order(sales_data$FYEAR,decreasing=T),]
# merge data tables
# merge(dataset1,dataset2,by=merge_column,all=TRUE/FALSE)
my_data=merge(sales_data,cust_data,by="CUSTID")
# output the data to a file
write.table(my_data,file="output_my_data.txt",sep=",",quote=FALSE,append=FALSE,na="N/A")
# show the properties of a dataframe
str(my_data)
# recode the value of variables
my_data$fsex=factor(my_data$SEX,levels=c("Male","Female"),labels=c("男","女"))
# boxplot test
boxplot(FSALES ~ SEX,data=my_data)
boxplot(FSALES ~ CUSTNAME,data=my_data)