For loop function with two subscripts - r

I am new to writing loop functions and I am trying to solve this. I would like the y matrix to be populated with the values obtained from the for loop. Unfortunately y remains blank and full of 0's after the loop is executed.
mv <- c(0,1,2) # location vector
s <- 1 # scale
increment <- seq(-6,6,0.01) # Create a sequence of x values
y=matrix(0,length(increment),length(mv))
for (i in length(increment)) {
for (j in length(mv)) {
y[i,j] <- 1/(1+ exp(-(increment[i]-mv[j])/s))
}
}

Change your loop to start at 1, for now it is only using 1 value (length(increment)):
for (i in 1:length(increment)) {
for (j in 1:length(mv)) {
y[i,j] <- 1/(1+ exp(-(increment[i]-mv[j])/s))
}
}

Related

saving lists as elements of matrix

I am trying to add lists as an element of matrix. I am using the following code
wiip <- matrix(list(), 5, 5)
for (i in colnames(wave1)) {
for (j in colnames(wave1)) {
wiip[[i,j]] <- gclo(wave1[[i]], wave1[[j]])
}
}
the wiip[[i,j]] says subscripts out of bounds.
the output from the function gclo is a list and I want to save all the outputs. that is the double loop creates i*j(i runs over 1 to 5 and so does j, hence total 25 lists objects are created) number of lists. how to save each list together in one object.
Try to save the result in a list and use [, i] and [,j] to subset each column of the matrix.
wiip <- vector('list', ncol(wave1)*ncol(wave1))
ind <- 0
for (i in colnames(wave1)) {
for (j in colnames(wave1)) {
ind <- ind + 1
wiip[[ind]] <- gclo(wave1[, i], wave1[, j])
}
}

why my sum() function don't work for the matrix operations in R

The following is my R code, I want the tx[1, 1] = 1*2^1 + 4*5^2 + 7*8^3, tx[1, 2] = 4*5 + 7*8^2, tx[1, 3] = 7*8,but it only including 7*8^3 for tx[1, 1] and 7*8^2 for tx[1, 2]. The other elements in the tx matrix only include the last term in the sum operation too. So,could anyone tell me how to rectify my code to have a correct output? Thanks in advance!
A <- matrix(c(1:9),ncol=3)
B <- matrix(c(2:10),ncol=3)
tx<-matrix(rep(NA,3*3),3,3)
for(j in 1:3)
{
for(i in 1:3)
{
for(k in 1:3-i+1)
{
tx[j,i]<-sum(A[j,k+i-1]*(B[j,k+i-1])^k)
}
}
}
Right now it's not summing the results of each loop, just saving out whatever the last loop result happens to be. Try this instead:
A <- matrix(c(1:9),ncol=3)
B <- matrix(c(2:10),ncol=3)
tx<-matrix(rep(0,3*3),3,3)
for(j in 1:3)
{
for(i in 1:3)
{
for(k in 1:3-i+1)
{
tx[j,i]<-tx[j,i] + (A[j,k+i-1]*(B[j,k+i-1])^k)
}
}
}
I initialized the tx matrix to start at 0 and added the result of each loop to the existing total, so that it increments over each loop.

Limit the values used in a function to their sum

I created a function with that I want to calculate (several) vectors of data.
Actually the vctors should be for a range (1:100) for one variable while the others stay constant at differnt values in turn.
The function is:EI <- function(x,y,z) {(x+y)/(2*(2*x+y)+z)}
my Problem is, the sum of x+y+z has to be limited to 100. And i don't know how to tell it the function.
For example, if x = 20, y can only take values from 0:80 , i.e. (100-20), and z can takey values from 0 : 100-(x+y).
I used the following code where z is not regarded all. I thought, I would get at least one large vector, but all I get is a single number:
for(x in 1:100) {
for(y in 0:(100-x)) {
for(z in 0:(100-(x+y))) {
v1 <- c(EI(x,y,z))
}
}
}
I need to tell the function EI() that the sum of x+y+z has alwys to be 100.
Has anybody an idea how to solve this problem?
If you want to create a vector you need to do that:
v1 <- c()
for(x in 1:100) {
for(y in 0:(100-x)) {
for(z in 0:(100-(x+y))) {
v1 <- c(v1, EI(x,y,z))
}
}
}
Unfortunately this will be slow (because at each step you are reallocating a new vector), a better alternative is to begin by allocate your vector with the appropriate size:
v1 <- numeric(171700)
k <- 0
for(x in 1:100) {
for(y in 0:(100-x)) {
for(z in 0:(100-(x+y))) {
k <- k + 1
v1[k] <- EI(x,y,z)
}
}
}
You can also write the same thing with sapply function and this is slightly faster:
v1 <- unlist(sapply(1:100,
function(x) {
unlist(sapply(0:(100-x),
function(y) {
sapply(0:(100-(x+y)),
function(z) {EI(x,y,z)}) }))}))

Putting each value in a table

I have an r code where I am printing the values and doing a small calculation on it.
for (j in sample.data1$SENTIMENT_STRENGTH_91D) {
i = (j - max(sample.data1$SENTIMENT_STRENGTH_91D))/C
}
this prints out each value of i. How can I put these values into a vector inside the loop?
I have tried this code but it only gives the last values
for (j in sample.data1$SENTIMENT_STRENGTH_91D) {
i = (j - max(sample.data1$SENTIMENT_STRENGTH_91D))/C
D <- c(i)
}
You can change your for loop to the following:
D = numeric(0)
for (j in sample.data1$SENTIMENT_STRENGTH_91D) {
i = (j - max(sample.data1$SENTIMENT_STRENGTH_91D))/C
D <- c(D, i)
}
But it turns out growing an object in a for loop in R is very slow. You probably don't even need a for loop. You can try taking advantage of R's vectorization:
x <- sample.data1$SENTIMENT_STRENGTH_91D
D <- (x - max(x))/C

Using If trying to get a vector a certain length

I'm trying to use if function to create a vector X that follows the pattern of the if statement, that is length 5. However, when I print X, I get 5 vectors with length 1. How do I fix this
for (i in 1:5) {
if (i <2){
a<-i
}
else {
a<-(i-1)
}
X<-a
print(X)
}
R overwrites the contents of your variables a and X with each loop. To avoid this, you can make X a list, and put your value in a different position with each loop.
X <- list()
a <- list()
for(i in 1:5) {
if(i<2){
a <- i
} else {
a <- i-1
}
X[i] <- a
}
Since your final plan is to create a vector, you may initialize a vector ("X") first, and then add a value ("a") in each 'for' loop.
X = vector("numeric",0)
for (i in 1:5){
if (i<2){
a <- i
}
else{
a <- (i-1)
}
X = c(X, a)
print(X)
}
X
# [1] 1 1 2 3 4

Resources