0% found this document useful (0 votes)
5 views6 pages

R Programming Data Analytics Lab Guide

Uploaded by

madhesh.k011105
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

R Programming Data Analytics Lab Guide

Uploaded by

madhesh.k011105
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Data Analytics using R Programming Lab

1. Temperature Conversion (F ↔ C) Program

choice <- readline("FtoC or CtoF? ")


t <- [Link](readline("Enter Temperature: "))

if(choice=="FtoC")
print(paste("Celsius =", (t-32)*5/9))
else
print(paste("Fahrenheit =", t*9/5+32))

Output

FtoC or CtoF? FtoC


Enter Temperature: 98
[1] "Celsius = 36.6667"

2. Area of Shapes Program

shape <- readline("Shape (rect/sq/circ/tri): ")

if(shape=="rect"){
l <- [Link](readline("Length: "))
b <- [Link](readline("Breadth: "))
print(l*b)
}

if(shape=="sq"){
s <- [Link](readline("Side: "))
print(s*s)
}

if(shape=="circ"){
r <- [Link](readline("Radius: "))
print(3.14*r*r)
}

if(shape=="tri"){
b <- [Link](readline("Base: "))
h <- [Link](readline("Height: "))
print(0.5*b*h)
}
Output
Shape (rect/sq/circ/tri): circ
Radius: 5
[1] 78.5

3. Even numbers from 1 to n Program

n <- [Link](readline("Enter n: "))


for(i in 1:n) if(i%%2==0) print(i)

Output

Enter n: 10
[1] 2
[1] 4
[1] 6
[1] 8
[1] 10

4. Function to print squares Program

sq <- function(n){
for(i in 1:n) print(i^2)
}

n <- [Link](readline("Enter n: "))


sq(n)

Output

Enter n: 5
[1] 1
[1] 4
[1] 9
[1] 16
[1] 25

5. Using cbind() and rbind() Program

a <- [Link](x=1:3, y=4:6)


b <- [Link](z=7:9)

print(cbind(a,b))
print(rbind(a,a))
Output

xyz
1147
2258
3369

xy
114
225
336
414
525
636

6. String Manipulation Program

s <- readline("Enter string: ")

print(nchar(s))
print(toupper(s))
print(tolower(s))
print(substr(s,1,3))
print(paste("Hello", s))

Output

Enter string: Rprogramming


[1] 12
[1] "RPROGRAMMING"
[1] "rprogramming"
[1] "Rpr"
[1] "Hello Rprogramming"

7. Data Structures (Vector, List, Data Frame) Program

v <- c(1,2,3)
l <- list(a=1, b="hello", c=TRUE)
d <- [Link](id=1:3, name=c("A","B","C"))

print(v)
print(l)
print(d)
Output

[1] 1 2 3
$a
[1] 1

$b
[1] "hello"

$c
[1] TRUE

id name
1 1 A
2 2 B
3 3 C

8. Read CSV and Analyze Program

data <- [Link]("[Link]")


print(head(data))
print(summary(data))
print(str(data))

Output (example)
id score
1 1 45
2 2 78

summary:
id score
Min. :1 Min. :45
Max. :2 Max. :78

'[Link]': 2 obs. of 2 variables:


$ id : int 1 2
$ score: int 45 78
9. Pie Chart & Bar Chart Program

x <- c(10,20,30)
labels <- c("A","B","C")

pie(x, labels)
barplot(x, [Link]=labels)

Output

Graphs will be displayed (pie + bar chart).

10. Statistical Analysis of Dataset Program

d <- c(5,7,8,10,15)
print(mean(d))
print(median(d))
print(sd(d))
print(summary(d))

Output

[1] 9
[1] 8
[1] 3.847077
Min. 1st Qu. Median Mean 3rd Qu. Max.
5 7 8 9 10 15

11. Recursive Factorial Program

fact <- function(n){


if(n==0) return(1)
return(n*fact(n-1))
}

n <- [Link](readline("Enter number: "))


print(fact(n))

Output

Enter number: 5
[1] 120
12. Count Even & Odd Numbers Program

n <- [Link](readline("How many numbers? "))


arr <- numeric(n)

for(i in 1:n){
arr[i] <- [Link](readline(paste("Enter number", i, ": ")))
}

ev <- sum(arr%%2==0)
od <- sum(arr%%2==1)

cat("Even =", ev, " Odd =", od)

Output

How many numbers? 5


Enter number 1: 2
Enter number 2: 5
Enter number 3: 8
Enter number 4: 3
Enter number 5: 10
Even = 3 Odd = 2

You might also like