Randomly mixing elements in vector R [duplicate] - r

This question already has answers here:
How to randomize a vector
(2 answers)
Closed 6 years ago.
Supposing I have the vector
x <- c(1,2,3,4,5,6)
Is there any way I could randomly mix its elements?
Or create a vector which would have integer elements from 1 to 6 which do not repeat?

We need sample to do that
sample(x)
If it needs to be repeated, use the replicated
replicate(3, sample(x))

Related

R Command for replacing value [duplicate]

This question already has answers here:
Replace given value in vector
(8 answers)
Closed 4 years ago.
hi just learning R and looking at how to do this.
How do i change a value of "1" in vehicle vector where it should be filled with the proper value from vendor$shop vector. I'm trying to make a weighted adjacency matrix
vehicle_vector[vehicle_vector == 1] <- new_value

Repeating entries in match in R [duplicate]

This question already has answers here:
Find all positions of all matches of one vector of values in second vector
(3 answers)
Finding All Positions for Multiple Elements in a Vector
(7 answers)
Closed 4 years ago.
In the following example, there is repeating entries in the second vector. How do I make R to tell me the position of both appearances of 5, please?
match(5, c(1,2,9,5,3,6,7,4,5))
Use the which function:
which(c(1,2,9,5,3,6,7,4,5) == 5)

R- list of combination of 3 item sets from n items [duplicate]

This question already has answers here:
How to get all possible combinations of n number of data set?
(2 answers)
How to calculate combination and permutation in R?
(6 answers)
Closed 5 years ago.
I am getting a tough time in selecting the combinations of factors.
I have a vector as ("Ryan", "Leo", "Jack","Harry","Edd").
I want to get the list of of all combinations taken 3 of the names once.
I want to do it in R. Probably a resultant matrix will help me.
We can use combn
t(combn(vec1, 3))

The most efficient way to calculate numbero of unique values in vector [duplicate]

This question already has answers here:
How to count the number of unique values by group? [duplicate]
(1 answer)
Unique values in each of the columns of a data frame
(9 answers)
Closed 5 years ago.
Is there better way to compute this instead of:
length(unique(vector))
let's assume that we donno what class is the vector.
You can use
library(data.table)
vector <- c(1,2,2,2,3)
uniqueN(vector)

Randomly shuffle a grid of values? [duplicate]

This question already has answers here:
Randomly re-order (shuffle) rows of a matrix?
(2 answers)
Closed 9 years ago.
What tools are available to randomly shuffle a grid of values in R?
I know of none. The values can't be changed, I just need their order reshuffled. Indeed, the grid could be represented as a linear data set, in which case the question becomes: how do I randomly reshuffle a vector of values?
sample(x) gives a random permutation of x.
For example:
> sample(c(1,3,5,7,9))
[1] 5 7 1 3 9

Resources