R Example Problems
I've listed a few problems that you might want to work through to
exercise your R skills. Note that this is not a homework and is not
graded. You are free to ignore it if you wish
- Write a function called
insert(vec, value, pos) where
vec is a vector, value is a single scalar (number,
boolean or string) and
pos is the position in the vector at which the value should
be inserted. The function should report an error is an invalid
position is specified
- Read in the file data2.csv and then
perform count the number of rows that have yes in the
columned called y. Do the same thing for no and maybe
- Look up the table function and redo the above
question - it should become a one line solution
- Using the above dataset, generate a data.frame where any row
that has a value greater than 50 in the z column, will have
the value -1 in the x colum
- Since the y column in the above dataset has 3 levels,
yes, no and maybe, determine the mean value of
the z column for each group. That is, the mean value of
z for all rows that have a yes in their y
column and so on
- You can do the above question by manually subsetting the
data.frame three times. Look up the by function and see if
you can rewrite the problem using it - it will be much shorter!
- Make a cumulative plot of x. To do this, you need to
calculate the following elements: x[1], x[1]+x[2],
x[1]+x[2]+x[3] and so on. The last element will be the sum of
all the rows of x. (The R function cumsum will do this
for you, but it's instructive to write it yourself)
Last modified: Sat Feb 23 11:46:33 EST 2008