Sorry, that I didn't wrote the sum function in latex. I tried $$ but that didn't work...
I am a beginner in R and I want to sum up:
Sum from i=0 to n by 1.054^i
I reserached the sum() function in R. However, it seems to me that is only can just add elements and not include an index or something.
So my question is: Can I solve that with a simple sum function or do I have to use a for loop for example?
I really appreciate your answer!
UPDATE
Here is a link to my sum(sorry that I cannot post it, but I need more reputation :(link )
In R most operations are vectorized, it requires changing the mindset a bit from other languages and for this question the answer is rather than looping, you simply do the entire operation on your sequence of numbers "at once":
sum(1.054^(0:n))
Related
I'm unsure how to do this. If possible a walkthrough on this would be greatly appreciated! Just started a class that uses R and find somethings to be very complicated.
Thanks!
I have tried to just write simple types of loops but not sure how to tie in all of the functions I need to do.
I have to solve a problem with permutations. The function takes vector a with n elements as a parameter. I declare b as #variable - there should be the permutation 1:n that gives the best result after finding the solution of a problem.
The error appears when I want to create #constraint. I have to use a[b[1]], so it takes the first element from vector which is a variable. It gives my error, that I can't use type VariableRef as a index of an array. But how can I get around this when I have to use it?
I sounds as if you have two optimisation problems one of which is an integer programming problem. You might think about separating the two.
(Sorry for not writing a comment, my reputation is still too low ;-) )
everyone
I brought some new trivial questions to ask. I've searched, but I failed to look for the most efficient answer.
The string(or list) I would like to make looks like:
c("who","expr1","who","expr2",...,"who","expr100")
How can I efficiently make this string using loop(or another function) in R?
Thank you~!!
At least this is one choice:
c(rbind("who", paste0("expr", 1:100)))
I am trying to translate some matlab code to R.
This is the code:
v_i = vfull(:,i:ns:k*ns);
I think this means take every ns_th column from i to k*ns in the matrix v_i, right? If I am wrong, can someone please tell me what it means?
Regardless, how would one implement this in R?
You interpretation of what Matlab is doing is correct. Here is how to do the equivalent in R
v_i = vfull[ ,seq(i,(k*ns),ns)]
I have been trying to produce a command in R that allows me to produce a new vector where each row is the sum of 25 rows from a previous vector.
I've tried making a function to do this, this allows me to produce a result for one data point.
I shall put where I haver got to; I realise this is probably a fairly basic question but it is one I have been struggling with... any help would be greatly appreciated;
example<-c(1;200)
fun.1<-function(x)
{sum(x[1:25])}
checklist<-sapply(check,FUN=fun.1)
This then supplies me with a vector of length 200 where all values are NA.
Can anybody help at all?
Your example is a bit noisy (e.g., c(1;200) has no meaning, probably you want 1:200 there, or, if you would like to have a list of lists then something like rep, there is no check variable, it should have been example, etc.).
Here's the code what I think you need probably (as far as I was able to understand it):
x <- rep(list(1:200), 5)
f <- function(y) {y[1:20]}
sapply(x, f)
Next time please be more specific, try out the code you post as an example before submitting a question.