Related
tt<-c(3,2,3,5,3,5,5,4,3,1,5,2,1,5,4,1,3,5,3,3)
ff<-matrix(tt,nrow=5)
print(ff)
print(t(apply(ff,1,sort)))
I want to order the second row only by ascending order not all rows, but it always show me all rows.
ff[2, ] <- sort(ff[2, ])
ff
# [,1] [,2] [,3] [,4]
# [1,] 3 5 5 1
# [2,] 2 2 3 5
# [3,] 3 4 1 5
# [4,] 5 3 5 3
# [5,] 3 1 4 3
You can order assign the order to the second row only:
tt<-c(3,2,3,5,3,5,5,4,3,1,5,2,1,5,4,1,3,5,3,3)
ff<-matrix(tt,nrow=5)
ff[2, ] <- ff[2, ][order(ff[2, ])]
print(ff)
[,1] [,2] [,3] [,4]
[1,] 3 5 5 1
[2,] 2 2 3 5
[3,] 3 4 1 5
[4,] 5 3 5 3
[5,] 3 1 4 3
I tried with the following code
rbind(1, matrix(c(1,2,3,4,5,6,7,8,9,10), 5))
[,1] [,2]
[1,] 1 1
[2,] 1 6
[3,] 2 7
[4,] 3 8
[5,] 4 9
[6,] 5 10
but I wish to get output like below
[,1] [,2]
[1,] 1
[2,] 1 6
[3,] 2 7
[4,] 3 8
[5,] 4 9
[6,] 5 10
cbind a single vector with NA and then use rbind
rbind(cbind(1, NA),matrix(1:10, 5))
# [,1] [,2]
#[1,] 1 NA
#[2,] 1 6
#[3,] 2 7
#[4,] 3 8
#[5,] 4 9
#[6,] 5 10
For purposes of getting the exact output, we can do the following(see the note below):
noquote(rbind(c(1,""),matrix(c(1,2,3,4,5,6,7,8,9,10), 5)))
[,1] [,2]
[1,] 1
[2,] 1 6
[3,] 2 7
[4,] 3 8
[5,] 4 9
[6,] 5 10
NOTE
Using "" to introduce a blank will lead to coercion to character.
We could use as.numeric to have numerics but this would lead to NAs which has already been demonstrated.
Using NA instead of "" is more realistic and useful
Using R ... I have a list of tables.
# Example data
z <- list(cbind(c(1,2), c(3,4)), cbind(c(1,2), c(3,4,5,6)), cbind(c(1,2), c(1,2,3,4,5,6)), cbind(c(1,2), c(3,4)), cbind(c(1,2), c(3,4,5,6,9,4,5,6)))
z <- setNames(z, c("Ethnicity", "Country", "Age Band", "Marital Status", "Hair Color"))
z
$Ethnicity
[,1] [,2]
[1,] 1 3
[2,] 2 4
$Country
[,1] [,2]
[1,] 1 3
[2,] 2 4
[3,] 1 5
[4,] 2 6
$`Age Band`
[,1] [,2]
[1,] 1 1
[2,] 2 2
[3,] 1 3
[4,] 2 4
[5,] 1 5
[6,] 2 6
$`Marital Status`
[,1] [,2]
[1,] 1 3
[2,] 2 4
$`Hair Color`
[,1] [,2]
[1,] 1 3
[2,] 2 4
[3,] 1 5
[4,] 2 6
[5,] 1 9
[6,] 2 4
[7,] 1 5
[8,] 2 6
I would like to "collapse" (not sure if that is the right word) this list into one super table, as the column variables are the same for every table in the list. I would want the output to look something like that which I have written below... Is there any way to do this? I tried using do.call(rbind, z) but this didn't give me the proper output.
Ethnicity
[1,] 1 3
[2,] 2 4
Country
[1,] 1 3
[2,] 2 4
[3,] 1 5
[4,] 2 6
`Age Band`
[1,] 1 1
[2,] 2 2
[3,] 1 3
[4,] 2 4
[5,] 1 5
[6,] 2 6
`Marital Status`
[1,] 1 3
[2,] 2 4
`Hair Color`
[1,] 1 3
[2,] 2 4
[3,] 1 5
[4,] 2 6
[5,] 1 9
[6,] 2 4
[7,] 1 5
[8,] 2 6
This produces your desired output if I understand it correctly:
sink("output.txt")
for (i in seq_along(z)) {
cat(names(z)[i], '\n') # print out the header
write.table(z[[i]], row.names = FALSE, col.names = FALSE)
}
sink()
I open a connection to a text file with sink then loop over your list of tables and print each one out using write.table.
It produces the following output:
Ethnicity
1 3
2 4
Country
1 3
2 4
1 5
2 6
Age Band
1 1
2 2
1 3
2 4
1 5
2 6
...
I would like to create 1000 random lists of 1652 genes from a universe of 44.400 genes.
I decided to replace. I used the following instruction to create the random lists:
randomMatrix<-replicate(1000, sample(gene_list, 1652, replace = T))
The point is that in each list a gene is replicated. For my study, genes can be replicated between lists but not in each list. How can I impose not to replicate genes in each single list?
Thanks in advance
It should work with replace = FALSE:
randomMatrix<-replicate(1000, sample(gene_list, 1652, replace = FALSE))
This, of course, requires at least 1652 unique values in gene_list.
A reproducible example would be nice to illustrate your problem, since you didn't give us such example I just assume a List and made some replications
List <- list(c(2,1,3,4,5,6), c(1,4,5,7,0,6), c(2,4,7,9,3,1))
set.seed(001)
replicate(3, lapply(List, sample, 7, replace=TRUE), simplify = FALSE)
which produces
[[1]]
[[1]][[1]]
[1] 1 3 4 6 1 6 6
[[1]][[2]]
[1] 7 7 1 4 4 0 5
[[1]][[3]]
[1] 3 7 3 1 7 3 1
[[2]]
[[2]][[1]]
[1] 1 4 2 1 3 2 3
[[2]][[2]]
[1] 6 5 5 7 5 4 0
[[2]][[3]]
[1] 3 3 2 3 7 3 9
[[3]]
[[3]][[1]]
[1] 5 4 4 5 2 3 5
[[3]][[2]]
[1] 0 5 6 5 4 1 1
[[3]][[3]]
[1] 4 9 9 7 1 4 7
Note that this approach will give you a resampled data (with replacement) for each element of your original list, that's why each replication is a list consisting in three elements each one.
If you write sapply instead of lapply inside replicate(...) the resulting output would be nicer.
set.seed(001)
replicate(3, sapply(List, sample, 7, replace=TRUE), simplify = FALSE)
[[1]]
[,1] [,2] [,3]
[1,] 1 7 3
[2,] 3 7 7
[3,] 4 1 3
[4,] 6 4 1
[5,] 1 4 7
[6,] 6 0 3
[7,] 6 5 1
[[2]]
[,1] [,2] [,3]
[1,] 1 6 3
[2,] 4 5 3
[3,] 2 5 2
[4,] 1 7 3
[5,] 3 5 7
[6,] 2 4 3
[7,] 3 0 9
[[3]]
[,1] [,2] [,3]
[1,] 5 0 4
[2,] 4 5 9
[3,] 4 6 9
[4,] 5 5 7
[5,] 2 4 1
[6,] 3 1 4
[7,] 5 1 7
I have a list of matrices (with the same number of columns), say lst_Mat and I'd like to have all row-wise combinations of matrices in this list. For example, lst_Mat could be like this:
> lst_Mat
[[1]]
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 1 3 2 4
[3,] 1 3 4 2
[4,] 2 1 3 4
[5,] 2 3 1 4
[6,] 2 3 4 1
[[2]]
[,1] [,2] [,3] [,4]
[1,] 1 3 2 4
[2,] 3 1 2 4
[3,] 3 2 1 4
[4,] 3 2 4 1
[[3]]
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 1 2 4 3
[3,] 1 3 2 4
[4,] 1 3 4 2
[5,] 1 4 2 3
[6,] 1 4 3 2
[7,] 2 1 3 4
[8,] 2 1 4 3
[9,] 2 3 1 4
[10,] 3 1 2 4
[[4]]
[,1] [,2] [,3] [,4]
[1,] 2 1 4 3
[2,] 2 3 1 4
[3,] 3 1 2 4
[4,] 3 1 4 2
[5,] 3 2 1 4
As such, the total number of combinations would be 6*4*10*5=1200. This problem is analogous to the problem of generating all possible strings of English letters (i.e. a, b, c,..., x, y, z) with a specific length. For instance: aaa, aab, aac,..., aaz, aba, abb,..., abz, aca,... and so on.
I have come up with the following solution:
lst_Mat_len=list()
C=ncol(lst_Mat[[1]])
for (i in 1:length(lst_Mat))
lst_Mat_len[[length(lst_Mat_len)+1]]=(1:nrow(lst_Mat[[i]]))
combs=do.call(expand.grid, lst_Mat_len)
for (i in 1:nrow(combs)){
M=matrix(0, 0, C)
for (j in 1:ncol(combs))
M=rbind(M, lst_Mat[[j]][combs[i,j],])
# print(M)
}
Sample output of M:
> M
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 1 3 2 4
[3,] 1 2 3 4
[4,] 2 1 4 3
> M
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 1 3 2 4
[3,] 1 2 3 4
[4,] 2 3 1 4
That is, one row per matrix, each time.
I'd appreciate any other algorithms for doing so.
Here is another solution, I changed a little bit the example to make it more reproducible:
ones <- t(rep(1, 4))
lst_Mat <- list(1:6 %*% ones, 7:11 %*% ones, 12:21 %*% ones, 22:26 %*% ones)
combs <- expand.grid( sapply(lst_Mat, function(x) 1:nrow(x)) )
nbcombs <- nrow(combs)
res <- NULL
for (i in 1:nbcombs)
res[[i]] <- t(mapply(function(mat,line) mat[line,], lst_Mat, combs[i, ]))