Monday, 20 April 2015

r updates and packages

The package 'installr' makes it simple to upgrade R to a new version with the following command

library(installr) 


To install the latest version of R:

install.R()


To install the associated program used with R:

The below lists only some of the examples

install.ImageMagick()  
install.Rtools() 
install.MikTex()  
install.pandoc()   


To update installed R to the latest version:

updateR()


To install packages, you can set the source by specifying 'repos=' and specify the path to the library by setting 'lib=' (in Windows, .libPaths() can be used to set up a default path to the library folder at the beginning for the session)

install.packages("dplyr", repos="http://cran.csiro.au/", lib="C:/Users/Documents/MyLibrary")

   
If you are installing packages from the local directory, provide path and name of the file to be installed.

          install.packages("[path to the directory]/[name of file (e.g. ada_2.0-5.zip)]", repos=NULL,  type="source")
 
 
The list of all repos url's can be found at http://cran.r-project.org/mirrors.html


To list all the packages installed at a specified library path

installed.packages(lib.loc="path")

  
To check if any packages in the library are old,   
  

old.packages()

    
To check for new packages,   
  
new.packages()
 

To update existing packages to the latest version  


update.packages()

 


For those work places using Windows where firewall prevents downloading of the packages, you may need to download zip files from the source and install them locally by using the tool bar options in the R console.  

   




To check what packages are available:

(.packages())


To check all available packages:

(.packages(all.available=TRUE))

alternatively,

library()

 


To check for all packages in the repository:
available.packages()


To remove packages:

remove.packages("name_of_package","library_path")


  
  
To download the package in its compressed form (e.g. *.tar.gz):

download.packages("name_of_package",destdir="destination_path")








  
  
To download multiple packages at once,

download.packages(c("name_of_package1","name_of_package2","name_of_package3"), destdir="destination_path")

   
  
A contributed package called 'tools' provides useful function to check for dependencies of the installed packages:

library(tools)   
inst_pack<-installed.packages()   
package.dependencies(inst_pack)
     










Friday, 10 April 2015

Intersection & Union


Let's set vectors
Group1<-c("A","R","t","Y","p","Q") 
Group2<-c("Z","R","t","O","p","X")

> Group1  
[1] "A" "R" "t" "Y" "p" "Q"


> Group2   
[1] "Z" "R" "t" "O" "p" "X"


Intersection: there are 2 ways
> intersect(Group1,Group2)
[1] "R" "t" "p"

> Group1[which(Group1%in%Group2)]
[1] "R" "t" "p"


























Union:
> union(Group1,Group2)   
[1] "A" "R" "t" "Y" "p" "Q" "Z" "O" "X"

























Elements exclusive to Group 1: there are 2 ways
> setdiff(Group1,Group2)  
[1] "A" "Y" "Q"   

> Group1[which(!Group1%in%Group2)]   
[1] "A" "Y" "Q"
























Elements exclusive to Group 2: there are 2 ways
> setdiff(Group2,Group1)   
[1] "Z" "O" "X"

> Group2[which(!Group2%in%Group1)]  
 
[1] "Z" "O" "X"

























Mutually exclusive elements
> union(setdiff(Group1,Group2),setdiff(Group2,Group1))   
[1] "A" "Y" "Q" "Z" "O" "X"














Friday, 6 February 2015

Working on Excel file from R

There are few packages available in R to let you work with (import/export) Excel files from R. This blog page will focus on 'xlsx' package.


'xlsx' package provides a command to import excel file, but this did not work well in my case. If the excel document is in Excel 97-2003 (.xls) format, the following works quite well.

Note: if ODBC installed in Windows is 32-bit, you need to use 32-bit R to do this.

library(RODBC) 
library(xlsx) 

excel.connect<-odbcConnectExcel("file_name.xls") 
Data<-sqlFetch(excel.connect,"Sheet_Name") 
odbcClose(excel.connect)


For exporting data into excel document, the following works well.

