similar to: is there a way to let R do smart matrix-vector operation?

Displaying 20 results from an estimated 10000 matches similar to: "is there a way to let R do smart matrix-vector operation?"

2004 Feb 02
3
mvrnorm problem
I am trying to simulate draws from a multivariate normal using mvrnorm, and am getting the following error message: Error in mu + eS$vectors %*% diag(sqrt(pmax(ev, 0)), p) %*% t(X) : non-conformable arrays I do not understand why I am getting this message, since the vector of means I am giving to the function is 13 by 1 and the variance matrix I am giving to the function is 13
2011 Nov 04
6
Matrix element-by-element multiplication
is there a way to do element-by-element multiplication as in Gauss and MATLAB, as shown below? Thanks. --- a 1.0000000 2.0000000 3.0000000 x 1.0000000 2.0000000 3.0000000 2.0000000 4.0000000 6.0000000 3.0000000 6.0000000 9.0000000 a.*x 1.0000000 2.0000000 3.0000000 4.0000000
2004 Mar 31
2
array addition doesn't recycle!
Hi, I have noticed the following: > a <- array(1:4, c(2, 2)) > A <- array(1:4, c(2,2,2)) > A + a Error in A + a : non-conformable arrays It works with a matrix + a vector, why doesn't it work with arrays? Am I missing something? How would you do the above operation efficiently (ie I need to add a matrix to each "plane" of 3-dim array)? At the moment I am using
2024 Feb 29
2
[External] converting MATLAB -> R | element-wise operation
I decided to do a direct comparison of transpose and sweep. library(microbenchmark) NN <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, byrow = TRUE) # Example matrix lambda <- c(2, 3, 4) # Example vector colNN <- t(NN) microbenchmark( sweep = sweep(NN, 2, lambda, "/"), transpose = t(t(NN)/lambda), colNN = colNN/lambda ) Unit: nanoseconds expr min lq
2024 Feb 27
4
converting MATLAB -> R | element-wise operation
So, trying to convert a very long, somewhat technical bit of lin alg MATLAB code to R. Most of it working, but raninto a stumbling block that is probaably simple enough for someone to explain. Basically, trying to 'line up' MATLAB results from an element-wise division of a matrix by a vector with R output. Here is a simplified version of the MATLAB code I'm translating: NN = [1,
2024 Feb 27
2
converting MATLAB -> R | element-wise operation
Why anything but sweep? The fundamental data type in Matlab is a matrix... they don't have vectors, they have Nx1 matrices and 1xM matrices. Vectors don't have any concept of "row" vs. "column". Straight division is always elementwise with recycling as needed, and matrices are really vectors in row-major order: 1 2 3 4 5 6 is really 1 4 2 5 3 6 and when you do
2024 Feb 27
2
[External] converting MATLAB -> R | element-wise operation
> t(t(NN)/lambda) [,1] [,2] [,3] [1,] 0.5 0.6666667 0.75 [2,] 2.0 1.6666667 1.50 > R matrices are column-based. MATLAB matrices are row-based. > On Feb 27, 2024, at 14:54, Evan Cooch <evan.cooch at gmail.com> wrote: > > So, trying to convert a very long, somewhat technical bit of lin alg > MATLAB code to R. Most of it working, but raninto a stumbling block
2011 Jan 28
3
sapply puzzlement
Hi, I have this data.frame with two variables in it, > z V1 V2 1 10 8 2 NA 18 3 9 7 4 3 NA 5 NA 10 6 11 12 7 13 9 8 12 11 and a vector of means, > means <- apply(z, 2, function (col) mean(na.omit(col))) > means V1 V2 9.666667 10.714286 My intention was substracting means from z, so instictively I tried > z-means V1 V2 1 0.3333333
2003 Sep 05
4
Basic Dummy Variable Creation
Hi There, While looking through the mailing list archive, I did not come across a simple minded example regarding the creation of dummy variables. The Gauss language provides the command "y = dummydn(x,v,p)" for creating dummy variables. Here: x = Nx1 vector of data to be broken up into dummy variables. v = Kx1 vector specifying the K-1 breakpoints p = positive integer in the range
2009 Aug 09
1
binary operators that implement row and column sweeps of matrices by vectors
Submitted for perusal, comment, improvements, and/or critique. The presentation is in 3 sections: motivation, code, and comment. Motivation: As a new-comer to R from matrix oriented Gauss and Mata, I miss the tools for using a vector (and operator) to ‘sweep’ across a matrix. Here is how these work. If M is I rows by J columns, then one entry corresponding to
2017 Apr 18
3
Crash after (wrongly) applying product operator on object from LIMMA package
Hi, this is a problem that occurs in the presence of two libraries (limma, xlsx) and leads to a crash of R. The problematic code is the wrong application of sweep or the product ("*") function on an LIMMA MAList object. To my knowledge, limma does not define a "*" method for MAList objects. If only LIMMA is loaded but not package xlsx, the code does not crash but rather
2024 Feb 28
2
converting MATLAB -> R | element-wise operation
On Tue, 27 Feb 2024 13:51:25 -0800 Jeff Newmiller via R-help <r-help at r-project.org> wrote: > The fundamental data type in Matlab is a matrix... they don't have > vectors, they have Nx1 matrices and 1xM matrices. Also known as column vectors and row vectors. :) > Vectors don't have any concept of "row" vs. "column". They do in (numerical) linear
2011 Oct 09
2
Substract "previous" element
Dear all, I have a matrix with data and I want to substract from every value the previous element. Let's assume that my vector(matrix) is c<-(1,2,3,4,5) I want to get remove_previous c<-(0,1,2,3,4). How I can do that efficiently in R? I would like to thank you in advance for your help B.R Alex [[alternative HTML version deleted]]
2017 Apr 19
2
Crash after (wrongly) applying product operator on S4 object that derives from list
Dear Hilmar Perhaps this gives an indication of why the infinite recursion happens: ## after calling `*` on ma and a matrix: > showMethods(classes=class(ma), includeDefs=TRUE, inherited = TRUE) Function: * (package base) e1="FOOCLASS", e2="matrix" (inherited from: e1="vector", e2="structure") (definition from function "Ops")
2008 May 21
3
Converting a 'difftime' to integer - How to???
I want to find the DOY (Day of Year) of some dates. I think to substract the date 1. January from the data to achive this. Something like: > d <- as.Date("2006-03-13") - as.Date("2006-01-01") +1 > d Time difference of 72 days So far so good. But d is a 'difftime' object. How do I get an Integer value from that? I tried severel things, incuding the
2017 Apr 24
2
Crash after (wrongly) applying product operator on object from LIMMA package
Hi Hilmar, weird. The memory problem seems be due to recursion (my R, version 3.3.3, says: Error: evaluation nested too deeply: infinite recursion / options(expressions=)?, just write traceback() to see how it happens), but why does it segfault with xlsx? Nb xlsx is the culprit: neither rJava nor xlsxjars cause the problem. On the other hand, quick googling for r+xlsx+segfault returns tons of
2003 Jul 14
2
Subsetting a matrix
I'd welcome some comments or advice regarding the situation described below. The following illustrates what seems to me to be an inconsistency in the behaviour of matrix subsetting: > Z<-matrix(c(1.1,2.1,3.1,1.2,2.2,3.2,1.3,2.3,3.3),nrow=3) > Z [,1] [,2] [,3] [1,] 1.1 1.2 1.3 [2,] 2.1 2.2 2.3 [3,] 3.1 3.2 3.3 > dim(Z) [1] 3 3 >
2011 Mar 21
3
How to substract a valur from dataframe with condition
Hello All, I need help with my dataframe, it is big but here I am using a small table as an example. My dataframe df looks like: X1 X2 X3 1 2011-02 0.00 96.00 2 2011-02 0.00 2.11 3 2011-02 2.00 3.08 4 2011-02 0.06 2.79 5 2011-02 0.00 96.00 6 2011-02 0.00 97.00 7 2011-02 0.08 2.23 I want values in columns X2 and X3 to be checked if they are greater than
2012 Jul 31
1
TimeStamps to seconds
Dear all, I am having a matrix that stores data information in the following format. > roofPart1$TimeStamps[1:5,]      [,1] [,2] [,3] [,4] [,5]   [,6] [1,] 2011    7   21   15   25 20.609 [2,] 2011    7   21   15   25 23.265 [3,] 2011    7   21   15   25 26.000 [4,] 2011    7   21   15   25 28.671 [5,] 2011    7   21   15   25 31.343 I would like to be able to report some simple statistics
2003 Sep 02
1
convert character to POSIXct
Dear list-members, I would like to calculate the difference between two points in time. To convert a 'time (GMT)'-character with the format "1/1/1999 01:01:01" into an object of class "POSIXct"', I first use the strptime() as suggested in the details help(as.POSIXct). e.g. starttime<-strptime("1/1/1999 01:01:01",format="%d/%m/%Y %H:%M:%S")