Displaying 20 results from an estimated 30000 matches similar to: "matrix indexing"
2005 Dec 05
1
extracting p-values from lmer()
Dear R users,
I've been struggling with the following problem: I want to extract the Wald p-value
from an lmer() fit, i.e., consider
library(lme4)
n <- 120
x1 <- runif(n, -4, 4)
x2 <- sample(0:1, n, TRUE)
z <- rnorm(n)
id <- 1:n
N <- sample(20:200, n, TRUE)
y <- rbinom(n, N, plogis(0.1 + 0.2 * x1 - 0.5 * x2 + 1.5 * z))
m1 <- lmer(cbind(y, N - y) ~ x1 + x2 + (1 | id),
2009 Feb 23
1
Slow indexing access for Matrix
Consider the following little "benchmark"
> require(Matrix)
> tmp <- Matrix(c(rep(1,1000),rep(0,9000)),ncol=1)
> ind <- sample(1:10000,10000)
> system.time(tmp[ind,])
user system elapsed
0.004 0.001 0.005
> ind <- sample(1:1000,10000,replace=TRUE)
> system.time(tmp[ind,])
user system elapsed
0.654 0.006 0.703
>
2011 Jan 27
3
how to divide each element of a matrix by a specific value per column
Hi,
I'd like to divide each element of a matrix by a specific value per column. These specific values are stored in a list. For example:
> x <- c(1,2,3,4,5)
> y <- matrix(c(1:30), nrow = 6)
Now I want to divide each element in y[,1] by x[1], y[,2] by x[2] etc. I have tried this
> my_function <- function(data, ind) data/ind
> apply(y, 2, my_function, x)
[,1] [,2]
2005 Oct 02
1
plot question when type = "b" and pch is a vector
Dear R users,
I've been struggling some days with the following
problem: I'm interesting in producing the following
plot
x <- seq(0.01, 10, length = 20)
plot(c(0, 10), c(-20, 20), type = "n", xlab = "x",
ylab = expression(2 * alpha * log(x)))
pch. <- rep(NA, length(x))
for(i in 1:4){
pch.[7] <- as.character(i)
lines(x, 2 * i * log(x), type
2005 Oct 07
1
builiding R from sources
Dear R users,
I've been trying to build R from sources (in Windows) using Dr. Goto's BLAS,
unsuccessfully! I've followed the instructions in Section 3.1.2-3.1.3 of "R
Installation and Administration" manual (but maybe I did something wrong), but I
keep receiving the following error:
-- initially I get --
make: ./Rpwd.exe: Command not found
make[1]: ./Rpwd.exe: Command not
2006 Sep 23
2
Create a vector of indices from a matrix of start and end points
I have a very large dataframe and wish to extract a subset of rows. I
have a two column matrix listing the starting and ending indices of
one subset on each row. My idea is to create a vector of indices that
could be applied to the dataframe and I have a solution using a for
loop (below). But surely there is some more elegant way to do this! I
looked thorough the archives without
2009 Sep 19
1
matrix operations on grobs and grid units
Dear list,
As a minimal test of a more complex grid layout, I'm trying to find a
clean and efficient way to arrange text grobs in a rectangular layout.
The labels may be expressions, or text with a fontsize different of
the default, which means that the cell sizes should probably be
calculated using grobWidth() and grobHeight() as opposed to simpler
stringWidth() and stringHeight().
2005 Apr 19
2
indexing an array using an index-array, but one entry being ', '
Hi
I have the following array:
test <- array(c(1:16), dim = c(3,4,3))
test
## I call some enries using an index array
test.ind <- array(rbind(c(1,2,1), c(3,3,2)), dim = c(2,3))
test[test.ind]
## suppose I want all values in the 2nd row and 4th col over
## all three 3rd dimensions
test[2,4,]
how to specify a test.ind array with the last index left with ',' i.e
test.ind should be
2005 Jan 11
4
Matrix to "indexed" vector
I have a matrix that I want to turn into a transformed matrix that
includes the indices from the original matrix and the value. The
matrix is simply real-valued and is square (and large (8k x 8k)). I
want something that looks like (for the 3x3 case):
i j value
1 1 1.0
1 2 0.783432
1 3 -0.123482
2 1 0.783432
2 2 1.0
2 3 0.928374
and so on....
I can do this with for loops, but there is
2006 Apr 04
1
Indexing a vector by a list of vectors
Hello R-help -
I have
vec <- c("string1", "string2", "string3")
ind <- list(c(1,2),c(1,2,3))
I want "vec" indexed by each vector in the list "ind".
The first element of the list I want would be vec[c(1,2)],
the second element would be vec[c(1,2,3)], like the following.
[[1]]
[1] "string1" "string2"
[[2]]
[1]
2004 Oct 09
2
which() and value replacement in a matrix
Hi,
I cannot go through the archives with which() as key-word... so common. Though I am sure to have seen something about this subject
in the past could somebody put me on the track. I have a matrix (actually a data.frame) in which I would replace the non-null values
by 1.
I tried the following:
indices<-which(myforetbin > 0,arr.ind=T)
myforetbin[indices[,1],indices[,2]]<-1
and get
2004 Jun 18
2
Barplots and error indicators: Some R-Code
I' ve seen that several people are looking for a function that creates a
barplot with an error indicators (I was one of them myself). Maybe you will
find the following code helpful (There are some examples how to use it at
the end):
# Creates a barplot.
#bar.plot() needs a datavector for the height of bars and a error
#indicator for the interval
#many of the usual R parameters can be set:
2011 Jul 11
2
lm: mark sample used in estimation
Hi all,
I wanted to mark the estimation sample: mark what rows (observations)
are deleted by lm due to missingness. For eg, from the original
example in help, I have changed one of the values in trt to be NA
(missing).
# code below
# ----
# original example
> ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
> trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
#
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 >
2010 Sep 13
2
value returned by by()
Hi,
I noticed that by() returns an object of class 'by', regardless of what
its argument 'simplify' is. ?by says that it always returns a list if
simplify=FALSE, yet by.data.frame shows:
---<--------------------cut here---------------start------------------->---
function (data, INDICES, FUN, ..., simplify = TRUE)
{
if (!is.list(INDICES)) {
IND <-
2008 Mar 28
1
Beginner help with retrieving frequency and transforming a matrix
Hi All,
Just hoping some one can give me a hand with a problem...
I have a dataframe (DF) with about 5 million entries that looks something
like the following:
>DF
ID Cl Co Brd Ind A AB AB
1 S-3 IND A BR_F BR_F01 1 0 0
2 S-3 IND A BR_F BR_F01 1 0 0
3 S-3 IND A BR_F BR_F01 1 0 0
4 S-3 IND A BR_F BR_F01 1 0 0
5 S-3 IND A BR_F BR_F01 1 0 0
6 S-3 IND A BR_F
2008 Apr 15
1
by inconsistently strips class - with fix
summary:
The function 'by' inconsistently strips class from the data to which
it is applied.
quick reason:
tapply strips class when simplify is set to TRUE (the default) due to
the class stripping behaviour of unlist.
quick answer:
This can be fixed by invoking tapply with simplify=FALSE, or changing
tapply to use do.call(c instead of unlist
executable example:
2007 Feb 28
3
matrix manipulations
Dear friends,
I have a basic question with R. I'm generating a set
of random variables and then combining them using the
cbind statement. The code for that is given below.
for (i in 1:100)
{
y <- rpois(i,lambda=10)
X0 <- seq(1,1,length=i)
X1 <- rnorm(i,mean=5,sd=10)
X2 <- rnorm(i,mean=17,sd=12)
X3 <- rnorm(i,mean=3, sd=24)
ind <- rep(1:5,20)
}
2013 Feb 23
2
assign index to colnames(matrix)
Hello, I’m trying to follow the syntax of a script from a journal website. In order to create a regression formula used later in the script, the regression matrix must have column names “X1”, “X2”, etc. I have tried to assign these column names to my matrix ScoutRSM.mat using a for loop, but I don’t know how to interpret the error message. Suggestions? Thanks, Paul
2010 Aug 09
1
Smart Indexing
Hi all,
Suppose that I've two data frames, a and b say, both containing a column
'id'. While data frame 'a' contains multiple rows sharing the same id,
data frame 'b' contains just one entry per id (i.e. a 1 to n
relationship). For the ease of modeling I now want to generate a new
data frame c, which is basically a copy of data frame 'a' augmented by
the values