
Untitled
By: a guest on
Apr 13th, 2012 | syntax:
R | size: 1.81 KB | hits: 16 | expires: Never
#Evgueni Neiman
#Math 452
#Homework No. 3
### Question 1 ###
x=rpois(20,4); k1=rpois(20,5); k2=rpois(10,5);
names(x)=letters[k1]; out=letters[k2];
i = 1;
new_x = NULL;
while(i <= length(x))
{
j = 1;
found = FALSE
while(j <= length(out))
{
if(names(x[i]) == out[j])
{
found = TRUE;
j = 100000;
}
j = j + 1;
}
if(found == FALSE)
{
new_x = c(new_x,x[i]);
}
i = i + 1;
}
print(x); print(out); print(new_x);
#I am sure there is a simpler function to find out but this
#should make sense...
### Question 2 ###
##function matrix_repeat takes in a matrix as its first argument
###and a vector as its second argument
rep_mat = function(x,w)
{
row = dim(x)[1]; #get dimensions
col = dim(x)[2];
len = length(w);
i = 1;
while(i <= len)
{
temp = matrix(rep(x[i,],each=w[i]),nrow=w[i]);
new_x = rbind(new_x,temp);
i = i + 1;
}
return(new_x)
rm(new_x)
}
## to test this out:
X = matrix(1:9,3,3)
z = c(1,2,3)
rep_mat(X,z);
### Question 3 ###
box = function(x)
{
q1 = quantile(x,.25);
q2 = median(x);
q3 = quantile(x,.75);
xmax = max(x);
xmin = min(x);
IQR = q3 - q1;
lh = q1 - 3*IQR;
uh = q3 + 3*IQR;
xmin_h=min(x[x>lh]);
xmax_h=max(x[x<uh]);
xlim = c(0,4);
ylim = c(xmin,xmax);
plot(1,1,xlim,ylim,type="n");
#print(q1);
rect(1,q1,3,q3);
lines(c(2,2),c(q1,xmin_h),lty=2)
lines(c(1.5,2.5),c(xmin_h,xmin_h))
lines(c(1,3),c(q2,q2))
lines(c(2,2),c(q3,xmax_h), lty=2)
lines(c(1.5,2.5),c(xmax_h,xmax_h))
if(any(x>xmax_h)) points(2,x[x>xmax_h]);
if(any(x<xmin_h)) points(2,x[x<xmin_h]);
#now to test this out
x=rpois(100,5)
box(x);
}