Displaying 20 results from an estimated 10000 matches similar to: "Stacking matrix columns"
2023 Aug 06
2
Stacking matrix columns
You could also do
dim(x) <- c(length(x), 1)
On Sat, Aug 5, 2023, 20:12 Steven Yen <styen at ntu.edu.tw> wrote:
> I wish to stack columns of a matrix into one column. The following
> matrix command does it. Any other ways? Thanks.
>
> > x<-matrix(1:20,5,4)
> > x
> [,1] [,2] [,3] [,4]
> [1,] 1 6 11 16
> [2,] 2 7 12 17
> [3,]
2023 Aug 06
1
Stacking matrix columns
Stacking columns of a matrix is a standard operation in multilinear
algebra, usually written as the operator vec().
I checked to see if there is an R package that deals with multilinear
algebra. I found rTensor, which has a function vec().
So, yet another way to accomplish what you want would be:
> library(rTensor)
> vec(as.tensor(x))
Eric
On Sun, Aug 6, 2023 at 5:05?AM Bert Gunter
2023 Aug 06
1
Stacking matrix columns
Eric,
I am not sure your solution is particularly economical albeit it works for arbitrary arrays of any dimension, presumably. But it seems to involve converting a matrix to a tensor just to undo it back to a vector. Other solutions offered here, simply manipulate the dim attribute of the data structure.
Of course, the OP may have uses in mind which the package might make easier. We often get
2023 Aug 06
1
Stacking matrix columns
Or just dim(x) <- NULL.
(as matrices in base R are just vectors with a dim attribute stored in
column major order)
ergo:
> x
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
> x<- 1:20 ## a vector
> is.matrix(x)
[1] FALSE
> dim(x) <- c(5,4)
> is.matrix(x)
[1] TRUE
> attributes(x)
$dim
[1] 5 4
> ## in painful and unnecessary detail as dim() should
2023 Aug 06
1
Stacking matrix columns
Avi,
I was not trying to provide the most economical solution. I was trying
to anticipate that people (either the OP or others searching for how
to stack columns of a matrix) might be motivated by calculations in
multilinear algebra, in which case they might be interested in the
rTensor package.
On Sun, Aug 6, 2023 at 6:16?PM <avi.e.gross at gmail.com> wrote:
>
> Eric,
>
> I
2023 Aug 06
1
Stacking matrix columns
I wish to stack columns of a matrix into one column. The following
matrix command does it. Any other ways? Thanks.
> x<-matrix(1:20,5,4)
> x
???? [,1] [,2] [,3] [,4]
[1,]??? 1??? 6?? 11?? 16
[2,]??? 2??? 7?? 12?? 17
[3,]??? 3??? 8?? 13?? 18
[4,]??? 4??? 9?? 14?? 19
[5,]??? 5?? 10?? 15?? 20
> matrix(x,ncol=1)
????? [,1]
?[1,]??? 1
?[2,]??? 2
?[3,]??? 3
?[4,]??? 4
?[5,]??? 5
2023 Apr 06
4
R does not run under latest RStudio
I updated to latest RStudio (RStudio-2023.03.0-386.exe) but
R would not run. Error message:
Error Starting R
The R session failed to start.
RSTUDIO VERSION
RStudio 2023.03.0+386 "Cherry Blossom " (3c53477a, 2023-03-09) for Windows
[No error available]
I also tried RStudio 2022.12.0+353 --- same problem.
I then tried another older version of RStudio (not sure version
as I changed file
2024 Feb 29
2
Initializing vector and matrices
You could declare a matrix much larger than you intend to use. This works with a few megabytes of data. It is not very efficient, so scaling up may become a problem.
m22 <- matrix(NA, 1:600000, ncol=6)
It does not work to add a new column to the matrix, as in you get an error if you try m22[ , 7] but convert to data frame and add a column
m23 <- data.frame(m22)
m23$x7 <- 12
The only
2024 Aug 09
3
If loop
"Or use <<- assignment I think. (I usually return, but return can only
return one object and I think you want two or more"
You can return any number of objects by putting them in a list and
returning the list.
Use of "<<-" is rarely a good idea in R.
-- Bert
On Fri, Aug 9, 2024 at 1:53?AM CALUM POLWART <polc1410 at gmail.com> wrote:
>
> OK. The fact
2023 Feb 12
2
Removing variables from data frame with a wile card
x["V2"]
is more efficient than using drop=FALSE, and perfectly normal syntax (data frames are lists of columns). I would ignore the naysayers, or put a comment in if you want to accelerate their uptake.
As I understand it, one of the main reasons tibbles exist is because of drop=TRUE. List-slice (single-dimension) indexing works equally well with both standard and tibble types of data
2024 Mar 26
1
Printout and saved results
I just like the subroutine to spit out results (Mean, Std.dev, etc.) and
also be able to access the results for further processing, i.e.,
v$Mean
v$Std.dev
On 3/26/2024 11:24 AM, Richard O'Keefe wrote:
> Not clear what you mean by "saved".
> If you call a function and the result is printed, the result is
> remembered for a wee while in
> the variable .Last.value, so
2024 Mar 26
2
Printout and saved results
How can I have both printout and saved results at the same time.
The subroutine first return "out" and the printout gets printed, but not
saved.
I then run the "invisible" line. Results got saved and accessible but no
printout.
How can I have both printout and also have the results saved? Thank you!
> dstat4 <- function(data,digits=3){
+?? Mean??? <-
2023 Jan 14
2
Removing variables from data frame with a wile card
Thanks to all. Very helpful.
Steven from iPhone
> On Jan 14, 2023, at 3:08 PM, Andrew Simmons <akwsimmo at gmail.com> wrote:
>
> ?You'll want to use grep() or grepl(). By default, grep() uses extended
> regular expressions to find matches, but you can also use perl regular
> expressions and globbing (after converting to a regular expression).
> For example:
>
2024 Feb 19
5
Looping
I need to read csv files repeatedly, named data1.csv, data2.csv,? data24.csv, 24 altogether. That is,
data<-read.csv(?data1.csv?)
?
data<-read.csv(?data24.csv?)
?
Is there a way to do this in a loop? Thank you.
Steven from iPhone
[[alternative HTML version deleted]]
2024 Mar 26
1
Printout and saved results
Your desire is not unusual among novices... but it is really not a good idea for your function to be making those decisions. Look at how R does things:
The lm function prints nothing... it returns an object containing the result of a linear regression. If you happen to call it directly from the R command prompt and don't assign it to a variable, then the command interpreter notices that
2024 Aug 11
3
Printing
Thanks. Will try it.
Have not tried it but I think the following may work:
out$results<-NULL
out$results$ei<-ap
out$results$vi<-vap
All I need is printing by returning out (unless I turn it off). And,
retrieve ap and vap as needed as shown above. Guess I need to read more
about invisible.
On 8/11/2024 10:09 PM, Rui Barradas wrote:
> ?s 09:51 de 11/08/2024, Steven Yen escreveu:
2023 Jan 14
2
Removing variables from data frame with a wile card
I have a data frame containing variables "yr3",...,"yr28".
How do I remove them with a wild card----something similar to "del yr*"
in Windows/doc? Thank you.
> colnames(mydata)
? [1] "year"?????? "weight"???? "confeduc"?? "confothr" "college"
? [6] ...
?[41] "yr3"??????? "yr4"???????
2024 Feb 29
1
Initializing vector and matrices
x <- numeric(0)
for (...) {
x[length(x)+1] <- ...
}
works.
You can build a matrix by building a vector one element at a time this way,
and then reshaping it at the end. That only works if you don't need it to be
a matrix at all times.
Another approach is to build a list of rows. It's not a matrix, but a list of
rows can be a *ragged* matrix with rows of varying length.
On Wed,
2024 Mar 26
1
Printout and saved results
Just FYI, the R interpreter typically saves the last value returned briefly
in a variable called .Last.value that can be accessed before you do anything
else.
> sin(.5)
[1] 0.4794255
> temp <- .Last.value
> print(temp)
[1] 0.4794255
> sin(.666)
[1] 0.6178457
> .Last.value
[1] 0.6178457
> temp
[1] 0.4794255
> invisible(sin(0.2))
> .Last.value
[1] 0.1986693
So perhaps if
2018 Mar 14
1
Documenting R package with Rd file
I have trouble documenting an R package. In my .Rd file (sixth line below), I have
uhat<-m%*%y
but when the package is built (successfully), the matrix multiplication part does not show up in the documentation. The line become (missing %*% y)
uhat<-m
===
\examples{
x<-c(1,2,3,4,5)
y<-c(1,1,2,2,4)
x<-cbind(1,x)
m<-mmat(x)
uhat<-m%*%y
dstat(uhat)
}
?--
styen at ntu.edu.tw