Displaying 20 results from an estimated 90000 matches similar to: "compare and replace"
2005 Feb 21
5
Compare rows of two matrices
Hello,
#I have two matrices, eg.:
y <- matrix( c(20, NA, NA, 45, 50, 19, 32, 101, 10, 22, NA, NA, 80, 49, 61, 190), ncol=4 )
x <- matrix( c(20, NA, NA, NA, 50, 19, 32, 101, 10, 22, NA, NA, 80, 49, 61, 190), ncol=4 )
#Whereas x contains all NA?s from y plus some additional NA?s.
#I want to find the index of these additional NA?s. I think, there must be a very
2003 Jul 12
2
using cut on matrices
Dear list,
I'd like to use the function cut() on matrices, ie that when I apply
it to a matrix, it would return a matrix of the same dimensions
instead of a vector.
I wonder if there is a better (more elegant) solution than
matrix(cut(a, ...), ncol=ncol(a), nrow=nrow(a))
because I would like to use cut on both vectors and matrices and avoid
testing whether a is a matrix.
Thanks,
Tamas
2007 Nov 21
2
matrix elementwise average with NA's
Hello fellow R users,
I have a matrix computation that I imagine should be relatively easy to do, however I cannot figure out a nice way to do it. I have two matrices, for example
mat1 <- matrix(c(1:5,rep(NA,5), 6:10), nrow=3, byrow=T)
mat2 <- matrix(c(2:6, 6:10, rep(NA,5)), nrow=3, byrow=T)
I'd like to compute the element-wise average for non-NA entries. Of course
(mat1+mat2)/2
2005 Aug 18
2
matrix indexing
Dear R-users,
I was wondering for the following:
Let 'x' be a matrix and 'ind' and indicator matrix,
i.e.,
x <- array(1:20, dim = c(4, 5))
ind <- array(c(1:3, 3:1), dim = c(3, 2))
I'd like to get (as a vector) the elements of 'x'
which are not indexed by 'ind'. Since negative indices
are not allowed in index matrices I thought of using
something like:
2008 Apr 21
4
matrix problem
Hi Everyone,
I am running into a problem with matrices. I use R version 2.4.1 and
an older version.
The problem is this:
m<-matrix(ncol=3,nrow=4)
m[,1:3]<-runif(n=4)
That does what I expect; it fills up the rows of the matrix with the
data vector
> m
[,1] [,2] [,3]
[1,] 0.2083071 0.2083071 0.2083071
[2,] 0.5865763 0.5865763 0.5865763
[3,] 0.7901782 0.7901782
2012 Mar 16
2
Elegant Code
Hi,
Can anyone help to write a more elegant version of my code? I am sure
this can be put into a loop but I am having trouble creating the
objects b1,b2,b3,...,etc.
b1 <- rigamma(50,1,1)
theta1 <- rgamma(50,0.5,(1/b1))
sim1 <- rpois(50,theta1)
b2 <- rigamma(50,1,1)
theta2 <- rgamma(50,0.5,(1/b2))
sim2 <- rpois(50,theta2)
b3 <- rigamma(50,1,1)
theta3 <-
2006 Jun 24
2
data frame search
Hi,
I want to make a data frame which contains the positions of some searched values in another data frame.
Like:
Dataframe 1:
1 2 3 4 1 2 3 4
2 3 4 1 2 3 4 2
4 1 2 3 2 3 4 1
Let's say I searched on "4", then Dataframe 2 should contain:
x y
1 4
1 8
2 3
2 7
3 1
3 7
I have written a routine, but it seems to me that it isn't that perfect:
x<- 0
2010 Sep 04
4
Please explain "do.call" in this context, or critique to "stack this list faster"
I've been doing some consulting with students who seem to come to R
from SAS. They are usually pre-occupied with do loops and it is tough
to persuade them to trust R lists rather than keeping 100s of named
matrices floating around.
Often it happens that there is a list with lots of matrices or data
frames in it and we need to "stack those together". I thought it
would be a simple
2012 Dec 08
5
How to efficiently compare each row in a matrix with each row in another matrix?
Dear expeRts,
I have two matrices A and B. They have the same number of columns but possibly different number of rows. I would like to compare each row of A with each row of B and check whether all entries in a row of A are less than or equal to all entries in a row of B. Here is a minimal working example:
A <- rbind(matrix(1:4, ncol=2, byrow=TRUE), c(6, 2)) # (3, 2) matrix
B <-
2010 Jun 21
1
replace NA-values
Dear list,
I'm trying to replace NA-values with the preceding values in that column.
This code works, but I am sure there is a more elegant way...
df <- data.frame(id = c("A1", NA, NA, NA, "B1",
NA, NA, "C1", NA, NA, NA, NA),
value = c(1:12))
rn <- c(rownames(df[!is.na(df$id),]), nrow(df)+1)
rn <-
2009 Jan 09
3
Matrix: Problem with the code
Hi,
Can any one please explain why the following code doesn't work? Or can anyone suggest an alternative.
Suppose
x<-c(23,67,2,87,9,63,8,2,35,6,91,41,22,3)
mat<-0;
for(j in 1:length(x))
{
for(i in 1:p)
mat[i,j]<-x[j]^i;
}
Actually I want to have a matrix with p columns such that each column will have the
2008 Jan 15
2
Looking for simpler solution to probabilistic question
Hi
I have two processes which take with a certain probability (p1 and p2) x
number of years to complete (age1 and age2). As soon as thge first
process is completed, the second one begins. I want to calculate the
time it takes for the both processes to be completed.
I have the following script which gives me the answer, butI think there
must be a more elegant way of doing the calculations
2012 Jul 03
2
"evaluating expressions" contained in a dataframe
#I have a dataframe called "tests" that contain "character expressions". These
characters are rules that use data from within another dataframe. Is there
any way within R I can access the rules in the dataframe called tests, and
then "evaluate" these "rules"?
#An example may better explain what I am trying to accomplish:
tests <-
2004 Jul 30
2
pairwise difference operator
There was a BioConductor thread today where the poster wanted to find
pairwise difference between columns of a matrix. I suggested the slow
solution below, hoping that someone might suggest a faster and/or more
elegant solution, but no other response.
I tried unsuccessfully with the apply() family. Searching the mailing
list was not very fruitful either. The closest I got to was a cryptic
chunk
2005 Aug 15
4
Re-sort list of vectors
Hi.
Can anyone suggest a simple way to re-sort in R a list of vectors of the
following form?
input
$"1"
a b c
1 2 3
$"2"
a b c
4 5 6
Output should be something like:
"a"
"1" 1
"2" 4
"b"
"1" 2
"2" 5
"c"
"1" 3
"2" 6
I've been futzing with mapply(), outer(), split(), rbind()
2004 Jul 28
6
elegant matrix creation
Hello everybody.
I am trying to reproduce a particular matrix in an elegant way. If I
have
jj1 <-
structure(c(1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,
3,1,2,3,1,2,3,1,2,3,2,3,1,2,3,1,2,3,1,2,3,
1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,3,1,2,3,1,
2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,
2),.Dim = as.integer(c(9,9)))
[ image(jj1) is good here ] then I can get this with
2012 Sep 19
4
correlating matrices
Hi,
thank you for taking the time and reading my question. My question is
twofold:
1. I have several matrices with variables and one matrix with water levels.
I want to predict the water level with the data in the other matrices.
Basically,
* mod<-lm(matrix1 ~ matrix2+matrix3)*
( What looks like a minus is meant to be the wiggly minus.)
Of course I could dissemble the matrices and paste
2018 May 10
3
Fill down a new column in data frame with a number
Hi,
I am a begginer in R programming.
I am traying to create a a column in my data frame filled down with a
number.
> df$newcolumn <- number
How can I do it? I am considering use rep() but in this case it is
necessary know the number of rows in each data base that I have and I would
like to do it in a faster ( and more elegant) way.
TKs
[[alternative HTML version deleted]]
2005 Feb 21
0
AW: Compare rows of two matrices
Excellent. That was very helpful. Now I have full control about my NA?s :-)
Thank you very much!!!
Matthias
>
> Here is an another way
>
> count <- is.na(x) + is.na(y)
> which( count == 1, arr.ind=TRUE )
>
> 'count' gives you the number of missing values at for each
> row and column. Then you can find out how many occurances of
> both missing,
2009 Aug 26
3
changing equal values on matrix by same random number
Dear all,
I have about 30,000 matrix (512x512), with values from 1 to N.
Each value on a matrix represent a habitat patch on my
matrix (i.e. my landscape). Non-habitat are stored as ZERO.
No I need to change each 1-to-N values for the same random
number.
Just supose my matrix is:
mymat<-matrix(c(1,1,1,0,0,0,0,0,0,0,0,
0,0,0,0,2,2,2,0,0,0,0,
0,0,0,0,2,2,2,0,0,0,0,
3,3,0,0,0,0,0,0,0,4,4,