How to sum elements by intervals? - r

I am wondering how I can use dplyr (or other methods) to sum intervals of elements of a vector?
Lets say I have the vector: v = rep(2,800).
I want to get a new vector with the sums of intervals of 16 elements, having the content like this:
Vsum <- c(sum(v[1:16]), sum(v[17:32]), ..., sum(v[785:800]) )
length(Vsum)
[1] 50
NB! What I have tried myself:
sixteen <- seq(1,800,16)
sixteen_end <- sixteen + 15
sum(test[seksten:seksten_slutt])
[1] 32
But it only sum the first interval (1:16) and not for the rest of vector v.

You can use matrix with colSums:
colSums(matrix(v, 16))

Related

Sum up the differences between every element of a vector and a given threshold

I have the following vector:
my_vec <- c(2,3,5,3,5,2,6,7,2,4,6,8)
threshold <- 4
Is there a way to sum up the differences of all smaller elements of my_vec compared to the threshold value?
So the expected result on this example should be 8 (2+1+0+1+0+2+0+0+2+0+0+0)
For my purpose, the sum (8) is all I need (I don't need the difference between every element). I tried this by using a loop but unfortunately, there are several vectors of different length so I can't loop from 1:12 (as on the above vector) on a vector which has only 10 elements.
First subset elements below threshold and then sum difference to threshold:
threshold <- 4
sum((threshold - my_vec[my_vec < threshold]))
# [1] 8
You can use pmin between my_vec and threshold to get minimum of them and get sum of it's differences with threshold.
sum(threshold - pmin(my_vec, threshold))
#[1] 8

Calculate product of all following vector elements in R

I have some arbitrary vector such as
v <- seq(1,5)
For every index of v I now need to compute the product of all following elements of v.
Here, the result would be a vector w=(5*4*3*2,5*4*3,5*4,5,1) but I need a general algorithm for this. I am trying to avoid a loop (which is the obvious solution).
You can use cumprod with rev:
c(rev(cumprod(rev(v[-1]))), 1)
#[1] 120 60 20 5 1

Multiply certain elements of a vector in R

I have a vector [1:360] with integers and need to find the products of the first, second ... twelfth set of 30 elements. Ultimately, I need a function that gives me a vector [1:12] with the products of all twelve 30-element intervals.
I'm fairly new to R and have been stuck on this for too long.
A simple way to do this would be to turn your vector into a 30-row matrix and get the product of each column.
In the absence of a reproducible example, let's make one with a vector of 360 numbers drawn from a normal distribution:
set.seed(69)
vec <- rnorm(360)
We can turn vec into a 30 * 12 matrix by just doing matrix(vec, nrow = 30), which will fill the matrix by column. We then get the product of each column by using apply to apply the function prob to each column.
apply(matrix(vec, nrow = 30), 2, prod)
#> [1] -6.253460e-09 -4.413086e-09 -1.332389e-10 1.041448e-08 -1.779489e-08 1.255979e-10
#> [7] 3.463687e-13 -6.265196e-12 8.300651e-04 -1.041469e-10 4.256378e-09 1.439522e-09

create an incidence matrix with restrictions in r (i.graph)

I would like to create a (N*M)-Incidence Matrix for a bipartite graph (N=M=200).
However, the following restrictions have to be considered:
Each column i ( 1 , ... , 200 ) has a column sum of g = 10
each row has a Row sum of h = 10
no multiedges (The values in the incidence Matrix only take on the values [0:1]
So far I have
M <- 200; # number of rows
N <- 200; # number of colums
g <- 10
I <- matrix(sample(0:1, M*N, repl=T, prob= c(1-g/N,g/N)), M, N);
Does anybody has a solution?
Here's one way to do what you want. First the algorithm idea, then its implementation in R.
Two step Algorithm Idea
You want a matrix of 0's and 1's, with each row adding up to be 10, and each column adding up to be 10.
Step 1: First,create a trivial solution as follows:
The first 10 rows have 1's for the first 10 elements, then 190 zeros.
The second set of ten rows have 1's from the 11th to the 20th element and so on.
In other words, a feasible solution is to have a 200x200 matrix of all 0's, with dense matrices of 10x10 1's embedded diagonally, 20 times.
Step 2: Shuffle entire rows and entire columns.
In this shuffle, the rowSum and columnSums are maintained.
Implementation in R
I use a smaller matrix of 16x16 to demonstrate. In this case, let's say we want each row and each column to add up to 4. (This colsum has to be integer divisible of the larger square matrix dimension.)
n <- 4 #size of the smaller square
i <- c(1,1,1,1) #dense matrix of 1's
z <- c(0,0,0,0) #dense matrix of 0's
#create a feasible solution to start with:
m <- matrix(c(rep(c(i,z,z,z),n),
rep(c(z,i,z,z),n),
rep(c(z,z,i,z),n),
rep(c(z,z,z,i),n)), 16,16)
#shuffle (Run the two lines following as many times as you like)
m <- m[sample(16), ] #shuffle rows
m <- m[ ,sample(16)] #shuffle columns
#verify that the sum conditions are not violated
colSums(m); rowSums(m)
#solution
print(m)
Hope that helps you move forward with your bipartite igraph.

Convert a one column matrix to n x c matrix

I have a (nxc+n+c) by 1 matrix. And I want to deselect the last n+c rows and convert the rest into a nxc matrix. Below is what I've tried, but it returns a matrix with every element the same in one row. I'm not sure why is this. Could someone help me out please?
tmp=x[1:n*c,]
Membership <- matrix(tmp, nrow=n, ncol=c)
You have a vector x of length n*c + n + c, when you do the extract, you put a comma in your code.
You should do tmp=x[1:(n*c)].
Notice the importance of parenthesis, since if you do tmp=x[1:n*c], it will take the range from 1 to n, multiply it by c - giving a new range and then extract based on this new range.
For example, you want to avoid:
(1:100)[1:5*5]
[1] 5 10 15 20 25
You can also do without messing up your head with indexing:
matrix(head(x, n*c), ncol=c)

Resources