Calculating cohesive blocks cohesion igraph 0.5.4 - r

Is there a more or less direct way to estimate the cohesion for each cohesive block (i.e. the result of cohesive.blocks()) in igraph 0.5.4?
In the actual version (0.6) there is a function called cohesion(), but in version 0.5.x there is not. Is there an easier way to calculate it or should I just do it individually for each block (by hand!!)?

This is actually in the documentation, even in the example:
g <- graph.disjoint.union(graph.full(4), graph.empty(2,directed=FALSE))
g <- add.edges(g,c(3,4,4,5,4,2))
g <- graph.disjoint.union(g,g,g)
g <- add.edges(g,c(0,6,1,7,0,12,4,0,4,1))
## Find cohesive blocks:
gBlocks <- cohesive.blocks(g)
## Examine block membership and cohesion:
gBlocks$blocks
# [[1]]
# [1] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# [[2]]
# [1] 12 13 14 15 16
# [[3]]
# [1] 0 1 2 3 4 6 7 8 9 10
# [[4]]
# [1] 12 13 14 15
# [[5]]
# [1] 0 1 2 3 4
# [[6]]
# [1] 6 7 8 9
gBlocks$block.cohesion
# [1] 1 2 2 3 4 3

Related

am trying to generating grouping of numbers using sequences of numbers and slicing using for loop in R language. help me how to generate?

I am trying to generating grouping of numbers using sequences of numbers and slicing using a for-loop. My input is
s1 = seq(1,285, 5)
s2 = seq(5, 285, 5)
for (k in 1:57))
{
print(s1:s2)
}
But I am getting this output
[1] 1 2 3 4 5
[1] 1 2 3 4 5
[1] 1 2 3 4 5
[1] 1 2 3 4 5
instead of my expected
[1] 1 2 3 4 5
[1] 6 7 8 9 10
[1] 11 12 13 14 15
[1] 16 17 18 19 20
you shoud use k in for body
for (k in 1:57){
print(s1[k]:s2[k])
}

How can I split large dataset iteratively to get smaller datasets by rows

