Topic: Utilities

Topic Description:

Basic R utilities used in many packages and functions, such as date and string manipulations, statistical distributions, geometry of lines and distances. The R package date is required for the two data functions.



File: utilities/utilities.r

View File Source Download File No help file available

Function: left

Function Description: left

A standard left function. Returns the leftmost n characters of a string. If n<0, returns all except the rightmost n. If n==0, returns an empty string. Arguments can be vectors, but both must be the same length, or n can be a scalar.

Function Arguments:

ArgumentDefault Value
s
n

Function Source:

left=function(s,n)
{
if(length(s)==0) return(s)

result=s
if(length(n)==1) n=rep(n,length(s))

for(i in 1:length(n))
{
if(n[i]>0) result[i]=substr(s[i],1,n[i])
else if(n[i]<0)
{
len=nchar(s[i])
result[i]=substr(s[i],1,len+n[i])
}
else result[i]=''
}

return(result)
}