Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a specific column name in R

Tags:

r

colnames gives me the column names for a whole dataframe. Is there any way to get the name of one specified column. i would need this for naming labels when plotting data in ggplot.

So say my data is like this:

df1 <- data.frame(a=sample(1:50,10), b=sample(1:50,10), c=sample(1:50,10))

I would need something like paste(colnames(df1[,1])) which obviously won't work. any ideas?

like image 819
Joschi Avatar asked Dec 07 '22 09:12

Joschi


1 Answers

you call the name like this:

colnames(df1)[1] 
# i.e. call the first element of colnames not colnames of the first vector

however by removing your comma e.g.:

colnames(df1[1])

you can also call the names, becauseusing only [x] not [,x] or [[x]] keeps the data.frame structure not reducing to a vector unlike $x and [,x]

like image 181
user1317221_G Avatar answered Jan 02 '23 13:01

user1317221_G