'Call a specific column name in 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?

r


Solution 1:[1]

names(df1)[1]

will give you the name of the first column. So too will

names(df1[1])

Neither uses a comma.

Solution 2:[2]

Would colnames(df1)[1] solve the problem?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Henry
Solution 2 hiirulainen