Estimating parameters using a mixture of normal distributions 
I'm currently working on a statistics assignment using R. I am having a bit of trouble with questions 1 and 2.
For 1, my code is:
loglike = function(theta, x) {

  pi = theta[1]
  mu = theta[2]
  llh = NULL

  for (i in x) {
    f1 = dnorm(i, 0, 1)
    f2 = dnorm(i, mu, 1)
    llh = c(llh, log(pi*f1 + (1-pi)*f2))
  }
  return(-sum(llh))
}
}

Is this a correct way of approaching the problem? It's taking in a probability, the population mean, and a vector of observations as parameters and then calculating the log-likelihood.
For 2, my code so far is:
set.seed(1480)
group = sample(c(1,2), 100, replace=TRUE, prob=c(.25,.75))
rn1 = rnorm(100,0,1)
rn2 = rnorm(100,1,1)
x = rn1*(group==1) + rn2*(group==2)

I don't understand the second part of the question, where it is asking to plot the log-likelihood against the population mean. How would I go about doing this?
Thanks in advance. I'm a novice in this field, so this is all relatively new to me.
 A: I would like to add an aspect that might seem off-topic, but that should be taken into account because the assignment specifically asks for R code.
You should also utilize the vectorization of the buitlin functons. "Vectorization" means that the functions are overloaded to take scalars or vectors as input: in case of vectors, the function returns the vector of function values:
$$f(\vec{x}) := \left( f(x_1), \ldots, f(x_n)\right)$$
As R is an interpreted language, for loops are painfully slow and should be replaced by vectorized function calls, if possible. In your cae, you can directly write
sum( log( pi*dnorm(x) + (1-pi)*dnorm(x, mu, 1) ) )

Both log and dnorm are vectorized, ie.e. work with a vector as first argument. As a nice side-effect, vectorized functions automatically make use of parallelization (provided that R has been built with parallelization support), which leeds to further speedup.
A: (1) in the log-likelihood function, you won't generate new random numbers. Instead, you'll use your observations to calculate the log-likelihood:
$$\log \mathcal L(\mu,\pi|\mathcal X)=\sum_{x\in \mathcal X} \log f(x_i|\mu,\pi)$$
i.e. just substitution. Once you have it, in the second part, you'll use this function (call it loglik instead of mixture for a better name) with $\pi=0.25$, your dataset and various values of $\mu$ to plot it wrt $\mu$.
