Sunteți pe pagina 1din 5

X=6

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

To convert the class of a vector, you can use as. command.

a=c(2,3,4)
b=c("hello","hi","bml")
c=vector("logical",length=4)

#converting to different class

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")

install.packages("lubridate") #this is the way to install r packages


library(lubridate)
#List: A list is a special type of vector which contain elements of different data
types
my_list=list("a","b",5,4)
my_list[[2]]
#Note this the difference between R & Python is in indexing.Pytho starts from 0
index whereas R starts from index 1
my_list1=list("t","h",2)
#appending two lists
list_full=append(my_list1,my_list)
#structure of list full
str(list_full)
list_full[3] #find if the element in the list or not
#Matrices
my_matrix <- matrix(1:9, nrow=3, ncol=3)
dim(my_matrix) #dimension of the matrix
attributes(my_matrix)

#extracting various columns or row from matrix

my_matrix[2,]
my_matrix[0,]
my_matrix[2,3]
#dataframe

#it is taking the tabular data same as pandas in python.


df <- data.frame(name = c("ash","jane","paul","mark"), score = c(67,56,87,91))
#selecting a particular column only
df[,2] #by index number
df1=df[order(rank),] #by the variable name

#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)

#print the first 4 numbers of this vector


for(i in 1:4){
print (k[i])
}
#while loop

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)

#couple of useful functions


str(iris) #structure of the dataframe
nrow(iris) #number of observation of dataframe
ncol(iris) #number of columns in dataframe
names(iris) #names of the variables in the dataframe

S-ar putea să vă placă și