# 6/10/2015

nchar("leela") ## to get number of characters
## [1] 5
substr("statistics",1,5)
## [1] "stati"
x<-c("leela","sneha","sravs")
substr(x,1,4)
## [1] "leel" "sneh" "srav"
z<-c("us","uk")
substr(z,1,5)
## [1] "us" "uk"

to replace

sub(“old”,“new”,stringname)

l<-"curly is the smart one.curly is funny too."
sub("curly","moe",l) ## to replace only one word
## [1] "moe is the smart one.curly is funny too."
gsub("curly","moe",l) ## to replace only all word
## [1] "moe is the smart one.moe is funny too."
x<-"table prints its required arguements.table."
sub("prints","value",x,"its","that",x)
## [1] "table value its required arguements.table."
gsub("prints","value",x)
## [1] "table value its required arguements.table."

you have twosets of strings and you want to generate all combinations from those two sets

use the outer and pastefunctions together to generate amatrix of all possible combinations

paste: to combine two elements

locations<-c("NY","LA","CHI","HOU")
treatments<-c("T1","T2","T3")
outer(locations,treatments,paste,sep="+")
##      [,1]     [,2]     [,3]    
## [1,] "NY+T1"  "NY+T2"  "NY+T3" 
## [2,] "LA+T1"  "LA+T2"  "LA+T3" 
## [3,] "CHI+T1" "CHI+T2" "CHI+T3"
## [4,] "HOU+T1" "HOU+T2" "HOU+T3"
paste("leela","ambati",sep="")
## [1] "leelaambati"
class(paste("leela","ambati",sep=""))
## [1] "character"
paste("leela","ambati",sep="?")
## [1] "leela?ambati"
class(paste("leela","ambati",sep="?"))
## [1] "character"

getting curent date

sys.Date() class(sys.date()) # problm #you have the string implementation of a date,such as “2010-12-31”,and you want to convert that into a Date object #we have to use as.Date #by default as.Date assumes the string looks like yyyy-mm-dd # to handle in other formats we must specify format as per our requirement .use format=“%m/%d/%y.”

as.Date("2015/10/6")
## [1] "2015-10-06"
as.Date("11/10/2020",format="%m/%d/%y")
## [1] "2020-11-10"
as.Date("1/12/2015",format="%d/%m/%y")
## [1] "2020-12-01"
as.Date("12/1/2015",format="%m/%d/%Y")
## [1] "2015-12-01"
class(as.Date("12/1/2015",format="%m/%d/%Y"))
## [1] "Date"

ISOdate(2012,2,29)

as.Date(ISOdate(2012,2,29)) # also possible to add >ISOdatetime(year,month,day,hour,minute,second)

problem given a date object,you want to extract the julian date-which isin R,the number of days since January 1, 1970.

ISOdate(2015,10,6)
## [1] "2015-10-06 12:00:00 GMT"
as.Date(ISOdate(2015,10,6))
## [1] "2015-10-06"
class(as.Date("2015/10/6",format="%Y/%m/%d"))
## [1] "Date"
z<-ISOdate(2015,10,6)
a<-as.Date(ISOdate(2015,10,6))
format(a,"%Y")
## [1] "2015"
class(format(a,"%Y"))
## [1] "character"
as.integer(format(a,"%Y"))
## [1] 2015
z<-ISOdate(2015,15,12)
a<-as.Date(ISOdate(2015,15,12))
format(a,"%Y")
## [1] NA
a<-c(1,1,2,2,2,3,4,9,15)
a
## [1]  1  1  2  2  2  3  4  9 15
hist(a)