0% found this document useful (0 votes)
33 views3 pages

PA R Fundamentals Solution

The document contains solutions to 5 problems involving data manipulation and analysis using R. 1) It creates two vectors, one with values from 2 to 1024 and another with letters and numbers, using utility functions. 2) It extracts city names from address strings using regular expressions and string splitting. 3) It identifies prime numbers between 48-100 by checking divisibility against a list of known primes. 4) It lists car names from a dataset that have automatic transmission, more than 3 gears, and below average mileage. 5) It defines a function to calculate the mode of a character vector, and tests it on sample vectors.

Uploaded by

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

PA R Fundamentals Solution

The document contains solutions to 5 problems involving data manipulation and analysis using R. 1) It creates two vectors, one with values from 2 to 1024 and another with letters and numbers, using utility functions. 2) It extracts city names from address strings using regular expressions and string splitting. 3) It identifies prime numbers between 48-100 by checking divisibility against a list of known primes. 4) It lists car names from a dataset that have automatic transmission, more than 3 gears, and below average mileage. 5) It defines a function to calculate the mode of a character vector, and tests it on sample vectors.

Uploaded by

Priyanka Rana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1 Use various utility functions discussed to create vectors containing these values.

You need to create both


[first containing 10 values and another containing 26] vectors separately.
Solution:

2^(1:10)

## [1] 2 4 8 16 32 64 128 256 512 1024

paste0(letters,26:1)

## [1] "a26" "b25" "c24" "d23" "e22" "f21" "g20" "h19" "i18" "j17" "k16"
## [12] "l15" "m14" "n13" "o12" "p11" "q10" "r9" "s8" "t7" "u6" "v5"
## [23] "w4" "x3" "y2" "z1"

2 Below given is a vector containing first lines of many addresses. Extract city names from all the addresses
using string functions. Hint: you can use a for loop to iterate over results of strsplit for further processing.

address_list=c("802/hiranandani/Mumbai",
"2A/kalka-Delhi",
"345#near adyar#Chennai",
"10-shyaam bazzar-Kolkata")

Solution:

temp=gsub("-","/",address_list)
temp=gsub("#","/",temp)
l=strsplit(temp,"/")

for(i in 1:4){
print(l[[i]][3])
}

## [1] "Mumbai"
## [1] "Delhi"
## [1] "Chennai"
## [1] "Kolkata"

3 Use following bit to create a vector with prime numbers in 1:47. [Prime numbers are numbers which are
divisible only by themselves.]

primes=c(2,3,5,7,11,13,17,19,23,29,31,37,41,43,47)

Any number from 48 to 100 which is not divisible by any of the above listed primes is also a prime. Print
those prime numbers from 48 to 100.
Solution:

1
for(i in 48:100){
temp=i%%primes
condition=temp==0
if(sum(condition)==0){print(i)}
}

## [1] 53
## [1] 59
## [1] 61
## [1] 67
## [1] 71
## [1] 73
## [1] 79
## [1] 83
## [1] 89
## [1] 97

4 Find out , how many cars are there are in the dataset mtcars which have automatic transmission, number
of forward gears higher than 3 and below average mileage.List their names. [ calculate average mileage from
the data itself]. To find out which variable in the data represent mentioned above information do ?mtcars
Solution:

?mtcars
avg=mean(mtcars$mpg)
d=mtcars[mtcars$mpg<avg & mtcars$gear>3 & mtcars$am==0,]
rownames(d)

## [1] "Merc 280" "Merc 280C"

5 There is no native function in R to calculate mode for a variable. The function “mode” returns storage
mode of an object, not the statistical mode that we discussed in the class.
write a function which returns modes of a character vector. Test that on the following vectors

set.seed(2)
x=sample(letters[1:5],50,replace=T)
y=sample(letters[1:3],50,replace=T)

Solution:

mymode=function(x){
t=table(x)
result=names(t)[which(t==max(t))]
return(result)
}
mymode(x)

## [1] "e"

2
mymode(y)

## [1] "a"

Prepared By : Lalit Sachan


Contact : [email protected]

You might also like