Documente Academic
Documente Profesional
Documente Cultură
X**6 #exponential
class(X) #to know the type of data
mean(X) = 3.5 #lets try this
#concatenation
y="hello"
x="bml"
z=paste(y,x,sep=" ") #you can not directly concatenate two variable by just using
+ like python.
pos = regexpr('m', z) # to find the position of the character in the string
keep = substr(z, 1, 7)
sub("hello","hi", z) #you can replace any text any time
upper=toupper(z) #converting to upper
z=tolower(upper) #converting to lower
a=c(2,3,4)
b=c("hello","hi","bml")
c=vector("logical",length=4)
a1=as.factor(a)
b1=as.factor(b)
Sys.Date()
x=Sys.Date()+5
y=123456
y1=as.Date(y)
x1=as.numeric(x)
x2=as.Date(x1, "%d%b%Y")
my_matrix[2,]
my_matrix[0,]
my_matrix[2,3]
#dataframe
#Control structure in R
x =5
#check if statement
if (x+ 5 > 40){
print("There will be rain tomorrow!")
} else {
print ("There will be no rain tomorrow")
}
#for loop
k<- c(10,20,30,40,50,60)
x=10
#check if age is less than 17
while(x <= 10){
print(x)
x=x+2 }
#subset function
df1=subset(df,df$score<70)
#apply function
attach(iris)
iris1=iris
iris2=iris1[,-5]
iris_mean=apply(iris2,mean,2) #you can also allow some user defined function as
well
#write a user defined function
clean_comment<-function(x)
{
#replacing / with or
x<-gsub('/', ' or ',x)
#replacing negation terms with full expansion
x <-gsub("won't", "will not", x)
x=gsub("can't","can not",x)
x <-gsub("n't", "not", x)
return(x)
}
#sampling data
#take a random sample of size 50 from iris
# sample without replacement
mysample <- iris[sample(1:nrow(iris), 50,
replace=FALSE),]
#to fixed randomness you need to put seed
set.seed(1234)