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.

6
  • 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 Commented Mar 3, 2016 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 ? Commented Mar 3, 2016 at 20:37
  • 4
    Why exactly do you want them to be numeric? This sounds a bit like an XY problem. Commented Mar 3, 2016 at 20:37
  • Use as.numeric or as.integer on them if you need them for some kind of mathematical operations Commented Mar 3, 2016 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. Commented Mar 3, 2016 at 21:35

1 Answer 1

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..

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you @Jan Sila. I'll try to fit as.numeric(names(df)[3]) to my script if it is possible for my case.
No problem if you need further help, comment below.
the first answer to this question made me thin differently of my problem : stackoverflow.com/questions/9277363/… thank you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.