I have a dataset that has a column with days spanning from 1-182. I want to split this dataset into smaller 30 days data frames. However, I want the data frames to form as follows:
Dataframe 1: Day 1 - Day 30 (Row 1-30)
Dataframe 2: Day 2 - Day 31 (Row 2-31)
Dataframe 3: Day 3 - Day 33 (Row 3-32) and so on.
I already know how to split by 30 days but can't find a way to split like this! Please let me know how I can do this with some function in R
Here is my take on what you asked for.
dat <- data.frame(jday = 1:182,
value = rnorm(182, 10, 1))
# window interval
windx <- 30
# iterate up until you run out of rows
res <- lapply(1:(nrow(dat) - windx), function(i) {
dat[i:(i + (windx-1)),]
})
# 152 data.frames
length(res)
#> [1] 152
# 30 rows
nrow(res[[1]])
#> [1] 30
# look at first 6 values from first 6 data.frames
lapply(head(res), head)
#> [[1]]
#> jday value
#> 1 1 13.062751
#> 2 2 9.468940
#> 3 3 9.371270
#> 4 4 11.477544
#> 5 5 11.072019
#> 6 6 9.598129
#>
#> [[2]]
#> jday value
#> 2 2 9.468940
#> 3 3 9.371270
#> 4 4 11.477544
#> 5 5 11.072019
#> 6 6 9.598129
#> 7 7 9.349836
#>
#> [[3]]
#> jday value
#> 3 3 9.371270
#> 4 4 11.477544
#> 5 5 11.072019
#> 6 6 9.598129
#> 7 7 9.349836
#> 8 8 10.149530
#>
#> [[4]]
#> jday value
#> 4 4 11.477544
#> 5 5 11.072019
#> 6 6 9.598129
#> 7 7 9.349836
#> 8 8 10.149530
#> 9 9 9.521323
#>
#> [[5]]
#> jday value
#> 5 5 11.072019
#> 6 6 9.598129
#> 7 7 9.349836
#> 8 8 10.149530
#> 9 9 9.521323
#> 10 10 9.726165
#>
#> [[6]]
#> jday value
#> 6 6 9.598129
#> 7 7 9.349836
#> 8 8 10.149530
#> 9 9 9.521323
#> 10 10 9.726165
#> 11 11 8.876201
# all data.frames are 30 rows long
all(unlist(lapply(res, nrow) == 30))
#> [1] TRUE
Created on 2020-12-03 by the reprex package (v0.3.0)
Assuming you have a data.frame like this:
set.seed(1)
d <- data.frame(matrix(sample(20, 30, TRUE), ncol = 3))
# X1 X2 X3
# 1 6 5 19
# 2 8 4 5
# 3 12 14 14
# 4 19 8 3
# 5 5 16 6
# 6 18 10 8
# 7 19 15 1
# 8 14 20 8
# 9 13 8 18
# 10 2 16 7
... create a matrix that identifies the rows of interest. Here, I'm interested in every three rows, thus 1-3, 2-4, 3-5, ... , 8-10. Change "3" to 30 for your case.
m <- embed(1:nrow(d), 3)
m
# [,1] [,2] [,3]
# [1,] 3 2 1
# [2,] 4 3 2
# [3,] 5 4 3
# [4,] 6 5 4
# [5,] 7 6 5
# [6,] 8 7 6
# [7,] 9 8 7
# [8,] 10 9 8
Once you have those, use lapply across the indices to extract the relevant rows.
lapply(1:nrow(m), function(x) d[rev(m[x, ]), ])
# [[1]]
# X1 X2 X3
# 1 6 5 19
# 2 8 4 5
# 3 12 14 14
#
# [[2]]
# X1 X2 X3
# 2 8 4 5
# 3 12 14 14
# 4 19 8 3
#
# [[3]]
# X1 X2 X3
# 3 12 14 14
...
...
# [[7]]
# X1 X2 X3
# 7 19 15 1
# 8 14 20 8
# 9 13 8 18
#
# [[8]]
# X1 X2 X3
# 8 14 20 8
# 9 13 8 18
# 10 2 16 7
The result is a list of your data.frames. You can use list2env if you really want to have all the subsets as separate data.frames in your workspace.

How can you generate multiple vectors of different length in R?

I would like to use a for loop to generate multiple vectors and save their values for later use. The end result ideally would be:
vector_1 = c(1)
vector_2 = c(1,2,3)
vector_3 = c(1,2,3,4,5,6)
.
.
.
vector_i = c(1,2,3,...,n) #for some n generated during the loop. This n does not always have an upper bound.
This is so that I can use each vector later on to plot multiple lines on the same graph with the axis of the graph scaled correctly.
The following code is the best example I can come up with to try and describe the idea but obviously using 'vector_i' for each loop is not going to work.
for (i in 1:n){
length = sample(1:i^2,1)
vector_i = seq(1,length)
}
You could use the following function:
make_vectors <- function(n) lapply(seq(n), function(i) seq(sample(i^2, 1)))
Which allows:
vector <- make_vectors(5)
vector
#> [[1]]
#> [1] 1
#>
#> [[2]]
#> [1] 1 2 3 4
#>
#> [[3]]
#> [1] 1 2 3 4
#>
#> [[4]]
#> [1] 1 2 3 4 5 6
#>
#> [[5]]
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
And you can access each one like this:
vector[[5]]
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
This keeps your global environment tidy and allows you to iterate through your vectors more easily than writing them all as independent entities.
We could use sequence
v1 <- sequence(c(1, 3, 6))
v1
#[1] 1 1 2 3 1 2 3 4 5 6
If we need it in a list
split(v1, cumsum(v1 == 1))
#$`1`
#[1] 1
#$`2`
#[1] 1 2 3
#$`3`
#[1] 1 2 3 4 5 6

how to do the systematic permutation in R?

