Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

1.

x <- 15
x -1

2.

print('Hello Word!')
## [1] "Hello Word!"

3.

x <- 4
y <- 5
z <- x+y
print(z)

4.

x <- 4
y <- 5
cat('The sum of x and y is', x+y)

5.

x <- True
x

6.

x <- TRUE
x

7.

class(x)

8.
x <- 1L
x

9.

x <- 2.6
x

class(x)

10.

msg <- c("Hello", "World")


msg
paste("hello","world")

Vector

11.

x <- 1:5
x

12.

x <- rep(0, 5)
x

13.

x <- seq(4,10,by=2)
x

14.

Numeric Vector
x <- c(1, -1, 3.5, 2)
x

15.

x <- c("Apple", "Banana")


x
16.

x <- c("Apple", "Banana")


x <- c(x, "Grapes")

17.

x <- c(1, -1, 3.5, 2)

x + 2 #every element plus 2

18. Vector naming starts with 1

x <- c(1, -1, 3.5, 2)

x[3] #Pick out the 3rd element

19.

x <- 1:10

length(x)

20.

M = matrix( c('a','a','b','c','b','a'), nrow = 2, ncol = 3, byrow = TRUE)


print(M)

21.

x<- matrix(1:20, nrow=5, ncol=4, byrow=FALSE)

22.

x[2,] #the second row

23.

x[,1] #the first column

x[1,2] #first row, second column


24.

z <- matrix(1:6,ncol=2,byrow=T)

length(z) # total number of elements

ncol(z) # total number of columns

nrow(z) # total number of rows

25. Array behaves like matrix but it is multi-dimensional.

x<- array(1:12, c(2,3,2))

26.Dataframe is most useful form of data type in R. It behaves like matrix but can
contain strings (letters/words)

df <- data.frame(c(1,2),

c("Good", "Bad"))
df

27.

df <- data.frame(GPA = c(1,2),

outcomes = c("Good", "Bad"))


df
28.

x <- c(2,4)

y <- c(1,3)
df <- data.frame(x,y)
df

29.

df <- data.frame(x=c(2,4),y=c(1,3))

df

30.

df$x #vector x

df[1] #1st column

31.

df$x <- NULL #To remove particular vector from dataframe, simplify assume
NULL to it.

32.

df <- data.frame(x=c(2,4),y=c(1,3))

df$z <- df$x + df$y


df

33. Functions: Functions take in inputs, execute commands, and return output.

Hello<- function (...) {


cat("Hello World")
}
Hello()
34.

myadd <- function (x, y) {


return(x+y)
}
myadd(3, 4)

35.

myadd <- function (x, y=1) {


return(x+y)
}
myadd(3)

36.

myadd <- function (x, y) {


return(x+y)
}
myadd(1:3, 3:5)

37.

result <- myadd(3, 4)


result

38.
gender <- c(rep("male",20), rep("female", 30))
gender <- factor(gender)
gender
summary(gender)

39.

