[R] Matrix to data frame conversion

Rui Barradas ruipbarradas at sapo.pt
Mon Nov 12 17:55:44 CET 2012


Hello,

I'm unable to reproduce your error, removing the space in "Std. Error" 
and changing "t value" to "t.value" the following read in as data.frame 
with no problems.

x <- read.table(text="
                                 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
", header = TRUE)

# Now assign the original names:
names(x) <- c("Estimate", "Std. Error", "t value", "Pr(>|t|)")

# And see what we have
 > x
                   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


Can you give us your sessionInfo() ?

Rui Barradas
Em 12-11-2012 15:45, PavloEs escreveu:
> 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 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.




More information about the R-help mailing list