HI, I also have no trouble in reading the example data you gave, which BTW do not have any duplicate rownames. I guess there must be some rows in the original dataset that must be repeated. For example: comb_model<-read.table(text=" Estimate Std.Error tvalue Pr(>|t|) (Intercept) 3.593793 0.012537 286.656 0.000000000 as.factor(dow)1 0.027378 0.007348 3.726 0.000196109 as.factor(dow)2 0.034211 0.007348 4.656 0.000003288 as.factor(dow)3 0.013481 0.007393 1.823 0.068281371 as.factor(dow)4 0.020708 0.007358 2.814 0.004903774 as.factor(dow)5 0.030164 0.007327 4.117 0.000038886 as.factor(dow)6 0.021723 0.007327 2.965 0.003039448 as.factor(dow)6 0.021723 0.007327 2.965 0.003039448 as.factor(fluepi) 0.044272 0.014697 3.012 0.002601912 o3max 0.003441 0.001723 1.998 0.045785798 o3max 0.004881 0.00281 1.885 0.00540889 ",sep="",header=TRUE,row.names=NULL)
#here there are two types of repeats, rownames only repeated (o3max) and whole rows repeated (as.factor(dow)6) #If I wanted to delete the repeated rows. comb_model2<-unique(comb_model[duplicated(comb_model)|duplicated(comb_model,fromLast=TRUE),]) comb_model3<-rbind(comb_model[!(duplicated(comb_model)|duplicated(comb_model,fromLast=TRUE)),],comb_model2) mat2<-as.matrix(comb_model3[,2:5]) #matrix can have repeated rownames row.names(mat2)<-comb_model3[,1] #changing repeated rownames to unique rownames mat3<-mat2[duplicated(rownames(mat2))|duplicated(rownames(mat2),fromLast=TRUE),] rownames(mat3)<-paste(rownames(mat3),letters[1:nrow(mat3)],sep="") mat4<-mat2[!(duplicated(rownames(mat2))|duplicated(rownames(mat2),fromLast=TRUE)),] library(plyr) comb_model4<-join(as.data.frame(mat4),as.data.frame(mat3),type="full") rownames(comb_model4)<-c(rownames(mat4),rownames(mat3)) #instead of join(), you could use ?merge() #comb_model4<-merge(as.data.frame(mat4),as.data.frame(mat3),all=TRUE,sort=FALSE) #rownames(comb_model4)<-c(rownames(mat4),rownames(mat3))colnames(comb_model4)[4]<-"Pr(>|t|)" comb_model4 # Estimate Std.Error tvalue Pr(>|t|) #(Intercept) 3.593793 0.012537 286.656 0.000000000 #as.factor(dow)1 0.027378 0.007348 3.726 0.000196109 #as.factor(dow)2 0.034211 0.007348 4.656 0.000003288 #as.factor(dow)3 0.013481 0.007393 1.823 0.068281371 #as.factor(dow)4 0.020708 0.007358 2.814 0.004903774 #as.factor(dow)5 0.030164 0.007327 4.117 0.000038886 #as.factor(fluepi) 0.044272 0.014697 3.012 0.002601912 #as.factor(dow)6 0.021723 0.007327 2.965 0.003039448 #o3maxa 0.003441 0.001723 1.998 0.045785798 #o3maxb 0.004881 0.002810 1.885 0.005408890 #Another way will be to use ?rbind() comb_model4<-rbind(as.data.frame(mat4),as.data.frame(mat3)) # no need colnames(comb_model4)[4]<-"Pr(>|t|)" A.K. ----- Original Message ----- From: PavloEs <nici...@gmail.com> To: r-help@r-project.org Cc: Sent: Monday, November 12, 2012 10:45 AM Subject: [R] Matrix to data frame conversion I have a matrix which I wanted to convert to a data frame. As I could not succeed and resorted to export to csv and reimport it again. Why did I fail in the attempt and how can I achieve what I wanted without this roundabouts? The original matrix: > str(comb_model0) num [1:90, 1:4] 3.5938 0.0274 0.0342 0.0135 0.0207 ... - attr(*, "dimnames")=List of 2 ..$ : chr [1:90] "(Intercept)" "as.factor(dow)1" "as.factor(dow)2" "as.factor(dow)3" ... ..$ : chr [1:4] "Estimate" "Std. Error" "t value" "Pr(>|t|)" > dim(comb_model0) [1] 90 4 > class(comb_model0) [1] "matrix" > > > head(comb_model0,10) Estimate Std. Error t value Pr(>|t|) (Intercept) 3.593793 0.012537 286.656 0.000000000 as.factor(dow)1 0.027378 0.007348 3.726 0.000196109 as.factor(dow)2 0.034211 0.007348 4.656 0.000003288 as.factor(dow)3 0.013481 0.007393 1.823 0.068281371 as.factor(dow)4 0.020708 0.007358 2.814 0.004903774 as.factor(dow)5 0.030164 0.007327 4.117 0.000038886 as.factor(dow)6 0.021723 0.007327 2.965 0.003039448 as.factor(fluepi) 0.044272 0.014697 3.012 0.002601912 o3max 0.003441 0.001723 1.998 0.045785798 One of the many conversion attempts: > xx<-as.data.frame(comb_model0) > xx Error in data.frame(Estimate = c(" 3.59379258", " 0.02737812", " 0.03421067", : duplicate row.names: (Intercept), as.factor(dow)1, as.factor(dow)2, as.factor(dow)3, as.factor(dow)4, as.factor(dow)5, as.factor(dow)6, as.factor(fluepi) write.csv(comb_model0, "test.csv") test<-read.csv("test.csv") class(test) [1] "data.frame" > dim(test) [1] 90 5 head(test,9) X Estimate Std..Error t.value Pr...t.. 1 (Intercept) 3.59379258 0.012537 286.65611 0.000000000 2 as.factor(dow)1 0.02737812 0.007348 3.72596 0.000196109 3 as.factor(dow)2 0.03421067 0.007348 4.65575 0.000003288 4 as.factor(dow)3 0.01348142 0.007393 1.82343 0.068281371 5 as.factor(dow)4 0.02070761 0.007358 2.81419 0.004903774 6 as.factor(dow)5 0.03016411 0.007327 4.11665 0.000038886 7 as.factor(dow)6 0.02172334 0.007327 2.96476 0.003039448 8 as.factor(fluepi) 0.04427231 0.014697 3.01232 0.002601912 9 o3max 0.00344111 0.001723 1.99772 0.045785798 -- View this message in context: http://r.789695.n4.nabble.com/Matrix-to-data-frame-conversion-tp4649311.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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. ______________________________________________ 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.