I'm new to R and I've got a question:
choice <- c(TRUE, FALSE, FALSE, FALSE)
rep(sample(choice, size = 4, replace=FALSE), times = n)
always repeats the same vector, e.g. (FALSE, TRUE, FALSE, FALSE)
However, I want to have n different random samples of the vector choice in a new vector (replace must be FALSE, because only 1 in 4 elements should be TRUE).
Which function should I choose? I'm not allowed to use for-loops.
You can use replicate. It returns a matrix, which you can then turn into a vector.
choice <- c(TRUE, FALSE, FALSE, FALSE)
n <- 3
set.seed(42) # for reproducibility
as.vector(replicate(n, sample(choice, size = 4, replace=FALSE)))
#[1] FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE
Related
Is there a function that will help me output all 2^n permutations of a boolean vector of length n? For instance, if i have a boolean vector of length n=2, c(FALSE,FALSE), i should obtain 2^2=4 permutations.
As such, I need a function, that will generalize this output for an array of length n,
that means if n=3, output should be of length 2^3 and so on...
I have already tried permutations from gtools package but this seems to be incorrect, or providing only a partial answer to say the least. This method does not generalize well and has given me errors for n>2 as well.
> permutations(2,2,c(TRUE,FALSE))
[,1] [,2]
[1,] FALSE TRUE
[2,] TRUE FALSE
Output should be:
FALSE, FALSE,
TRUE, TRUE,
FALSE, TRUE,
TRUE, FALSE
You where missing repeats.allowed=T :
gtools::permutations(2,2, c(T,F), repeats.allowed = T)
[,1] [,2]
[1,] FALSE FALSE
[2,] FALSE TRUE
[3,] TRUE FALSE
[4,] TRUE TRUE
You can make your custom function around permutations:
my_permute <- function(vect, n, repeats = TRUE) {
gtools::permutations(length(vect), n, vect, repeats.allowed = repeats)
}
my_permute(vect=c(T,F), n=2)
Example with more elements:
my_permute(letters[1:3], n=3)
You can use expand.grid,
expand.grid(c(TRUE, FALSE), c(TRUE, FALSE))
# Var1 Var2
#1 TRUE TRUE
#2 FALSE TRUE
#3 TRUE FALSE
#4 FALSE FALSE
You can use gtools package and the function permutations:
This is the source code:
library(gtools)
x <- c(TRUE, FALSE)
permutations(n=length(x),r=2,v=x,repeats.allowed=T)
I want to take a logical matrix and and all of the columns in the matrix together to create a vector. An example:
a = c(TRUE, TRUE, FALSE, TRUE, FALSE, FALSE)
A = matrix(a, nrow = 3, ncol = TRUE, byrow = TRUE)
I would like to produce
[1] TRUE FALSE FALSE
Currently I am doing this with
apply(A, 1, function(x) Reduce('&', x))
However, this goes very slow with the size of my input, and I was wondering if there was a more efficient way to do this.
It will be faster to use:
apply(A,1,all)
instead of a call to Reduce.
Also, the rowSums function is equivalent to apply(x,1,sum) but much faster, so it will probably be even faster to do:
rowSums(A)==ncol(A)
Instead of using Reduce within apply, an option would be to either convert the 'A' to data.frame and then do
Reduce(`&`, as.data.frame(A))
#[1] TRUE FALSE FALSE
Or split by col and then
Reduce(`&`, split(A, col(A)))
#[1] TRUE FALSE FALSE
Given logical vector x:
x <- c(FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, TRUE, FALSE,
FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, TRUE)
How to split x based on every FALSE/TRUE patterns? Of course, we can simply do the split based on TRUE/FALSE patterns using !x.
So the split would search for the patterns FALSE, FALSE, ..., FALSE , TRUE, TRUE, ..., TRUE until we reach again a FALSE. At which point, we stop. Said differently, we do the split every time we move from a TRUE to a FALSE.
Here is what I ended up with:
p <- which(diff(x)==-1)+1
split(x, cumsum(seq_along(x) %in% p))
So the output is rightly:
# $`0`
# [1] FALSE FALSE FALSE TRUE TRUE
# $`1`
# [1] FALSE FALSE TRUE
# $`2`
# [1] FALSE FALSE FALSE FALSE TRUE TRUE TRUE
# $`3`
# [1] FALSE TRUE
Any other solution to this problem? More efficient way to do this?
I'm new to R and I've got a question:
choice <- c(TRUE, FALSE, FALSE, FALSE)
rep(sample(choice, size = 4, replace=FALSE), times = n)
always repeats the same vector, e.g. (FALSE, TRUE, FALSE, FALSE)
However, I want to have n different random samples of the vector choice in a new vector (replace must be FALSE, because only 1 in 4 elements should be TRUE).
Which function should I choose? I'm not allowed to use for-loops.
You can use replicate. It returns a matrix, which you can then turn into a vector.
choice <- c(TRUE, FALSE, FALSE, FALSE)
n <- 3
set.seed(42) # for reproducibility
as.vector(replicate(n, sample(choice, size = 4, replace=FALSE)))
#[1] FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE
How to get index number in a Boolean vector? For instance, my vector looks like this:
vector = (TRUE TRUE FALSE TRUE FALSE TRUE FALSE FALSE FALSE TRUE TRUE FALSE)
How to get index number for all TRUEs? vector["TRUE"] doesn't work.
Try using the which function (type ?which):
> my.vec <- c(TRUE, FALSE, FALSE, TRUE)
> which(my.vec)
> [1] 1 4