Displaying 20 results from an estimated 10000 matches similar to: "Replace zeroes in vector with nearest non-zero value"
2008 Nov 05
2
matrix indexing and update
Folks,
I have a matrix:
set.seed(123)
a <- matrix(rnorm(100), 10)
And a vector:
b <- rnorm(10)
Now, I want to switch the signs of those rows of a corresponding to
indices in b whose values exceed the 75 %-ile of b
which(b > quantile(b)[4])
[1] 2 6 10
so I want, in effect:
a[2, ] <- -a[2, ]
a[6, ] <- -a[6, ]
a[10, ] <- -a[10, ]
I thought I could do
a[which(b >
2009 Mar 27
2
adding matrices with common column names
folks,
if i have three matrices, a, b, cc with some colnames in common, and i
want to create a matrix which consists of the common columns added up,
and the other columns tacked on, what's a good way to do it? i've got
the following roundabout code for two matrices, but if the number of
matrices increases, then i'm a bit stymied.
> a <- matrix(1:20,ncol=4); colnames(a) <-
2009 Apr 27
2
series at low freq expanded into high freq
Folks,
If I have a series mm of, say, monthly observations, and a series dd of
daily dates, what's a good way of expanding mm such that corresponding
to each day in dd within the corresponding month in mm, the values of mm
are repeated?
So e.g., if I have mm:
mm <- c(15, 10, 12, 13, 11)
names(mm)<-c("Nov 2008", "Dec 2008", "Jan 2009", "Feb
2010 Jan 20
1
min and max operations on matrix
Folks,
I've got a matrix x as follows:
> x <- matrix(c(1,2,3,5,3,4,3,2,1), ncol = 3, byrow = TRUE)
> x
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 5 3 4
[3,] 3 2 1
In each row of x, I want to replace the minimum value by -1, the maximum
value by +1 and all other values by 0.
So in the above case I want to end up as follows:
[,1] [,2] [,3]
[1,] -1 0
2009 Mar 02
2
Goldbach partitions code
Folks,
I put up a brief note describing my naive attempts to compute Goldbach
partitions, starting with a brute-force approach and refining
progressively.
http://jostamon.blogspot.com/2009/02/goldbachs-comet.html
I'd welcome your suggestions on improvements, alternatives, other
optimisations, esp. to do with space vs time tradeoffs.
Is this an example interesting enough for
2008 Jul 02
5
multiplication question
folks,
is there a clever way to compute the sum of the product of two vectors such that the common indices are not multiplied together?
i.e. if i have vectors X, Y, how can i compute
Sum (X[i] * Y[j])
i != j
where i != j
also, what if i wanted
Sum (X[i] * Y[j] * R[i, j])
i != j
where R is a matrix?
thanks,
murali
2007 Mar 29
1
creating conditional list of elements
Sorry to plague the list, but I think I got the answer. The following
would do:
> signalList <- list(tradingRules$Signal[tradingRules$Enabled]) [[1]]
> length(signalList)
[1] 2
Now my problem is shifted: I have the Signal column in the original data
frame referring to actual
matrices previously created in R. That is, bar_signal and cif_signal are
extant matrices. What I
need is the
2007 Sep 19
3
Row-by-row regression on matrix
Folks,
I have a 3000 x 4 matrix (y), which I need to regress row-by-row against a
4-vector (x) to create a
matrix lm.y of intercepts and slopes. To illustrate:
y <- matrix(rnorm(12000), ncol = 4)
x <- c(1/12, 3/12, 6/12, 1)
system.time(lm.y <- t(apply(y, 1, function(z) lm(z ~ x)$coefficient)))
[1] 44.72 18.00 69.52 NA NA
Takes more than a minute to do (and I need to do many
2007 Feb 13
2
Computing stats on common parts of multiple dataframes
Folks,
I have three dataframes storing some information about
two currency pairs, as follows:
R> a
EUR-USD NOK-SEK
1.23 1.33
1.22 1.43
1.26 1.42
1.24 1.50
1.21 1.36
1.26 1.60
1.29 1.44
1.25 1.36
1.27 1.39
1.23 1.48
1.22 1.26
1.24 1.29
1.27 1.57
1.21 1.55
1.23 1.35
1.25 1.41
1.25 1.30
1.23 1.11
1.28 1.37
1.27 1.23
R> b
EUR-USD NOK-SEK
1.23 1.22
1.21 1.36
1.28 1.61
1.23 1.34
1.21 1.22
2012 Feb 20
2
stats on transitions from one state to another
Folks,
I'm trying to get stats from a matrix for each transition from one state to another.
I have a matrix x as below.
structure(c(0, 2, 2, 2, 0, 0, 0, 1, 1, 1, 1, 2, 2, 1, 1, 1, 0,
0, 2, 2, 0.21, -0.57, -0.59, 0.16, -1.62, 0.18, -0.81, -0.19,
-0.76, 0.74, -1.51, 2.79, 0.41, 1.63, -0.86, -0.81, 0.39, -1.38,
0.06, 0.84, 0.51, -1, -1.29, 2.15, 0.39, 0.78, 0.85, 1.18, 1.66,
0.9, -0.94,
2010 Oct 07
1
Vector replace 0 elements without using a loop
Hi,
With a vector like:
x = c (22, 23, 22.5, 0,0,24, 0, 23.2, 23.5, 0,0,0, 26)
How can I replace the 0's with the previous last value without looping
through the vector ?
Something tells me I am missing the obvious.
Thanks,
Chris
--
View this message in context: http://r.789695.n4.nabble.com/Vector-replace-0-elements-without-using-a-loop-tp2966191p2966191.html
Sent from the R help
2007 Apr 27
2
Jarque-Bera and rnorm()
Folks,
I'm a bit puzzled by the fact that if I generate 100,000 standard normal
variates using rnorm() and perform the Jarque-Bera on the resulting vector,
I get p-values that vary drastically from run to run. Is this expected?
Surely the p-val should be close to 1 for each test?
Are 100,000 variates sufficient for this test?
Or is it that rnorm() is not a robust random number generator?
2011 Mar 31
3
choosing best 'match' for given factor
Folks,
I have a 'matching' matrix between variables A, X, L, O:
> a <- structure(c(1, 0.41, 0.58, 0.75, 0.41, 1, 0.6, 0.86, 0.58,
0.6, 1, 0.83, 0.75, 0.86, 0.83, 1), .Dim = c(4L, 4L), .Dimnames = list(
c("A", "X", "L", "O"), c("A", "X", "L", "O")))
> a
A X L O
A 1.00 0.41
2010 Aug 31
2
simultaneous estimation
Hi folks,
Not sure what this sort of estimation is called. I have a 2-column time-series x(i,t) [with (i=1,2; t=1,...T)], and I want to do the following 'simultaneous' regressions:
x(1,t) = (d - 1)(x(1, t-1) - mu(1))
x(2,t) = (d - 1)(x(2, t-1) - mu(2))
And I want to determine the coefficients d, mu(1), mu(2).
Note that the d should be the same for both estimations, whereas the
2009 Dec 03
4
Replace values in a vector
Hi all,
I have a vector like this:
x<- c(0.7, 0.1, 0, 0.2, 0.2, 0, 0, 0 , 0, 0.4, 0, 0.8, 1.8)
I would like to replace the zero values with the first previous non zero value.
my returning vector should look like this:
y<-c( 0.7, 0.1, 0.1,0.2,0.2,0.2,0.2,0.2, 0.4, 0.4, 0.8, 1.8)
How can I do this in R without using for loop?
Thank you
2010 Oct 12
1
graphics layout
Folks,
I'm battling the layout() functionality in graphics, and getting a bit mixed up. I'd like to create subscreens like so:
_________ _________
| | |
| 1 | 2 |
|_________|________ |
| | |
| 3 | 4 |
|_________|_________|
| |____6____|
| 5 |____7____|
|_________|____8____|
Note that subscreens 1:5 are the same
2010 Jun 28
1
Zoo series to a date time stamp that is regular
NOTE: I will provide data if necessary, but I didn't want clutter
everyones mailbox
All:
I have a time series with level and temperature data for 11 sites for
each of three bases. I will have to do this more than once is what I
am saying here. OK, The time series are zoo objects with index
values in chron format. The problem is that the date and times should
be at even 15 min intervals,
2007 Mar 16
1
cumsum over varying column lengths
Folks,
I have a matrix of historicalReturns, where entry (i, j) is the daily return
corresponding to date i and equity j. I also have a matrix startOffset,
where entry (1, k) is the row offset in historicalReturns where I entered
into equity k.
So we have that NCOL(startOffset) = NCOL(historicalReturns).
Now I would like compute for each column in historicalReturns, the
cumulative return
2007 Feb 16
1
array searches
Folks,
I have a dataframe comprising a column of dates and a column of signals (-1,
0, 1) that looks something like this:
30/01/2007 0
31/01/2007 -1
01/02/2007 -1
02/02/2007 -1
03/02/2007 1
04/02/2007 1
05/02/2007 1
06/02/2007 1
07/02/2007 1
08/02/2007 1
09/02/2007 0
10/02/2007 0
11/02/2007 0
12/02/2007 1
13/02/2007 1
14/02/2007 1
15/02/2007 0
16/02/2007 0
What I need to do is for each signal
2010 Oct 18
1
questions on unstack()
Folks,
I have the following dataframe:
> x <- structure(list(name = c("EU B", "EU B", "EU B", "EU B", "EU B",
"EU B", "AU A", "AU A", "AU A", "AU A", "AU A", "AU A"), date = c("2010-10-11",
"2010-10-12", "2010-10-13",