A general question that I have been pursuing for some time but have set aside. When finishing some analysis, I can have multiple matrices that have specific column names. Ideally, I would like to combine these separate matrices for a final output as a csv file. A generic example: Matrix 1 var1A var1B var1C x x x x x x Matrix 2 var2A var2B var2C x x x x x x I would like a final exportable matrix or dataframe or whichever format is most workable. Matrix 3 var1A var1B var1C x x x x x x var2A var2B var2C x x x x x x However, no matter which function I try reports an error that the column names are not the same. Any insights would be appreciated. Thanks as always, Charles [[alternative HTML version deleted]]
If they have the same number of rows, you can use cbind() to create one object
to write out.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live
Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
Charles Determan Jr <deter088 at umn.edu> wrote:
>A general question that I have been pursuing for some time but have set
>aside. When finishing some analysis, I can have multiple matrices that
>have specific column names. Ideally, I would like to combine these
>separate matrices for a final output as a csv file.
>
>A generic example:
>
>Matrix 1
>var1A var1B var1C
>x x x
>x x x
>
>Matrix 2
>var2A var2B var2C
>x x x
>x x x
>
>I would like a final exportable matrix or dataframe or whichever format
>is
>most workable.
>
>Matrix 3
>var1A var1B var1C
>x x x
>x x x
>
>var2A var2B var2C
>x x x
>x x x
>
>However, no matter which function I try reports an error that the
>column
>names are not the same.
>
>Any insights would be appreciated.
>Thanks as always,
>Charles
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
Hello, If you just want to write the matrices to a csv file instead of write.csv you can use write.table with options append=TRUE, sep="," and col.names=FALSE. If you want to merge (rbind) them, you can use code similar to pattern1 <- "(^var)[[:digit:]]+(.*$)" pattern2 <- "(^var)_n_(.*$)" colnames(Matrix1) <- sub(pattern1, "\\1_n_\\2", colnames(Matrix1)) colnames(Matrix2) <- sub(pattern1, "\\1_n_\\2", colnames(Matrix2)) Mat <- rbind(Matrix1, Matrix2) colnames(Mat) <- sub(pattern2, "\\1_\\2", colnames(Mat)) Mat Hope this helps, Rui Barradas Em 26-10-2012 04:51, Charles Determan Jr escreveu:> A general question that I have been pursuing for some time but have set > aside. When finishing some analysis, I can have multiple matrices that > have specific column names. Ideally, I would like to combine these > separate matrices for a final output as a csv file. > > A generic example: > > Matrix 1 > var1A var1B var1C > x x x > x x x > > Matrix 2 > var2A var2B var2C > x x x > x x x > > I would like a final exportable matrix or dataframe or whichever format is > most workable. > > Matrix 3 > var1A var1B var1C > x x x > x x x > > var2A var2B var2C > x x x > x x x > > However, no matter which function I try reports an error that the column > names are not the same. > > Any insights would be appreciated. > Thanks as always, > Charles > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Dennis, This works well and is exactly what I wanted for these matrices. Thank you very much, however, when I would try to export the resulting list it just is bound by columns. Now this isn't so bad for 2 matrices but I hope to apply this where there are many matrices and scrolling down a file such as a 'csv' would be desired. Any thoughts on exporting a list of matrices? Thanks, Charles On Fri, Oct 26, 2012 at 12:00 AM, Dennis Murphy <djmuser@gmail.com> wrote:> Hi: > > You can't represent the same columns with different names in a matrix (or > data frame, for that matter). If you want to preserve variable names in the > various matrices and collect them into one R object, I'd suggest creating a > list, each component of which is a matrix. > > Toy example: > > m1 <- matrix(1:9, nrow = 3, dimnames = list(NULL, paste0("var", 1:3))) > m2 <- matrix(rpois(9, 5), nrow = 3, dimnames = list(NULL, paste0("var", > 4:6))) > L <- list(m1, m2) > names(L) <- paste0("matrix", 1:2) > L > > Dennis > > On Thu, Oct 25, 2012 at 8:51 PM, Charles Determan Jr <deter088@umn.edu>wrote: > >> A general question that I have been pursuing for some time but have set >> aside. When finishing some analysis, I can have multiple matrices that >> have specific column names. Ideally, I would like to combine these >> separate matrices for a final output as a csv file. >> >> A generic example: >> >> Matrix 1 >> var1A var1B var1C >> x x x >> x x x >> >> Matrix 2 >> var2A var2B var2C >> x x x >> x x x >> >> I would like a final exportable matrix or dataframe or whichever format is >> most workable. >> >> Matrix 3 >> var1A var1B var1C >> x x x >> x x x >> >> var2A var2B var2C >> x x x >> x x x >> >> However, no matter which function I try reports an error that the column >> names are not the same. >> >> Any insights would be appreciated. >> Thanks as always, >> Charles >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help@r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> > >[[alternative HTML version deleted]]
Hi,
Not sure how you want the results to look like in .csv file.
If "L" is the list of matrices, you can also use ?sink()
sink("L.csv")
L
?sink()
#L.csv output:
$matrix1
var1 var2 var3
[1 ] 1 4 7
[2 ] 2 5 8
[3 ] 3 6 9
$matrix2
var4 var5 var6
[1 ] 4 7 3
[2 ] 3 4 5
[3 ] 2 3 2
If you want to do some analysis on the .csv file, you can try this:
new3<-readLines("L.csv")
new4<-new3[!grepl("matrix",new3)]
new5<-gsub("\\[.*\\]","",new4)
do.call(rbind,lapply(lapply(strsplit(new5," "),function(x)
paste(unlist(strsplit(x," +")),collapse=" ")),function(x)
unlist(strsplit(x,"+ "))))
#???? [,1]?? [,2]?? [,3]?
#[1,] "var1" "var2" "var3"
#[2,] "1"??? "4"??? "7"??
#[3,] "2"??? "5"??? "8"??
#[4,] "3"??? "6"??? "9"??
#[5,] "var4" "var5" "var6"
#[6,] "4"??? "7"??? "3"??
#[7,] "3"??? "4"??? "5"??
#[8,] "2"??? "3"??? "2"??
#Another way to save the file:
?out_file <- file("L1.csv", open="a")?
for (i in seq_along(L)){
??? write.table(names(L)[i], file=out_file, sep=",",
dec=".",
quote=FALSE, col.names=FALSE, row.names=FALSE)
??? write.table(L[[i]], file=out_file, sep=",", dec=".",
quote=FALSE,
col.names=NA, row.names=TRUE)? #writes the data.frames
}
close(out_file)? #
new1<-readLines("L1.csv")
?new2<-new1[!grepl("matrix",new1)]
?dat2<-do.call(rbind,lapply(strsplit(new2,","),`[`,2:4))
dat2
#??? [,1]?? [,2]?? [,3]?
#[1,] "var1" "var2" "var3"
#[2,] "1"??? "4"??? "7"??
#[3,] "2"??? "5"??? "8"??
#[4,] "3"??? "6"??? "9"??
#[5,] "var4" "var5" "var6"
#[6,] "4"??? "7"??? "3"??
#[7,] "3"??? "4"??? "5"??
#[8,] "2"??? "3"??? "2"??
A.K.
----- Original Message -----
From: Charles Determan Jr <deter088 at umn.edu>
To: Dennis Murphy <djmuser at gmail.com>
Cc: r-help at r-project.org
Sent: Friday, October 26, 2012 10:20 AM
Subject: Re: [R] Merge matrices with different column names
Dennis,
This works well and is exactly what I wanted for these matrices.? Thank you
very much, however, when I would try to export the resulting list it just
is bound by columns.? Now this isn't so bad for 2 matrices but I hope to
apply this where there are many matrices and scrolling down a file such as
a 'csv' would be desired.? Any thoughts on exporting a list of matrices?
Thanks,
Charles
On Fri, Oct 26, 2012 at 12:00 AM, Dennis Murphy <djmuser at gmail.com>
wrote:
> Hi:
>
> You can't represent the same columns with different names in a matrix
(or
> data frame, for that matter). If you want to preserve variable names in the
> various matrices and collect them into one R object, I'd suggest
creating a
> list, each component of which is a matrix.
>
> Toy example:
>
> m1 <- matrix(1:9, nrow = 3, dimnames = list(NULL,
paste0("var", 1:3)))
> m2 <- matrix(rpois(9, 5), nrow = 3, dimnames = list(NULL,
paste0("var",
> 4:6)))
> L <- list(m1, m2)
> names(L) <- paste0("matrix", 1:2)
> L
>
> Dennis
>
> On Thu, Oct 25, 2012 at 8:51 PM, Charles Determan Jr <deter088 at
umn.edu>wrote:
>
>> A general question that I have been pursuing for some time but have set
>> aside.? When finishing some analysis, I can have multiple matrices that
>> have specific column names.? Ideally, I would like to combine these
>> separate matrices for a final output as a csv file.
>>
>> A generic example:
>>
>> Matrix 1
>> var1A? ? ? ? ? var1B? ? ? ? ? var1C
>> x? ? ? ? ? ? ? ? ? ? ? x? ? ? ? ? ? ? x
>> x? ? ? ? ? ? ? ? ? ? ? x? ? ? ? ? ? ? x
>>
>> Matrix 2
>> var2A? ? ? ? ? var2B? ? ? ? ? var2C
>> x? ? ? ? ? ? ? ? ? ? ? x? ? ? ? ? ? ? x
>> x? ? ? ? ? ? ? ? ? ? ? x? ? ? ? ? ? ? x
>>
>> I would like a final exportable matrix or dataframe or whichever format
is
>> most workable.
>>
>> Matrix 3
>> var1A? ? ? ? ? var1B? ? ? ? ? var1C
>> x? ? ? ? ? ? ? ? ? ? ? x? ? ? ? ? ? ? x
>> x? ? ? ? ? ? ? ? ? ? ? x? ? ? ? ? ? ? x
>>
>> var2A? ? ? ? ? var2B? ? ? ? ? var2C
>> x? ? ? ? ? ? ? ? ? ? ? x? ? ? ? ? ? ? x
>> x? ? ? ? ? ? ? ? ? ? ? x? ? ? ? ? ? ? x
>>
>> However, no matter which function I try reports an error that the
column
>> names are not the same.
>>
>> Any insights would be appreciated.
>> Thanks as always,
>> Charles
>>
>>? ? ? ? [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>
??? [[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.