Вы находитесь на странице: 1из 3

# Import Excel data

pelican <- read.csv("Pelican.csv", TRUE, ",")


View(pelican)
class(pelican)
str(pelican)

# plot categorical

plot(pelican$Type.of.Customer)

# plot numerical

plot(pelican$Net.Sales)

# plot 2 x categorical
plot(pelican$Type.of.Customer, pelican$Method.of.Payment)

# plot histogram

hist(pelican$Net.Sales)

# box plot
boxplot(pelican$Net.Sales)

rm(list = ls())
--------------------------------------------------------------------------
plot(pelican$Age, pelican$Net.Sales, xlab = "Age", ylab = "Net Sales", main = "Age
vs Sales", col = "orange")

plot(pelican$Age, pelican$Net.Sales, col = "darkgreen")


plot(pelican$Age, pelican$Net.Sales, xlab = "Age", ylab = "Net Sales", main = "Age
vs Sales", col = "red4", col.main = "darkgray", cex.axis = 1.5, pch = 1)
salescalls <- c(20,40,20,30,10,10,20,20,20,30)
unitsold <- c(30,60,40,60,30,40,40,50,30,70)

######################################
plot(salescalls, unitsold)
lm(unitsold~salescalls)
abline(lm(unitsold ~ salescalls))

attach(mtcars)

plot(wt, mpg)
abline(lm(mpg~wt))
title("Reg")
detatch(mtcars)
----------------------------------------------------------------
A1 <- matrix(1:10, nrow = 5, byrow = TRUE)
nrow(A)
ncol(A)
dim(A)
# Check if elements are equal

A == B

# Matrix Multiplication (Transpose)

A %*% t(B)
rownames(A) <- c("First", "Second", "Third", "Fourth", "Fifth")
cbind(1:3, 4:6)
# Subsetting Matrix

j <- matrix(sample(1:15, 12), nrow = 3)


# subset multiple elements

j[2, c(2, 3)]


j[c(1, 2), c(2, 3)]
j[c(1, 3), c(1, 3, 4)]

# subset by names

rownames(j) <- c("r1", "r2", "r3")


----------------------
log(10)

exp(10)

abs(35)
sqrt(100)

ceiling(34.59)

floor(34.59)

round(34.59, 1)

trunc(34.59)
# Paste
i <-10
paste("i = ", i)

# To convert into uppercase or lowercase

toupper("hello")
sum1 <- function()
{
a <- 10
b <- 10
c <- a+b
print("Addition of two numbers")
print(c)
}
sum1()
------------------
x <- 0
if(x > 0)
{
print("Positive number")
}else if(x < 0)
{
print("Negative number")
}else
print("Zero")
n <- readline("Enter any number: ")
n <- as.integer(n)
# Check wether the no. is odd or even
if((n%%2) == 0)
---------------------------------------------------------------
#sum the rows
apply(theMatrix, 1, sum, na.rm=TRUE)
rowSums(theMatrix)
theList <- list(A = matrix(1:9, 3), B = 1:5, C = matrix(1:4,2), D=2)
require(ggplot2)
data(diamonds)
head(diamonds)

View(diamonds)
# Calculate the average price for each type of cut: Fair, Good, Very Good, Premium,
Ideal

aggregate(price ~ cut, diamonds, mean)

# Group the data by more than one variable: cut and color

aggregate(price ~ cut + color, diamonds, mean)

# To aggregate two variables: price and carat using cbind

aggregate(cbind(price, carat) ~ cut, diamonds, mean)

# Multiple variables can be supplied to both sides

aggregate(cbind(price, carat) ~ cut + color, diamonds, mean)

require(plyr)
aggregate(price ~ cut, diamonds, each(mean, median))

######### Correlation ###########

cor(diamonds$carat, diamonds$price)
round(cor(diamonds$carat, diamonds$price), 2)
cor.test(diamonds$carat, diamonds$price)
----------------------------------------------------
x <- sample(x = 1:100, size = 100, replace = TRUE)
# Weighted mean
grades <- c(90, 89, 78, 55)
weights <- c(1/2, 1/4, 1/8, 1/8)

mean(grades)

weighted.mean(x = grades, w = weights)


var(x)

sd(y)
summary(y)

# Calculate 25th and 75the percentile from vector x

quantile(x, probs = c(0.25, 0.75))

Вам также может понравиться