[R] How to convert a matrix to a dataframe and makes its first row be the column name

jim holtman jholtman at gmail.com
Sat Jul 25 10:23:08 CEST 2009


If what you are showing is a 'matrix' (you did not include a
reproducible script, nor did you show an 'str' of the data), then it
is a character matrix.  I took your data, read it in as a dataframe
and then converted it to 'matrix' (x.mat).  You can see below that
this is a character matrix.  If I convert this back to a dataframe
(x.new) this will now have the columns as factors since they were
originally characters.  You can then convert the factors back to
numeric with the 'for' loop.  It would help in the future if you could
provide commented, minimal, self contained, reproducible code so we
don't have to guess at what you have:

> x
  Site A  B  C  D
1    X 1  2  3  4
2    Y 5  6  7  8
3    Z 9 10 11 12
> str(x)
'data.frame':   3 obs. of  5 variables:
 $ Site: Factor w/ 3 levels "X","Y","Z": 1 2 3
 $ A   : int  1 5 9
 $ B   : int  2 6 10
 $ C   : int  3 7 11
 $ D   : int  4 8 12
> x.mat <- as.matrix(x)
> x.mat
     Site A   B    C    D
[1,] "X"  "1" " 2" " 3" " 4"
[2,] "Y"  "5" " 6" " 7" " 8"
[3,] "Z"  "9" "10" "11" "12"
> str(x.mat)
 chr [1:3, 1:5] "X" "Y" "Z" "1" "5" "9" " 2" " 6" "10" " 3" " 7" "11" " 4" ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:5] "Site" "A" "B" "C" ...
> x.new <- as.data.frame(x.mat)
> x.new
  Site A  B  C  D
1    X 1  2  3  4
2    Y 5  6  7  8
3    Z 9 10 11 12
> str(x.new)
'data.frame':   3 obs. of  5 variables:
 $ Site: Factor w/ 3 levels "X","Y","Z": 1 2 3
 $ A   : Factor w/ 3 levels "1","5","9": 1 2 3
 $ B   : Factor w/ 3 levels " 2"," 6","10": 1 2 3
 $ C   : Factor w/ 3 levels " 3"," 7","11": 1 2 3
 $ D   : Factor w/ 3 levels " 4"," 8","12": 1 2 3
> for (i in 2:ncol(x.new)) x.new[[i]] <- as.numeric(as.character(x.new[[i]]))
> x.new
  Site A  B  C  D
1    X 1  2  3  4
2    Y 5  6  7  8
3    Z 9 10 11 12
> str(x.new)
'data.frame':   3 obs. of  5 variables:
 $ Site: Factor w/ 3 levels "X","Y","Z": 1 2 3
 $ A   : num  1 5 9
 $ B   : num  2 6 10
 $ C   : num  3 7 11
 $ D   : num  4 8 12
>


On Fri, Jul 24, 2009 at 2:56 PM, yabado<alexhsi1123 at hotmail.com> wrote:
>
> Hi
>
> I want to convert a matrix to a dataframe.
>
> The matrix is like this
>
> Site   A     B     C     D
>
> X       1     2     3     4
>
> Y       5     6     7     8
>
> Z       9     10    11   12
>
>
> The converted dataframe need to have the first row as column names
> and the values of each column (1, 2, 3, 4; ......12;) be numeric, not
> factors.
>
> I used data.frame(), but it gave me auto-assigned column names (like X1, X2,
> X3, X4)
> and put the alphabet and numbers together as factors of each column.
>
> Is there a way to correct it?
>
> Thank you
>
> --
> View this message in context: http://www.nabble.com/How-to-convert-a-matrix-to-a-dataframe-and-makes-its-first-row-be-the-column-name-tp24650062p24650062.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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list