Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use gather function (tidyr package) when column names are numeric?

Tags:

r

tidyr

Is there any way I can use the gather function in the tidyr package to gather columns whose names are numeric characters? For example:

0           1            1.5
0.072008549 0.0722986133 0.0724443032
0.016977539 0.0171147309 0.0171839512
0.007328922 0.0074180263 0.0074631892
0.002660683 0.0027039452 0.0027259732
0.000406033 0.0004143108 0.0004185449
like image 735
jroberayalas Avatar asked Sep 21 '25 03:09

jroberayalas


2 Answers

As well as using backticks, you can refer to the columns by number:

gather(df, key, value, 2:3)
like image 50
alexwhan Avatar answered Sep 22 '25 16:09

alexwhan


When you have numeric names, make sure to escape them using backticks "`". For example gather(d, key, value, `0`)

like image 26
konvas Avatar answered Sep 22 '25 16:09

konvas