0% found this document useful (0 votes)
8 views

R All Program

Uploaded by

charlie charles
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)
8 views

R All Program

Uploaded by

charlie charles
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
You are on page 1/ 10

1.Assign 10 values to a variable. Find its length, sum and mean.

# Create a vector with 10 values

my_vector <- c(10, 20, 30, 40, 50, 60, 70, 80, 90, 100)

# Find the length of the vector

vector_length <- length(my_vector)

print(paste("Length of the vector:", vector_length))

# Calculate the sum of the vector

vector_sum <- sum(my_vector)

print(paste("Sum of the vector:", vector_sum))

# Calculate the mean of the vector

vector_mean <- mean(my_vector)

print(paste("Mean of the vector:", vector_mean))

Output:

2.Using sequence function to generate the numbers, increment by 10


# Generate a sequence from 1 to 10 with a step of 10

sequence <- seq(from = 10, to = 100, by = 10)

# Print the sequence

print(sequence)

Output:

3.Install a package “insurance Data”

1.First method: run the below command in R studio

install.packages("insuranceData")

2.second method: Open tool option R studio then install packages

Tool – Install Packages - insuranceData

5. From this package, load the data SingaporeAuto


a. Hint: You may have to use a function data( ) or get(data( ))

library(insuranceData)

data("SingaporeAuto")
6.Find out the nature of the variables, check for NA’s, size and shape

Output:
8.Extract first 10 and 40 to 70 cases with first 5 and 11, 13, 15th variables

library(insuranceData)

data("SingaporeAuto")

data=SingaporeAuto

## Extracting the case

data[c(1:20,50:60),c(1:3,11,13,15)]

Output:
9.Create new variable based on the
a. variable Exp_weights
i. If Exp_weights is negative, then newvariable is Low else High
b. variable NCD
NCD New Variable
0 NIL
10 and 20 Level1
30 Level2
Else Level3

library(dplyr)

library(insuranceData)

data("SingaporeAuto")

data=SingaporeAuto
## create a new variable

data=data %>% mutate(EX=case_when(Exp_weights<0~"LOW",

TRUE~"HIGH"))

data=data %>% mutate(NCD_NEW=case_when(NCD==0~"NIL",

NCD==10&NCD==20~"LEVEL1",

NCD==30~"LEVEL2",

TRUE~"LEVEL3"))

View(data)

Output:

10. Create a subset that has only Clm_Count = 0 and has only numeric variables

library(dplyr)

library(insuranceData)

data("SingaporeAuto")

data=SingaporeAuto

## Creating the subnet


data1 = data %>% filter(Clm_Count==0) %>% select_if(is.numeric)

Output:

11.Find the counts of


a. SexInsured
b. VehicleType
c. SexInsured and VehicleType
d. VehicleType and SexInsured
e. Exp_weights < 0.75 and VehicleType

library(dplyr)
library(insuranceData)
data("SingaporeAuto")
##summary
data %>% group_by(SexInsured) %>% summarize(SI=n())
data %>% group_by(VehicleType) %>% summarize(VT=n())
data %>% group_by(VehicleType,SexInsured) %>% summarize(VS=n())
data %>% group_by(SexInsured,VehicleType) %>% summarize(VS=n())
data %>% group_by(Exp_weights<0.75,VehicleType) %>% summarize(EV=n())

Output:
12.Change name of the variables of your own choice

library(dplyr)
library(insuranceData)
data("SingaporeAuto")
## Rename
data=data %>% rename(New_PC = PC)

Output:
1&2. Simple way of first and second program

library(dplyr)

seq(10,200,by=10)

a = seq(10,200,by=10)

mean(a)

median(a)

sum(a)

length(a)

Output:

You might also like