I have a list of 40 data sets who all have the same columns. I want to bind the 7th column of each data set. I thought about doing this with a matrix using cbind. This is my code:
RetRates <- function(q) {
q <- matrix(nrow = 766, ncol = length(ListeActions),
data = rep(0, 766), byrow = TRUE)
s <- 0
for (i in 1:length(ListeActions)) {
x <- ListeActions[[i]]
q[,i] <- cbind(q[,i], x[,9]) ## I need the 9th column
}
return(q)
}
Hedi <- matrix(nrow = 766, ncol = length(ListeActions),
data = rep(0, 766), byrow = TRUE)
Hedi <- RetRates(Hedi)
I get these warnings :
Warning messages: 1: In replace(q[, i], 1:766, x[, 9]) : the number
of objects to be replaced is not a multiple of the size of the
replacement !
Let's take a smaller example: cbind the 5th columns of each of these 3 matrices
d1 <- matrix(runif(30), 5, 6)
d2 <- matrix(rnorm(30), 5, 6)
d3 <- matrix(rnorm(30), 5, 6)
First we put the 3 matrices in a list
M <- list(d1=d1, d2=d2, d3=d3)
Then we could use, as in your question, a for loop
res1 <- matrix(NA, nrow=5, ncol=length(M))
for (i in 1:length(M)) {
res1[, i] <- M[[i]][,5]
}
Or we could use some magical R functions to get the result in one slightly more obscure command
res2 <- do.call(cbind, lapply(M, "[",,5))
Related
I would like to read 3 independent correlation matrix in one array.
I have followed the as indicated in here
However, getting error and don’t why. I would appreciate if some one could see my code and help me.
Here are my codes and simulated data.
dataDir <- getwd()
## Each matrix is in a csv file
set.seed(22)
## m1
li.A <- matrix(rnorm(100), nrow = 20)
rownames(li.A) <- LETTERS[1:20]
colnames(li.A) <- paste0("S_", ncol = 1:5)
m1 <- cor(t(li.A))
write.csv(m1, file = “m1.csv")
# m2
set.seed(42)
pa.A <- matrix(rnorm(100), nrow = 20)
rownames(pa.A) <- LETTERS[1:20]
colnames(pa.A) <- paste0("S_", ncol = 1:5)
m2 <- cor(t(pa.A))
write.csv(m2, file = “m2.csv")
# m3
set.seed(44)
li.B <- matrix(rnorm(100), nrow = 20)
rownames(li.B) <- LETTERS[1:20]
colnames(li.B) <- paste0("S_", ncol = 1:5)
m3 <- cor(t(li.B))
write.csv(m3, file = “m3.csv")
fileList <- dir(path=dataDir,pattern = ".csv")
## Read all matrices into an array
A <- array(as.numeric(NA),dim=c(20,20,3)) # There are 3 matrices of size 20 x 20
for (i in 1:length(fileList)){
A[,,i] <- as.matrix(read.delim(file.path(dataDir,fileList[i]), sep = ';', header=TRUE, row.names=1))
}
here is the error.
Error in A[, , i] <- as.matrix(read.delim(file.path(dataDir, fileList[i]), :
replacement has length zero
Thank you!
The issue would be related to the sep = ';' instead it is sep="," and it returns a single string column instead of the multiple columns. Therefore, when we do the assignment with indexing, it showed the error
A <- array(as.numeric(NA),dim=c(20,20,3)) # There are 3 matrices of size 20 x 20
for (i in 1:length(fileList)){
A[,,i] <- as.matrix(read.delim(file.path(dataDir,fileList[i]),
sep = ',', header=TRUE, row.names=1))
}
dim(A)
#[1] 20 20 3
I am using R v 3.0.0 (2013-04-03) and RStudio v 1.1.463 under Win-7 64-bit.
In the following source code:
# Problem 1 - Matrix powers in R
#
# R does not have a built-in command for taking matrix powers.
# Write a function matrixpower with two arguments mat and k that
# will take integer powers k of a matrix mat.
matrixMul <- function(mat1)
{
rows <- nrow(mat1)
cols <- ncol(mat1)
matOut = matrix(, nrow = rows, ncol = cols) # empty matrix
for (i in 1:rows)
{
for(j in 1:cols)
{
vec1 <- mat1[i,]
vec2 <- mat1[,j]
mult1 <- vec1 * vec2
matOut[i,j] <- mult1
}
}
return(matOut)
}
matrixpower<-function(mat1, k)
{
matOut <-mat1#empty matix
for (i in k)
{
matOut <- matrixMul(matOut)
}
return(matOut)
}
mat1 <- matrix(c(1,2,3,4,5,6,7,8,9), nrow = 3, ncol=3)
power1 <- matrixMul(mat1)
the declaration
matOut <- matrix(, nrow = rows, ncol = cols) # empty matrix
is giving the following syntax error even before compilation:
missing argument to function call
I am following these instructions.
What am I doing wrong here?
Try this:
matOut = matrix(numeric(rows*cols), nrow = rows, ncol = cols) # empty matrix
I need to convert the following code to one with for loop, what is the easiest way to do it?
set.seed(123)
iter <- 1000
s1 <- 2
mat1 <- matrix(data = rcauchy(iter*s1,0,1),nrow = iter,ncol = s1)
sets1 <- apply(mat1,1,median)
hist(sets1)
s2 <- 5
mat2 <- matrix(data = rcauchy(iter*s2,0,1),nrow = iter,ncol = s2)
sets2 <- apply(mat2,1,median)
hist(sets2)
s3 <- 10
mat3 <- matrix(data = rcauchy(iter*s3,0,1),nrow = iter,ncol = s3)
sets3 <- apply(mat3,1,median)
hist(sets3)
s4 <-20
mat4 <- matrix(data = rcauchy(iter*s4,0,1),nrow = iter,ncol = s4)
sets4 <- apply(mat4,1,median)
hist(sets4)
I tried the following:
set.seed(1234)
iter <- 1000
size <- c(2,5,10,20)
for(i in 2:size){
for (j in 1:iter){
mat[] <- matrix(data = rcauchy(i*j,0,1),nrow=iter,ncol=i)
s <- apply(mat,1,median)
hist(s)
}
}
But it does not work, please help
The easies way is to wrap the creation of the matrix into a lapply function.
set.seed(123)
iter <- 1000
size <- c(2,5,10,20)
returnmatrix<-lapply(size, function(i){
mat<-matrix(data = rcauchy(i*iter,0,1),nrow=iter,ncol=i)
s <- apply(mat,1,median)
hist(s, main=paste("Histogram when S=", i))
mat
})
The lapply function will plot the histograms and will return the matrixes as list if additional processing is desired.
I have a list of matrices I would like to first convert into individual vectors then combine them into one large matrix. I have been trying to implement a for loop to do this and have been unable to do so with any success.
Example data: 5 10x10 matrices in a list
m1 <- matrix(1:100, nrow = 10, ncol = 10)
m2 <- matrix(1:100, nrow = 10, ncol = 10)
m3 <- matrix(1:100, nrow = 10, ncol = 10)
m4 <- matrix(1:100, nrow = 10, ncol = 10)
m5 <- matrix(1:100, nrow = 10, ncol = 10)
mylist <- list(m1, m2, m3, m4, m5)
I can turn an individual matrix into a vector using the following:
unlist(mylist[1])
My for loop is as follows, and currently outputs them all into a single vector:
z = list()
for (i in mylist[length(mylist)]) {
n <- unlist(mylist)
z <- c(n)
}
length(z)
[1] 500
The output I would like would be the df:
m1 <- unlist(mylist[1])
m2 <- unlist(mylist[2])
m3 <- unlist(mylist[3])
m4 <- unlist(mylist[4])
m5 <- unlist(mylist[5])
df <- cbind(m1, m2, m3, m4, m5)
Any help would be great! Thanks!
To use a for loop, I would do it like this:
z = list()
for (i in seq_along(mylist)) {
z[[i]] = c(mylist[[i]])
}
But when you're just applying a simple function to each list element, lapply is very nice:
z = lapply(mylist, c)
If you then want to cbind all of these vectors, we can either use Reduce or do.call:
do.call(what = cbind, args = z)
Reduce(f = cbind, x = z)
The do.call is equivalent to cbind(z[[1]], z[[2]], z[[3]], ...), and the Reduce is equivalent to cbind(z[[1]], cbind(z[[2]], cbind(z[[3]], ...))). Since cbind accepts any number of arguments, do.call is probably nicer. You would need Reduce if you wanted to add or multiply the vectors, for example.
I am running a poisson on a given matrix in R. And for some reasons, I would like to avoid any rowSums and colSums equal to 5 in example. I add repeat in my code to solve this issue but the run never end, even for small matrix.
Any ideas?
Thanks
mat <- matrix(c(seq(1,60,1)), nrow = 6, ncol = 10, byrow = TRUE)
poiss <- function(mat) {
repeat{
pmat <- rpois(length(mat), 1:3)
dim(pmat) <- dim(mat)
if(any(rowSums(pmat)) == 5 | any(colSums(pmat)) == 5) break
}
return(pmat)
}
p = poiss(mat)