data <-
c("East","West","East","North","North","East","West","West","West","
East","North")
print (data)
print(is.factor(data))
factor_data <- factor(data)
print(factor_data)
print(is.factor(factor_data))
40.
emp.data <- data.frame(emp_id = c (1:5),emp_name =
c("Rick","Dan","Michelle","Ryan","Gary"),
salary = c(623.3,515.2,611.0,729.0,843.25),
start_date = as.Date(c("2012-01-01", "2013-09-23", "2014-11-15",
"2014-05-11", "2015-03-27")),
stringsAsFactors = FALSE)
print(emp.data)

str(emp.data)

print(summary(emp.data))

result <- data.frame(emp.data$emp_name,emp.data$salary)


print(result)

41.

emp.newdata <- data.frame(


emp_id = c (6:8),
emp_name = c("Rasmi","Pranab","Tusar"),
salary = c(578.0,722.5,632.8),
start_date = as.Date(c("2013-05-21","2013-07-30","2014-06-17")),
stringsAsFactors = FALSE)

emp.newdata

emp.finaldata <- rbind(emp.data,emp.newdata)


print(emp.finaldata)

42.

x <- 1
y <- 2
if(x < y){print("Yes!")}
43.

x <- 1
y <- 2
if(x<y){
print("Yes!")
} else{
print("No~")
}

44.

n <- 10
i <- 1
sum <- 0
while (i <= n){ # i is the control variable
sum <- sum + i # accumulate i into sum
i <- i + 1 # increment i by 1
}

sum

45.

students <- c("Amy", "Tom")


for (student in students){
cat(student,"\n")
}

46.

# Create a vector.
x <- c(12,7,3,4.2,18,2,54,-21,8,-5)

# Find Mean.
result.mean <- mean(x)
print(result.mean)

47.

# Create the vector.


x <- c(12,7,3,4.2,18,2,54,-21,8,-5)

# Find the median.


median.result <- median(x)
print(median.result)
48.

x <- 30L
if(is.integer(x)) {
print("X is an Integer")
}

49.

x <- c("what","is","truth")

if("Truth" %in% x)
{
print("Truth is found")
}
else
{
print("Truth is not found")
}

50.

x <- c("what","is","truth")

if("Truth" %in% x){


print("Truth is found")
} else
{
print("Truth is not found")
}

51.
x <- c("what","is","truth")

if("Truth" %in% x) {
print("Truth is found the first time")
} else if ("truth" %in% x) {
print("truth is found the second time")
} else {
print("No truth found")
}
52.
x <- switch(
3,
"first",
"second",
"third",
"fourth"
)
print(x)

53.

 H is a vector or matrix containing numeric values used in bar chart.


 xlab is the label for x axis.
 ylab is the label for y axis.
 main is the title of the bar chart.
 names.arg is a vector of names appearing under each bar.
 col is used to give colors to the bars in the graph.

# Create the data for the chart


H <- c(7,12,28,3,41)

# Give the chart file a name


png(file = "barchart1.png")

# Plot the bar chart


barplot(H)

# Save the file


dev.off()
54.

v is a vector containing numeric values used in histogram.


main indicates title of the chart.
col is used to set color of the bars.
border is used to set border color of each bar.
xlab is used to give description of x-axis.
xlim is used to specify the range of values on the x-axis.
ylim is used to specify the range of values on the y-axis.
breaks is used to mention the width of each bar.

# Create data for the graph.


v <- c(9,13,21,8,36,22,12,41,31,33,19)

# Give the chart file a name.


png(file = "histogram.png")

# Create the histogram.


hist(v,xlab = "Weight",col = "yellow",border = "blue")

# Save the file.


dev.off()

55.
sales_total<-c(100,200,300,500,99,1000,3000,560,-980,50,90,8000)
sales_group<-vector(mode="character",length=length(sales_total))
sales_group[sales_total<100]<-"small"
sales_group[sales_total>=100 & sales_total<500]<-"medium"
sales_group[sales_total>=500]<-"high"
spender<-factor(sales_group,levels =c("small","medium","high"))
print(spender)

Output:

[1] medium medium medium high small high high high small small
[11] small high
Levels: small medium high

56.
The mode is the value that has highest number of occurrences in a set of data.
R does not have a standard in-built function to calculate mode.

# Create the function.


getmode <- function(v) {
uniqv <- unique(v)
uniqv[which.max(tabulate(match(v, uniqv)))]
}

# Create the vector with numbers.


v <- c(2,1,2,3,1,2,3,4,1,5,5,3,2,3)

# Calculate the mode using the user function.


result <- getmode(v)
print(result)

57.

List is the most comprehensive data type. It can contain anything: vector, array,
matrix and even dataframe.

x <- c(2, 3, 5)
df <- data.frame(y=c(2,3,4),z=c(1,3,5))
name <- c("NUS", "NTU", "SMU")
x <- list(x,df,name)

x[[1]]
58.

# Get and print current working directory.


print(getwd())

# Set current working directory.


setwd("/web/com")

# Get and print current working directory.


print(getwd())

data <- read.csv("input.csv")


print(data)

# Create a data frame.


data <- read.csv("input.csv")

# Get the max salary from data frame.


sal <- max(data$salary)
print(sal)

59.

# Create a data frame.


data <- read.csv("input.csv")
retval <- subset(data, as.Date(start_date) > as.Date("2014-01-01"))

# Write filtered data into a new file.


write.csv(retval,"output.csv")
newdata <- read.csv("output.csv")
print(newdata)

60.
data()

AirPassengers Monthly Airline Passenger Numbers 1949-1960


BJsales Sales Data with Leading Indicator
BJsales.lead (BJsales)
Sales Data with Leading Indicator
BOD Biochemical Oxygen Demand
CO2 Carbon Dioxide Uptake in Grass Plants
ChickWeight Weight versus age of chicks on different diets
DNase Elisa assay of DNase
EuStockMarkets Daily Closing Prices of Major European Stock
Indices, 1991-1998
Formaldehyde Determination of Formaldehyde
HairEyeColor Hair and Eye Color of Statistics Students
Harman23.cor Harman Example 2.3
Harman74.cor Harman Example 7.4
Indometh Pharmacokinetics of Indomethacin
InsectSprays Effectiveness of Insect Sprays
JohnsonJohnson Quarterly Earnings per Johnson & Johnson Share
LakeHuron Level of Lake Huron 1875-1972
LifeCycleSavings Intercountry Life-Cycle Savings Data
Loblolly Growth of Loblolly pine trees
Nile Flow of the River Nile
Orange Growth of Orange Trees
OrchardSprays Potency of Orchard Sprays
PlantGrowth Results from an Experiment on Plant Growth
Puromycin Reaction Velocity of an Enzymatic Reaction
Seatbelts Road Casualties in Great Britain 1969-84
Theoph Pharmacokinetics of Theophylline
Titanic Survival of passengers on the Titanic
ToothGrowth The Effect of Vitamin C on Tooth Growth in
Guinea Pigs
UCBAdmissions Student Admissions at UC Berkeley
UKDriverDeaths Road Casualties in Great Britain 1969-84
UKgas UK Quarterly Gas Consumption
USAccDeaths Accidental Deaths in the US 1973-1978
USArrests Violent Crime Rates by US State
USJudgeRatings Lawyers' Ratings of State Judges in the US
Superior Court
USPersonalExpenditure Personal Expenditure Data
UScitiesD Distances Between European Cities and Between
US Cities
VADeaths Death Rates in Virginia (1940)
WWWusage Internet Usage per Minute
WorldPhones The World's Telephones
ability.cov Ability and Intelligence Tests
airmiles Passenger Miles on Commercial US Airlines,
1937-1960
airquality New York Air Quality Measurements
anscombe Anscombe's Quartet of 'Identical' Simple Linear
Regressions
attenu The Joyner-Boore Attenuation Data
attitude The Chatterjee-Price Attitude Data
austres Quarterly Time Series of the Number of
Australian Residents
beaver1 (beavers) Body Temperature Series of Two Beavers
beaver2 (beavers) Body Temperature Series of Two Beavers
cars Speed and Stopping Distances of Cars
chickwts Chicken Weights by Feed Type
co2 Mauna Loa Atmospheric CO2 Concentration
crimtab Student's 3000 Criminals Data
discoveries Yearly Numbers of Important Discoveries
esoph Smoking, Alcohol and (O)esophageal Cancer
euro Conversion Rates of Euro Currencies
euro.cross (euro) Conversion Rates of Euro Currencies
eurodist Distances Between European Cities and Between
US Cities
faithful Old Faithful Geyser Data
fdeaths (UKLungDeaths)
Monthly Deaths from Lung Diseases in the UK
freeny Freeny's Revenue Data
-
-
-
-
-

> data(iris)
> summary(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width
Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100
1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300
Median :5.800 Median :3.000 Median :4.350 Median :1.300
Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
Species
setosa :50
versicolor:50
virginica :50

> plot(iris)

output picture2

R Programming basics

https://bookdown.org/kochiuyu/Technical-Analysis-with-R/using-rstduio.html

2. To run R code online


https://rdrr.io/snippets/

You might also like