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
Related
This question already has answers here:
How to get the sum of each four rows of a matrix in R
(3 answers)
Average of n rows
(1 answer)
Closed 8 months ago.
I have a column with hundreds of values, I want to get the average for each 4 rows, then I move to another four rows, etc. How could I write that loop in R studio, I attached here a simple example, what should I do if I have NA values, how to consider those values as zeros?
This question already has answers here:
R keep rows with at least one column greater than value
(3 answers)
Delete rows in R if a cell contains a value larger than x
(1 answer)
Closed 2 years ago.
I have a matrix, and I want to remove all rows that contain at least one element less than a value, 3 on this example. Sample data:
A=matrix(c(10,2,4,8,5,4,8,10,5),byrow=T,ncol=3)
#Remove rows that contain at least one value less than 3
final_matrix=matrix(c(8,5,4,8,10,5), byrow=T,ncol=3)
How to get to the final matrix from the initial matrix A? My real matrix contains thousands of rows tens of columns, this is a toy example. I tried A=A[A>3,] but I get an error "logical subscript too long"
This question already has answers here:
Dividing a vector into categories
(1 answer)
How to create a categorical variable in R with unequal categoris [closed]
(1 answer)
R if else with for loop [duplicate]
(3 answers)
Closed 5 years ago.
I'm programming in R.I need to divided a vector into x partitions(eg,x=4),and get the partition number of the vector, something like this...
a <- data.frame(x=1:20)
a$numofpartition<-ifelse(a$x<6,1,ifelse(a$x<11,2,ifelse(a$x<16,3,4)))
As the code,I divided a$x into 4 partitions,and get the partition number for each x, any functions in R could do this? Thank you!
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))
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))