1

Is It possible to set colnames of a matrix in R to numeric ? I know that they are character or NULL by default. But, If there's a way to transform them to numeric, It would be so helpful for me.

Any clarification would be welcome.

EDIT

I'll explain myself more clearely :

I have a dataframe that contains two numeric column, for example :

> xy
     x y
[1,] 1 1
[2,] 2 2
[3,] 3 5
[4,] 4 7

> xy = as.data.table(xy)

> xy_cast = dcast(xy, x~y, value.var="y", fun.aggregate=length)

> xy_cast
  x 1 2 5 7
1 1 1 0 0 0
2 2 0 1 0 0
3 3 0 0 1 0
4 4 0 0 0 1

> xy_cast = xy_cast[-1]

> xy_cast
  1 2 5 7
  1 1 0 0 0
  2 0 1 0 0
  3 0 0 1 0
  4 0 0 0 1

> class(colnames(xy_cast))
[1] "character"

As you see my colnames are numbers by they are coerced to character. If I can transform colnames to numeric it would reduce the execution time of the rest of the algorithm.

But, I'm not sure that's possible.

SOLUTION

I tried to treat the problem from another corner, so I thought differently :

which( colnames(df)=="b" )

This R function helped me to go through my colnames by selecting the column number of my column name, which helped me reduce execution time.

The first answer to this question helped me in my problem resolution :

Get the column number in R given the column name

Thank you for responses.

Community
  • 1
  • 1
sarah
  • 229
  • 5
  • 13
  • 2
    You can assign a number but that will be coerced to `character`. From the docs: `either NULL or a character vector of non-zero length` – talat Mar 03 '16 at 20:33
  • @docendodiscimus , that's my problem : my colnames contains numbers but they are automatically coerced to `character`. Is there a solution for this ? – sarah Mar 03 '16 at 20:37
  • 4
    Why exactly do you want them to be numeric? This sounds a bit like [an XY problem](http://meta.stackexchange.com/a/233676). – slamballais Mar 03 '16 at 20:37
  • Use `as.numeric` or `as.integer` on them if you need them for some kind of mathematical operations – talat Mar 03 '16 at 20:38
  • It's still not entirely clear why you want them to be numeric. What kind of algorithm are you applying here? Otherwise, if your column names never change, you can just store them in a separate numeric vector. – slamballais Mar 03 '16 at 21:35
  • the first answer to this question made me thin differently of my problem : http://stackoverflow.com/questions/9277363/get-the-column-number-in-r-given-the-column-name thank you. – sarah Mar 04 '16 at 11:46

1 Answers1

4

You can by default to the columns by their respective number

 df[,3] 

will return third column, or you can also use

 df[,"155"] 

which will return the column with name "155" which is a character. If you're after getting the column name as a numeric, you can extract it and then coerce say

 as.numeric(names(df)[3]) 

will return the name of third column as a numeric, if it is a numerical character. So you can easily go back and forth..

Jan Sila
  • 1,554
  • 3
  • 17
  • 36
  • Thank you @Jan Sila. I'll try to fit `as.numeric(names(df)[3])` to my script if it is possible for my case. – sarah Mar 03 '16 at 21:46
  • No problem if you need further help, comment below. – Jan Sila Mar 03 '16 at 21:49
  • the first answer to this question made me thin differently of my problem : http://stackoverflow.com/questions/9277363/get-the-column-number-in-r-given-the-column-name thank you. – sarah Mar 04 '16 at 11:43