For a simple export:
library(xlsx)
wb<-createWorkbook() 
sheet<-createSheet(wb,sheetName="Sheet_Name") 
addDataFrame(dataframe,sheet,row.names=FALSE)  
saveWorkbook(wb,"file_name")


To create multiple worksheets:
wb<-createWorkbook() 
sheet<-createSheet(wb,sheetName="Sheet_Name_1") 
addDataFrame(dataframe_1,sheet,row.names=FALSE)  
saveWorkbook(wb,"file_name")

sheet<-createSheet(wb,sheetName="Sheet_Name_2") 
addDataFrame(dataframe_2,sheet,row.names=FALSE)  
saveWorkbook(wb,"file_name")


For exporting data with formatting:

wb<-createWorkbook()
 
sheet<-createSheet(wb,sheetName="Sheet_Name") 

for(i in c(required row numbers){
 
ROW<-createRow(sheet,rowIndex=i)

#populate selected columns (selected respective to different formatting)

for(j in c(required column numbers)){ 
CELL<-createCell(ROW,colIndex=j)[[1,1]]  

#data format written as per the excel specification (go to cell format and custom format for other examples)

cs<-CellStyle(wb,dataFormat=DataFormat("#,##0;[Red]-#,##0"))+
Font(wb,isBold=FALSE)+
Alignment(h="ALIGN_RIGHT",wrapText=TRUE) 
setCellValue(CELL,dataframe[i,j],showNA=FALSE)  
setCellStyle(CELL,cs)
}

#populate remaining columns (selected respective to different formatting)

for(j in c(required column numbers)){ 
CELL<-createCell(ROW,colIndex=j)[[1,1]]   
cs<-CellStyle(wb,dataFormat=DataFormat("$#,##0.00;[Red]-$#,##0.00"))+
Font(wb,isBold=FALSE)+
Alignment(h="ALIGN_RIGHT",wrapText=TRUE) 
setCellValue(CELL,dataframe[i,j],showNA=FALSE)  
setCellStyle(CELL,cs)
}














Number formatting

For presentation of numeric tables in reports, one often has to use '000 separators and use equal spacing to make the results look neater and pretty.

formatC() in the base package is useful for this purpose.


Example 1:

Given the below input
data.frame(V1=c((-1:1)/0,c(1,100)*pi))
          V1
1       -Inf
2        NaN
3        Inf
4   3.141593
5 314.159265


converted to below
 data.frame(V1=formatC(c((-1:1)/0,c(1,100)*pi), width = 8, digits = 1))
        V1
1     -Inf
2      NaN
3      Inf
4        3
5    3e+02


Alternatively for 2 digits displayed
data.frame(V1=formatC(c((-1:1)/0,c(1,100)*pi), width = 8, digits = 2))
        V1
1     -Inf
2      NaN
3      Inf
4      3.1
5  3.1e+02


Example 2:

Given the below numbers

12344448.2 
3244149.9210001 
321.21 
78186271.32155

To only show integers
data.frame(V1=formatC(c(12344448.2,3244149.9210001,3217747.214,78186271.32155),format="d"))
        V1
1 12344448 
2  3244149 
3  321 
4 78186271

To show numbers with scientific format

data.frame(V1=formatC(c(12344448.2,3244149.9210001,321.21,78186271.32155),format="e"))
          V1 
1 1.2344e+07 
2 3.2441e+06 
3 3.2121e+02 
4 7.8186e+07


To show numbers with scientific format when the number is big

data.frame(V1=formatC(c(12344448.2,3244149.9210001,321.21,78186271.32155),format="g")) 
         V1
1 1.234e+07
2 3.244e+06
3     321.2
4 7.819e+07


To show numbers in its actual form (this is limited by the options setting in R)

data.frame(V1=formatC(c(12344448.2,3244149.9210001,321.21,78186271.32155),format="f")) 
             V1
1 12344448.2000
2  3244149.9210
3      321.2100
4 78186271.3215


To show remainder of digits in decimal points, specify 'digits' parameter (but format="f" will change the original input to comply with the new setting - so this should be used when you want to round off figures to smaller number of digits)

data.frame(V1=formatC(c(12344448.2,3244149.9210001,321.21,78186271.32155),format="f",digits=15)) 
                        V1
1 12344448.199999999254942
2  3244149.921000100206584
3      321.209999999999980
4 78186271.321549996733665


Use format="fg" in combination with 'digits' specification to display the actual input

data.frame(V1=formatC(c(12344448.2,3244149.9210001,321.21,78186271.32155),format="fg",digits=15)) 
                V1
1       12344448.2
2  3244149.9210001
3           321.21
4   78186271.32155



To insert '000 separator

data.frame(V1=formatC(c(12344448.2,3244149.9210001,321.21,78186271.32155),format="fg",digits=15,big.mark=",")) 
                 V1
1      12,344,448.2
2 3,244,149.9210001
3            321.21
4  78,186,271.32155


To make the decimal point more obvious

data.frame(V1=formatC(c(12344448.2,3244149.9210001,321.21,78186271.32155),format="fg",digits=15,big.mark=",",decimal.mark="\\")) 
                  V1
1      12,344,448\\2
2 3,244,149\\9210001
3            321\\21
4  78,186,271\\32155


To insert separators in the decimal points

data.frame(V1=formatC(c(12344448.2,3244149.9210001,321.21,78186271.32155),format="fg",digits=15,big.mark=",",decimal.mark="\\",small.mark=" ")) 
                   V1
1       12,344,448\\2
2 3,244,149\\92100 01
3             321\\21
4   78,186,271\\32155

















Friday, 30 January 2015

Directory & File Management

Managing directory from R becomes handy when automating batch processes, and helps with managing disk spaces when extracting and manipulating large volumes of data.


To check contents of the current (working) directory:

dir()



To obtain more information about the directory or files:

file.info(dir())  
  
file.info("home/document/folder_name")   



To set working directory:

setwd("home/document/folder_name")



To identify path to the current (working) directory:

getwd()



To create a new directory (you can only specify the file name if you are creating the new directory within the working directory):

dir.create("home/document/folder_name")
   

To create a new file:

file.create("FILE.txt")  



To create a temporary file or directory:

tempfile()  
  


To remove all files in a directory:

unlink("home/document/folder_name/*")



To delete a directory (you need to ensure that the directory you are trying to delete is not set as the working directory):

unlink("home/document/folder_name",recursive=TRUE)




To remove a file:

file.remove("home/document/folder_name/file_name.txt")



To check if a certain file exists:

file.exists("home/document/folder_name/file_name.txt")



To select files in the directory using wildcard:

Sys.glob("M*")  



To copy a file from one directory to another:

file.copy("home/document/folder_name/file_name.txt","home/document/folder_name2/file_name.txt")




To copy a file and replace a existing file with the same name:

file.copy("home/document/folder_name/file_name.txt","home/document/folder_name2/file_name.txt",overwrite=TRUE)


To copy multiple files from one directory to another:

file.copy(FILES,paste("home/document/folder_name2",FILES,sep="/"))



To change name of a file:

file.rename("home/document/folder_name/file_name.txt","home/document/folder_name/file_name2.txt")


To append files:

file.append("home/document/folder_name/file_name1.txt","home/document/folder_name/file_name2.txt","home/document/folder_name/file_name3.txt")

To compress a file into zip file:

zip("home/document/folder_name/zip_file_name.zip","home/document/folder_name/file_name.txt")



To extract files from compressed zip files:

unzip("home/document/folder_name/zip_file_name.zip",exdir="home/document/folder_name2")



To extract files from tar archive:

untar("home/document/folder_name/tar_file_name.tar",exdir="home/document/folder_name2")



To import gz files into R:
 

dat <- read.table(gzfile("path/file/txt.gz"), sep=", ", header = TRUE)