Displaying 20 results from an estimated 10000 matches similar to: "replace zeros in a block diagonal matrix with small random values?"
2010 Apr 23
3
reordering of matrix rows to maximize the sum of the diagonal
Hi r-help community,
This question isn't so much a syntax/coding one, but here goes:
Let's say I have matrix of arbitrary dimensions and I'd like to
reorder the rows in such a way that I could maximize the sum of the
entries along the diagonal.
For example, for this 3x3 matrix:
[,1] [,2] [,3]
[1,] 3 4 13
[2,] 9 1 2
[3,] 2 11 1
rearranging the rows
2010 Mar 09
3
Removing Zeros from matrix
Hi Everybody,
I have a matrix of about 45 columns. Some of the rows contain zeros. Using
>data1<-data[complete.cases(data),], I can remove the "NA" rows. But I am
unable to tackle that of zeros.
Can anybody give me an idea of how to remove rows containing zeros in a
matrix.
Thanks so much
Best
Ogbos
[[alternative HTML version deleted]]
2008 Oct 14
4
request: How to ignore columns having zero sums
Dear friends
I have an array consist of r-rows and c-columns e.g.
x=c(1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,0,0,0,0,0,0,0,0);
x1=array(x, dim=c(4,6))
output is
> x1
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 2 3 4 0 0
[2,] 1 2 3 4 0 0
[3,] 1 2 3 4 0 0
[4,] 1 2 3 4 0 0
How can i ignore columns having zero sums? Help in this regard
2009 Feb 17
2
Efficient matrix computations
Hi,
I am looking for two ways to speed up my computations:
1. Is there a function that efficiently computes the 'sandwich product' of
three matrices, say, ZPZ'
2. Is there a function that efficiently computes the determinant of a
positive definite symmetric matrix?
Thanks,
S.A.
[[alternative HTML version deleted]]
2010 Sep 06
3
Aggregate certain rows in a matrix
Hi,
I have a matrix that looks like this
a <- c(1,1,1,1,2,2,3,3,3,3)
b <- c(2,2,2,3,4,4,4,5,5,6)
c <- c(1,2,3,4,5,6,7,8,9,10)
M <- matrix(nr=10,nc=3)
M[,1] <- a
M[,2] <- b
M[,3] <- c
> M
[,1] [,2] [,3]
[1,] 1 2 1
[2,] 1 2 2
[3,] 1 2 3
[4,] 1 3 4
[5,] 2 4 5
[6,] 2 4 6
[7,] 3 4 7
2011 Oct 06
1
Concecutive zeros and ones
Dear all,
I have a data series (might be vector or matrix) which is composed only from zeros and ones like the following example
0 0 0 1 1 0 1 0 0 1 1 1 1 0 0 0
I want to be able to return back the length of concecutive zeros and the length of concecutive ones.
For that I want to have something like that returned:
zeros= [3 1 2 3];
ones=[2 1 4];
How I can do that simply in R?
I would like
2009 Feb 12
3
get top 50 correlated item from a correlation matrix for each item
Hi,
I have a correlation matrix of about 3000 items, i.e., a 3000*3000
matrix. For each of the 3000 items, I want to get the top 50 items that
have the highest correlation with it (excluding itself) and generate a
data frame with 3 columns like ("ID", "ID2", "cor"), where ID is those
3000 items each repeat 50 times, and ID2 is the top 50 correlated items
with ID,
2009 Apr 05
3
number of zeros in a matrix -row by row
Hi. I have an n x m matrix M some of who's entries are zeros. I want to know
how many zeros there are in each row -perhaps stored in a 1 x n vector
which lists the number of zeros for each row of M.
Before I had a vector V and I was able to get the number of zeros in V by
doing length(V[ V==0]) but when I try something similar for M, like M[ M==0]
it creates a vector not a matrix and so
2010 Nov 09
3
Row-wise recurive function call
Dear Group,
I have a following dataset:
> a
A B C D
1 22 3 31 40
2 26 31 36 32
3 3 7 49 16
4 24 40 27 26
5 20 45 47 0
6 34 43 11 18
7 48 48 24 2
8 3 16 39 48
9 20 49 7 21
10 17 36 47 10
> dput(a)
structure(list(A = c(22L, 26L, 3L, 24L, 20L, 34L, 48L, 3L, 20L,
17L), B = c(3L, 31L, 7L, 40L, 45L, 43L, 48L, 16L, 49L, 36L),
C = c(31L, 36L, 49L, 27L, 47L, 11L, 24L,
2011 Jun 20
2
Replace selected columns of a dataframe with NA
I am using the following command to replace all the missing values and
assorted typos in a dataframe with NA:
mydata[mydata>80]=NA
The problem is that the first column contains values which should be
more than 80, so really I want to do it just for
mydata[,2:length(mydata)]
I can't seem to re-write the code to fit:
mydata[,2:length(mydata)>80]=NA # no error message, but doesn't
2012 Apr 20
3
Matrix multiplication by multple constants
Dear R helpers
Suppose
x <- c(1:3)
y <- matrix(1:12, ncol = 3, nrow = 4)
> y
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
I wish to multiply 1st column of y by first element of x i.e. 1, 2nd column of y by 2nd element of x i.e. 2 an so on. Thus the resultant matrix should be like
> z
[,1] [,2] [,3]
[1,] 1
2007 May 11
1
Create an AR(1) covariance matrix
Hi All.
I need to create a first-order autoregressive covariance matrix
(AR(1)) for a longitudinal mixed-model simulation. I can do this
using nested "for" loops but I'm trying to improve my R coding
proficiency and am curious how it might be done in a more elegant
manner.
To be clear, if there are 5 time points then the AR(1) matrix is 5x5
where the diagonal is a constant
2008 Sep 17
5
Loop on vector name
[My previous message rejected, therefore I am sending same one with some modification]
I have 3 vectors with object name : dat1, dat2, dat3
Now I want to create a loop, like :
for (i in 1:3)
{
cat(sd(dati))
}
How I can do this in R?
Regards,
2010 Jul 26
2
Concatenate a mix of numbers and letters to create a vector name
Dear all,
I am trying to create a vector name, for example tmax.195012 from tmax., 1950 and 12. Obviously I don't wish to simply type it because the 3 name components are changing in each iteration within a loop. Is there any way of concatenating those 3 components (which are a mixture of numbers and letters)?
Thanks for reading,
Panos
2011 Aug 23
4
Correlation discrepancy
Dear R list, I have one very elementary question regrading correlation between two variables.
x = c(44,46,46,47,45,43,45,44)
y = c(44,43,41,41,46,48,44,43)
> cov(x, y)
[1] -2.428571
However, if I try to calculate the covariance using the formula as
covariance = sum((x-mean(x))*(y-mean(y)))/8 # no of of paired obs. = 8
or
covariance = sum(x*y)/8-(mean(x)*mean(y))
gives
2010 Sep 27
1
compare two matrices
Hi everyone:
I have a kinda easy question but i do not know how to solve that in a simple way.
I want to compare the rows of two matrices.
col1 <- c(1,2,3,4,5,6)
col2 <- c(6,5,4,3,2,1)
m <- cbind(col1, col2)
col3 <- c(1,3,2,6)
col4 <- c(6,3,5,1)
n <- cbind(col3, col4)
In matrix n, for example the first row is (1,6), it is also some row
2009 Sep 06
1
struggling with "split" function
I am very sorry for such a simple question, but I am struggling with "split".
I have the following data frame:
x<-data.frame(A=c(NA,NA,NA,NA,"split",NA,NA,NA,NA,"split",NA,NA,NA,NA,"split",NA,NA,NA,NA),
2008 Sep 21
2
Symmetric matrix
I have following matrix :
a = matrix(rnorm(36), 6)
Now I want to replace the lower-triangular elements with it's upper-triangular elements. That is I want to make a symmetric matrix from a. I have tried with lower.tri() and upper.tri() function, but got desired result. Can anyone please tell me how to do that?
2009 Oct 31
1
avoiding loop
Hi all,
I am trying to figure out a way to improve my code's efficiency by avoiding the use of loop.
I want to calculate a conditional mean(?) given time.
For example, from the data below, I want to calculate sum((value|choice==1)/sum(value)) across time.
Is there a way to do it without using a loop?
time cum_time choice value
1 4 1 3
1 4
2010 Feb 23
1
function on all pairs of vector entries
Hello all,
Is there a way in R to compute the multivariate normal density of every pair of entries in a vector efficiently instead of using for loop?
For example
Suppose I have a vector a=c(v_1,...,v_p)=c(0.5343909, -0.7784353, -0.0568370, 1.8772838, -1.3183407, 0.8227418,...)
I want to compute density(v_i, v_j) for every pair of entries (i,j) (i!=j) in a. The joint bivariate distribution