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.