I have a vector including 9 observations and set to 3 treatments. I need to do the systematic permutation,not 9!. It is 9!/(2!3!4!)=1260. first 9 choose 2,then 7 choose 3, rest obs will in treatment 3.
I have already write the code for 2 parts,but I can not figure out how to list all the possible outcomes. Since there are some observations are replicate. the permutation are not quite right. I need to assign the Id for each observation first. I have some problem about how to assign the id and permute id first, then return the real observations.
list is my R code.there are some mistakes. I need a help to complete this program. Thank you!
for (k in 1260) {
allperm<-c(A,B,C)
A <- combn(complete, 2)
for (i in 1:36){
complete_B <- complete[!(complete %in% A[,i])]
B <- combn(complete_B, 3)
for (j in 1:35){
C <- complete_B[!(complete_B %in% c(B[,j], A[,i]))]
}
}
}
here is a base R solution to obtain all permutations allPerms as well as the partitions A,B and C
v <- 1:9
allPerms <- lapply(sapply(combn(v,2,simplify = FALSE),
function(p) combn(v[-p],3,FUN = function(k) c(p,k),simplify = FALSE)),
function(k) c(k,v[-k]))
A <- lapply(allPerms, `[`,1:2)
B <- lapply(allPerms, `[`,3:5)
C <- lapply(allPerms, `[`,6:9)
such that
> head(allPerms)
[[1]]
[1] 1 2 3 4 5 6 7 8 9
[[2]]
[1] 1 2 3 4 6 5 7 8 9
[[3]]
[1] 1 2 3 4 7 5 6 8 9
[[4]]
[1] 1 2 3 4 8 5 6 7 9
[[5]]
[1] 1 2 3 4 9 5 6 7 8
[[6]]
[1] 1 2 3 5 6 4 7 8 9
> head(A)
[[1]]
[1] 1 2
[[2]]
[1] 1 2
[[3]]
[1] 1 2
[[4]]
[1] 1 2
[[5]]
[1] 1 2
[[6]]
[1] 1 2
> head(B)
[[1]]
[1] 3 4 5
[[2]]
[1] 3 4 6
[[3]]
[1] 3 4 7
[[4]]
[1] 3 4 8
[[5]]
[1] 3 4 9
[[6]]
[1] 3 5 6
> head(C)
[[1]]
[1] 6 7 8 9
[[2]]
[1] 5 7 8 9
[[3]]
[1] 5 6 8 9
[[4]]
[1] 5 6 7 9
[[5]]
[1] 5 6 7 8
[[6]]
[1] 4 7 8 9
If you want outputs in the format of matrix, you can try the code below
v <- 1:9
allPerms <- sapply(sapply(combn(v,2,simplify = FALSE),
function(p) combn(v[-p],3,FUN = function(k) c(p,k),simplify = FALSE)),
function(k) c(k,v[-k]))
A <- allPerms[1:2,]
B <- allPerms[3:5,]
C <- allPerms[6:9,]

Picking a variable in R

I have two variables: X and state which are given below
set.seed(3)
state <- rbinom(15,4,0.6)
X <- c(1:15)
X
state
and the output is
> state
[1] 3 2 3 3 2 2 4 3 2 2 2 2 2 2 1
> X
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
I want to select the Xs corresponding to the same state. Any idea how to do this in R?
Using split you get a list of 4 states
ll <- split(X,state)
$`1`
[1] 15
$`2`
[1] 2 5 6 9 10 11 12 13 14
$`3`
[1] 1 3 4 8
$`4`
[1] 7
ll[3]
$`3`
[1] 1 3 4 8
generally we use , ave to perform some operations while grouping.
For example here I get the mean of X by state:
ave(X,state,FUN = mean)
[1] 4.000000 9.111111 4.000000 4.000000 9.111111 9.111111 7.000000 4.000000 9.111111 9.111111 9.111111 9.111111 9.111111 9.111111 15.000000
Another way could be to put you variables in a data frame and then select them from there:
> df <- data.frame(x = X, state = state)
> df
x state
1 1 3
2 2 2
3 3 3
4 4 3
5 5 2
6 6 2
7 7 4
8 8 3
9 9 2
10 10 2
11 11 2
12 12 2
13 13 2
14 14 2
15 15 1
> df[df$state == 3,]
x state
1 1 3
3 3 3
4 4 3
8 8 3

Resources