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

R Studio Question and Answers

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)
12 views

R Studio Question and Answers

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/ 6

R Code

R console/ Out put


> #1.i. Calculate the average sales for items Yoga mat and Soccer ball separately.
>
> #Yoga mat
> Yoga_mat=subset(Sales, Item=="Yoga mat")
> mean(Yoga_mat$TotalSales)
[1] 566.6667
>
> #Soccer ball
> Soccer_ball=subset(Sales, Item=="Soccer ball")
> mean(Soccer_ball$TotalSales)
[1] 409.044
>
> #1. ii. Categorize the TotalSales variable as Poor Sales (TotalSales less than 300),
> #Steady Sales (TotalSales between 300(inclusive) and 600) and Strong Sales otherwise
> #into a new column named “SalesStatus”.
>
> SalesStatus=c()
> for (i in 1:length(TotalSales)) {
+ if (TotalSales[i]<300.00){
+ SalesStatus[i]="Poor Sales"
+ } else if (TotalSales[i]>=300 & TotalSales[i]<600.00){
+ SalesStatus[i]="Steady Sales"
+ } else {
+ SalesStatus[i]="Strong Sales"
+ }
+ }
>
> Sales=cbind(Sales, SalesStatus)
>
> #2.Create three vectors “Pulse1”, “Pulse2”, and “Smokes” using the following data
>
> Pulse1=c(98,87,88,79,76,83,92,95,81,77)
> Pulse2=c(140,120,100,130,120,115,135,125,110,105)
> Smokers=c(2,1,1,2,1,2,2,1,2,1)
>
> #2. i. Use these three data vectors to create a data frame with the name “DataPulse”
>
> DataPulse=data.frame(Pulse1,Pulse2,Smokers)
>
> #2. ii. Sort the data frame by “Pulse1” column. Name the sorted table as “SortPulse”.
>
> SortPulse=DataPulse[order(DataPulse$Pulse1),]
>
> #2. iii. Plot the “Pulse1” versus “Pulse2”. Interpret your results by fitting a line
>
> plot(Pulse1,Pulse2)
> line1=lm(Pulse2 ~ Pulse1, data=DataPulse)
> abline(line1, col="red")
>
> #2. iv. Modify the data frame, “DataPulse” by adding a new column named “sex” with the
> #following values
>
> sex=c("F","M","M","F","M","F","F","F","M","M")
> DataPulse=cbind(DataPulse, sex)
>
> #2. v. Find the minimum and maximum value of “Pulse1” for males and females separately.
>
> #females
> females=subset(DataPulse, sex=="F")
> min(DataPulse$Pulse1)
[1] 76
> max(DataPulse$Pulse1)
[1] 98
>
> #males
> males=subset(DataPulse, sex=="M")
> min(DataPulse$Pulse1)
[1] 76
> max(DataPulse$Pulse1)
[1] 98
>
> #2. vi. Find the tabular summary of the variable “Gender” with the percentage values
and the
> #names of the categories.
>
> tab2=prop.table(table(DataPulse$sex))*100
>
> #2. v. Code the values in the column “Smokes” as given below. 1=YES, 2=NO
>
> Smokes=c()
> for (i in 1:nrow(DataPulse)) {
+ if (Smokers[i]==1){
+ Smokes[i]="YES"
+ } else {
+ Smokes[i]="NO"
+ }
+ }
>
> DataPulse=cbind(DataPulse,Smokes)
>
> #3. i. Generate 100 random numbers from a normal distribution with mean 5 and
variance 64.
> #Name the vector with the name “RN”
>
> #standard devition^2= variance
> #standard deviation= 8
> RN=c(rnorm(100, mean = 5, sd = 8))
>
> #3. ii. Take a sample of 10 data elements from the vector “RN” and name that vector
as
> #“RNSample”
>
> RNSample=sample(RN,10)
>
> #3. iii. Generate a sequence with the following parameters.
> #First value= 10
> #No. of elements =10
> #Increment between two consecutive terms =10
> #Name the sequence with the name “seql”
>
> seql= seq(from = 10,
+ by = 10,
+ length.out = 10)
>
> #3. iv. Combine the two vectors “RNSample” and “seql” to create a data frame.
Name the data
> #frame as “DataSet1”
>
> DataSet1=data.frame(cbind(
+ RNSample,seql
+ ))

>
Data frames and vectors

Question 2. iii.
Interpretation:
The lines show an upward trend, meaning pulse 1 is somewhat directly proportional to pulse 2. If
Pulse1 increases Pulse2 will also increase. The slope of the line is positive due to the positive
correlation between the two variables Pulse 1 and Pulse 2.

You might also like