R Assignment 3-1
R Assignment 3-1
Assignment 3
1. Find all even numbers from
a)a numeric vector
b)logical vector
2. Write a R program to
a) create the given dataframe and
b) get the statistical summary and nature of the data of a given data frame
c) to extract first two rows from a given data frame.
d) to add new row(s) to an existing data frame
e) to drop column(s) by name from a given data frame.
3. Write a R program to create a list of dataframes and access each of those dataframes from the
list.
4. Write a R program to create a list containing a vector, a matrix and a list and give names to
the elements in the list.
a) Access the first and second element of the list.
b) add element at the end of the list.
c) remove the second element.
d) update the last element.
5. Write a R program to get the length of the first two vectors of a given list.
6. Write a R program to concatenate two given factor in a single factor.
7. Write a R program to find the levels of factor of a given vector.
8. Implement Binary search tree using R programming.
9. Write a R program to implement quick sort using recursion.
10. Print the following pattern using R programming.
1
22
333
4444
55555
11. Write a R program to check whether the given number is armstrong number or not.
12. Given a data frame and a vector, create a function that will add a the vector (if the vector
length match with the rows number of the data frame) as a new variable to the data frame.
Id=c(1:10)
Age=c(14,12,15,10,23,21,41,56,78,12)
Sex=c('F','M','M','F','M','F','M','M','F','M')
Code=letters[1:10]
df=data.frame(Id,Age,Sex,Code)
14.Create a function that, given a data frame and two indexes, exchanges two values of the
Code variable with each other.
For example, if the index is 1 and 3, you assign:
df[1,'Code']=df[3,'Code']
df[3,'Code']=df[1,'Code']
x,y integer
A=c(1:10)
B=seq(100,10,-10)
H=seq(-200,-50,along.with=B)
df=data.frame(A,B,H)
Create a function that given a data frame df calculate a new variable ‘SUM_x_y'(If x=2 and
y=3, then the new variable will be ‘SUM_2_3’,
if x=4 and y=10, then the new variable will be ‘SUM_4_10’),such that for each row ‘i’ is
equal to:
sum(x*df[1:i,1])+sum(y*df[1:i,2])
16. Create a function that given a numeric vector, sort this in ascending order and duplicate it
by two.
17. Create a function that given a numeric vector X returns the digits 0 to 9 that are not in X.
If X=0 2 4 8
the function return 1 3 5 6 7 9
18.Create a function that given two strings (one word each), check if one is an anagram of
another.
19. Create a function that given one word, return the position of word’s letters
on letters vector.
For example, if the word is ‘abc’, the function will return 1 2 3.
20. Create a function that given a string
return a matrix
[,1] [,2]
[1,] "NAME" " Maria "
[2,] "COUNTRY" "uruguay "
[3,] "EMAIL" " [email protected]"
ST=c('NAME:Maria /COUNTRY:uruguay
/EMAIL:[email protected]','NAME:Paul/COUNTRY:UK
/EMAIL:[email protected]',
[2,] "COUNTRY" "uruguay " "UK " "USA " "Spain "