Thursday 15 October 2020

Introduction to R programming using R studio-Beginner Basics

R is case sensitive
# is comment
(Rstudion Code menu->comment/Uncomment option)

To clear Rstudio console
Ctrl+L

To install package MASS
install.packages("MASS")

Alt hypen to get <- in R

to create a vector of 3 elements    
sample <- c(2,1,5.8)
captaincy <- data.frame(namesVector, YVector, playedVector)
1. create dataframe
2. access parts in dataframe
digits=2
mean(captaincy$y,ratio)
write.csv(captaincy,"Newcap.csv",row.names=FALSE)

newCap = read.csv("/../..1.csv")
View(newCap)

inbuilt dataset CO2

View(CO2)
> mean(CO2$conc)
[1] 435
> median(CO2$uptake)
[1] 28.3

> plot(CO2$Plant,CO2$uptake,type='l')


To check the packages installed in R type
library()

To load a package
library(MASS)

ctrl+shift+n to create new R script
ctrl+shift+enter in scipt window to run


Highlight a code and press ctrl+Enter to run only the selected part

sum(matrixA)
colSums(matrixA)
rowSums(matrixA)

To add new row in dataframe
captaincy <- rbind(captaincy, data.frame(name="kohli",Y=2016, played=30,won =20, lost =9,victory=20/30)

cbind is used to add new column to data frame

defeat=captaincy$lost/captaincy$played
captaincy<-cbind(captaincy,defeat)

CaptaincyOne <- read.csv("c.csv")
class(CaptaincyOne)
typeof(CaptaincyOne)  #to get to know about type of data
help(typeof)  #to know more about type of


head(CaptaincyOne,2) # top 2 rows will be displayed
tail(CaptaincyOne,2) # last 2 rows will be displayed
str(CaptaincyOne,2) # structural information  will be displayed


To merge two data frames
c<-merge(CaptaincyOne,CaptaincyTwo, by="names")

To read notepad file
t=read.table("one.txt")

Commonly used R objects are
1. vector
2. Lists
3. Matrices
4. Factors
5. Data Frames

Atomic vector types
logic
integer
numeric
complex 
character

Factor are often referred as categorical variables
Factors have levels

u can convert int to factors
f<factor(fa)

To display levels of factor variable f
levels(f)

as.matrix will convert into matrix

List can contain mixed data type elements
atomic vector has similar data type elements

my<-c(1:5)

list elements can be accesssed using index
mylist[[2]],[,3]  2nd row 3rd column

mylist[][3] indicates all rows 3rd column

to merge two lists l1 and l2
merge.list <- c(l1,l2)

To plot histogram
data <- read.csv("C:/Users/SARVANI/Desktop/rProjects/moviesData.csv")
dim(data)
hist(data$runtime, main = "Distibution of movies",xlab="runtime", xlim=c(0,300),col="blue"   )


to plot pie chart
g <- table(data$genre)
pie(g)

to plot barchart
barplot


2 comments: