Professional Documents
Culture Documents
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)
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)
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.
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.
17.
19.
x <- 1:10
length(x)
20.
21.
22.
23.
z <- matrix(1:6,ncol=2,byrow=T)
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.
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
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))
33. Functions: Functions take in inputs, execute commands, and return output.
35.
36.
37.
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))
41.
emp.newdata
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.
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.
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")
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.
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.
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.
59.
60.
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