Saturday, 26 November 2016

19 string functions in r Language

substr(,1,5)
nchar()
substr(,nchar()-8,nchar()
how to extract last 2 chars from vector
paste(a,b,sep='')
paste(a,b,"!",sep='')
paste(a,", ",b,"!",sep='')
paste(vector,", ",b,"!",sep='')
paste("x",1:10,sep=",")
grep("Ra",vector)
vector[grep("Ra",vector)]
grep("Ra",vector,value=TRUE)
grep("New",state.name,value=T)
strsplit(a,"   ")
sub("ram","nar",a)
gsub
----------

R version 3.3.2 (2016-10-31) -- "Sincere Pumpkin Patch"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

[Workspace loaded from ~/.RData]

> vec <-c("ramesh","suresh","naresh","ganesh")
> v<-"hi how are you"
> V1<-"just"
> ?substr
> substr(v,1,5)
[1] "hi ho"
> substr(v,4,20)
[1] "how are you"
> substr(v,4,2)
[1] ""
> substr(v,4,9)
[1] "how ar"
> nchar(v)
[1] 14
> v
[1] "hi how are you"
> nchar(vec)
[1] 6 6 6 6
> vec
[1] "ramesh" "suresh" "naresh" "ganesh"
> v
[1] "hi how are you"
> substr(v,nchar(v)-3,nchar(v))
[1] " you"
> substr(v,nchar(v)-3,nchar(v))
[1] " you"
> substr(vec,1,2)
[1] "ra" "su" "na" "ga"
> paste(v,v1)
Error in paste(v, v1) : object 'v1' not found
> v1
Error: object 'v1' not found
> V1
[1] "just"
> paste(v,V1)
[1] "hi how are you just"
> paste(v,V1,sep=' / ')
[1] "hi how are you / just"
> paste(v,V1,"!",sep=' / ')
[1] "hi how are you / just / !"
> paste(v,V1,"Ram",sep=' / ')
[1] "hi how are you / just / Ram"
> paste(v,",",V1,"Ram",sep=' / ')
[1] "hi how are you / , / just / Ram"
>
> paste(V1,1:10)
 [1] "just 1"  "just 2"  "just 3"  "just 4"
 [5] "just 5"  "just 6"  "just 7"  "just 8"
 [9] "just 9"  "just 10"
> grep("ra",vec)
[1] 1
> grep("esh",vec)
[1] 1 2 3 4
> vesh
Error: object 'vesh' not found
> vec
[1] "ramesh" "suresh" "naresh" "ganesh"
> grep("a",vec)
[1] 1 3 4
> grep("a",vec,value=T)
[1] "ramesh" "naresh" "ganesh"
> state.name
 [1] "Alabama"        "Alaska"      
 [3] "Arizona"        "Arkansas"    
 [5] "California"     "Colorado"    
 [7] "Connecticut"    "Delaware"    
 [9] "Florida"        "Georgia"     
[11] "Hawaii"         "Idaho"       
[13] "Illinois"       "Indiana"     
[15] "Iowa"           "Kansas"      
[17] "Kentucky"       "Louisiana"   
[19] "Maine"          "Maryland"    
[21] "Massachusetts"  "Michigan"    
[23] "Minnesota"      "Mississippi" 
[25] "Missouri"       "Montana"     
[27] "Nebraska"       "Nevada"      
[29] "New Hampshire"  "New Jersey"  
[31] "New Mexico"     "New York"    
[33] "North Carolina" "North Dakota"
[35] "Ohio"           "Oklahoma"    
[37] "Oregon"         "Pennsylvania"
[39] "Rhode Island"   "South Carolina"
[41] "South Dakota"   "Tennessee"   
[43] "Texas"          "Utah"        
[45] "Vermont"        "Virginia"    
[47] "Washington"     "West Virginia"
[49] "Wisconsin"      "Wyoming"     
> grep("V",sate.name,value=T)
Error in grep("V", sate.name, value = T) : object 'sate.name' not found
> grep("V",state.name,value=T)
[1] "Vermont"       "Virginia"   
[3] "West Virginia"
> strsplit(v,"/")
[[1]]
[1] "hi how are you"

> strsplit(v,"      ")
[[1]]
[1] "hi how are you"

> strsplit(V1,"      ")
[[1]]
[1] "just"

> strsplit(V1,"/")
[[1]]
[1] "just"

> sub("a","k",vec)
[1] "rkmesh" "suresh" "nkresh" "gknesh"
> sub("s","z",vec)
[1] "ramezh" "zuresh" "narezh" "ganezh"
> gsub("s","z",vec)
[1] "ramezh" "zurezh" "narezh" "ganezh"

No comments:

Post a Comment