# some R exercises

4 messages
Open this post in threaded view
|
Report Content as Inappropriate

## some R exercises

 This post was updated on . Hi! I tried everything to avoid asking somebody but I have absolutely no clue how to solve this tasks. I'll be really glad if someone could explain that to me... 1. Well, the first part is of course no problem but I have really trouble with the tasks i) and ii). just don't know how to write the function and why I should use rexp... 2. Oh I love loops. Well - "I think" I have to solve the first task to solve this... Well there is more but I'll tried to fight with it a few more hours. Thanks for helping!
Open this post in threaded view
|
Report Content as Inappropriate

## Re: some R exercises

 On Exercise 5, how about this (I'm rather new to R as well, so no guarantee this is right and I'm sure there are more efficient ways to do this!): fun1 <- function(x) {   y <- 0   for (i in 1:length(x))       {y[i] <- (x[i]-mean(x))^2}   sum(y)/(length(x)+1) } fun2 <- function(x) {   y <- 0   for (i in 1:length(x))   {y[i] <- (x[i]-mean(x))^2}   sum(y)/(length(x)) } x <- rexp(15) fun1(x) fun2(x)
Open this post in threaded view
|
Report Content as Inappropriate

## Re: some R exercises

 Just thought about it, you can have this of course "cheaper" without the loops. Like this: fun1 <- function(x) sum((x-mean(x))^2)/(length(x)+1) fun2 <- function(x) sum((x-mean(x))^2)/(length(x)) x <- rexp(15) fun1(x) fun2(x)