Paul Miller
2013-Apr-09 20:52 UTC
[R] Converting matrix to data frame without losing an assigned dimname
Hello All, Would like to be able to convert a matrix to a dataframe without losing an assigned dimname. Here is an example that should illustrate what I'm talking about. tableData <- state.x77[c(7, 38, 20, 46), c(7, 1, 8)] names(dimnames(tableData)) <- c("State", "") tableData State Frost Population Area Connecticut 139 3100 4862 Pennsylvania 126 11860 44966 Maryland 101 4122 9891 Virginia 85 4981 39780 tableData <- as.data.frame(tableData) tableData Frost Population Area Connecticut 139 3100 4862 Pennsylvania 126 11860 44966 Maryland 101 4122 9891 Virginia 85 4981 39780 Notice how "State" gets removed when converting to a dataframe. How can I get a dataframe with a separate column called "State" instead of having the state become the row.names? I can think of an ugly way to do it but suspect there must be something more elegant. Thanks, Paul
Gabor Grothendieck
2013-Apr-09 21:42 UTC
[R] Converting matrix to data frame without losing an assigned dimname
On Tue, Apr 9, 2013 at 4:52 PM, Paul Miller <pjmiller_57 at yahoo.com> wrote:> Hello All, > > Would like to be able to convert a matrix to a dataframe without losing an assigned dimname. > > Here is an example that should illustrate what I'm talking about. > > tableData <- state.x77[c(7, 38, 20, 46), c(7, 1, 8)] > names(dimnames(tableData)) <- c("State", "") > tableData > > State Frost Population Area > Connecticut 139 3100 4862 > Pennsylvania 126 11860 44966 > Maryland 101 4122 9891 > Virginia 85 4981 39780 > > tableData <- as.data.frame(tableData) > tableData > > Frost Population Area > Connecticut 139 3100 4862 > Pennsylvania 126 11860 44966 > Maryland 101 4122 9891 > Virginia 85 4981 39780 > > Notice how "State" gets removed when converting to a dataframe. How can I get a dataframe with a separate column called "State" instead of having the state become the row.names? I can think of an ugly way to do it but suspect there must be something more elegant. >Try this:> library(plyr) > adply(tableData, 1, c)State Frost Population Area 1 Connecticut 139 3100 4862 2 Pennsylvania 126 11860 44966 3 Maryland 101 4122 9891 4 Virginia 85 4981 39780 -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Rolf Turner
2013-Apr-09 22:24 UTC
[R] Converting matrix to data frame without losing an assigned dimname
Interesting. The problem is that only ***arrays*** are allowed to have a dimnames attribute, and data frames are not arrays (they are lists). The only way that I can see to achieve the effect that you want is to make the row names into the first column of your data frame and wipe out the row names. That is probably the "ugly" procedure that you've already thought of. You could automate the procedure to some extent: fixUpRowNames <- function(X,name) { X <- as.data.frame(X) X <- cbind(rownames(X),X) names(X)[1] <- name rownames(X) <- NULL X } Then do: tableData <- state.x77[c(7, 38, 20, 46), c(7, 1, 8)] newTD <- fixUpRowNames(tableData,"State") And then all is (more or less) in harmony. Best that I can come up with. cheers, Rolf Turner On 04/10/2013 08:52 AM, Paul Miller wrote:> Hello All, > > Would like to be able to convert a matrix to a dataframe without losing an assigned dimname. > > Here is an example that should illustrate what I'm talking about. > > tableData <- state.x77[c(7, 38, 20, 46), c(7, 1, 8)] > names(dimnames(tableData)) <- c("State", "") > tableData > > State Frost Population Area > Connecticut 139 3100 4862 > Pennsylvania 126 11860 44966 > Maryland 101 4122 9891 > Virginia 85 4981 39780 > > tableData <- as.data.frame(tableData) > tableData > > Frost Population Area > Connecticut 139 3100 4862 > Pennsylvania 126 11860 44966 > Maryland 101 4122 9891 > Virginia 85 4981 39780 > > Notice how "State" gets removed when converting to a dataframe. How can I get a dataframe with a separate column called "State" instead of having the state become the row.names? I can think of an ugly way to do it but suspect there must be something more elegant.
arun
2013-Apr-09 23:00 UTC
[R] Converting matrix to data frame without losing an assigned dimname
Hi, library(plyr) library(reshape2) ?df2<-mutate(dcast(melt(tableData),State~Var2), State=factor(State,levels=rownames(tableData))) dfNew<-df2[as.numeric(df2$State),c(1,3:4,2)] dfNew$State<- as.character(dfNew$State) ?dfNew #???????? State Frost Population? Area #1? Connecticut?? 139?????? 3100? 4862 #3 Pennsylvania?? 126????? 11860 44966 #2???? Maryland?? 101?????? 4122? 9891 #4???? Virginia??? 85?????? 4981 39780 A.K. ----- Original Message ----- From: Paul Miller <pjmiller_57 at yahoo.com> To: r-help at r-project.org Cc: Sent: Tuesday, April 9, 2013 4:52 PM Subject: [R] Converting matrix to data frame without losing an assigned dimname Hello All, Would like to be able to convert a matrix to a dataframe without losing an assigned dimname. Here is an example that should illustrate what I'm talking about. tableData <- state.x77[c(7, 38, 20, 46), c(7, 1, 8)] names(dimnames(tableData)) <- c("State", "") tableData State? ? ? ? ? Frost Population? Area ? Connecticut? ? 139? ? ? 3100? 4862 ? Pennsylvania? 126? ? ? 11860 44966 ? Maryland? ? ? 101? ? ? 4122? 9891 ? Virginia? ? ? ? 85? ? ? 4981 39780 tableData <- as.data.frame(tableData) tableData ? ? ? ? ? ? Frost Population? Area Connecticut? ? 139? ? ? 3100? 4862 Pennsylvania? 126? ? ? 11860 44966 Maryland? ? ? 101? ? ? 4122? 9891 Virginia? ? ? ? 85? ? ? 4981 39780 Notice how "State" gets removed when converting to a dataframe. How can I get a dataframe with a separate column called "State" instead of having the state become the row.names? I can think of an ugly way to do it but suspect there must be something more elegant. Thanks, Paul ______________________________________________ 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.
Paul Miller
2013-Apr-10 15:44 UTC
[R] Converting matrix to data frame without losing an assigned dimname
Hi David, Gabor, Arun, and Rolf, ? Thanks for your replies to my question. Very interesting and very helpful. I particularly liked the one-line solutions proposed by David and Gabor.?I had a suspicion that this conversion could be done using only a minimal amount of code. ? For what I'm doing,?it's helpful to have a "State" column that is character. For that reason, David's solution might be the best in this particular instance. ? Thanks again everyone. ? Paul ?