0

I am trying to select a column on a dataframe but I need to keep its column name. Imagine it as a one column dataframe. For example, this is what I need to accomplish:

df <- data.frame(col_a = c(1,2,3), col_b = c(5,2,8))
x <- df$col_a
colnames(x)
col_a # THIS VALUE IS WHAT I NEED

If you ask R for a colname of a vector, it simply returns NULL. Makes sense, but what if it was a column of a dataframe instead of a simple vector?

Why do I need this? I'm writing a function with ggplot2 and I need to specify in a lab which column is it using to plot the results. I would rather NOT use a string text on my function to select the columns with the function if possible because it is much easier to select variables from a dataframe when iterating.

So basically function(target, values) {... where the arguments are both these kind of "vectors/one-column-dataframes" and the input would be like my_function(target = df$col_a, values = df$col_b) so I could get the colnames(target) and colnames(values). Yes?

Any ideas? Thanks!

Bernardo
  • 461
  • 7
  • 20
  • See http://adv-r.had.co.nz/Subsetting.html#subassignment for more information on simplifying vs. preserving. – tyluRp Jul 13 '18 at 21:14
  • `df["col_a"]` (no commas) works as well. – r2evans Jul 13 '18 at 21:57
  • 1
    I think you are missing the important part of the question here which is the function where you want to use this value and what exactly you are passing to it. A function can get the name of the variable passed to it: https://stackoverflow.com/questions/24309910/how-to-get-name-of-variable-in-r-substitute but a variable can't keep track of it's own name (if that makes any sense). – MrFlick Jul 13 '18 at 23:35

2 Answers2

2

a data frame originally is a list so you can subset using $. However, to preserve the dataframe characteristics when creating a new column (list) you need to use drop=F

x<- df[,"col_a",drop=F]
colnames(x)
[1] "col_a"
A. Suliman
  • 12,923
  • 5
  • 24
  • 37
  • Yep, I get it @a-suliman but the idea is to NOT write all that "code" to simply get the vectors into the function. Know what I mean? Thanks for the super fast reply – Bernardo Jul 13 '18 at 21:02
1

data.frame is a list and columns are its elements. If you extract a column and want to keep its name too you can not use $, if you have to get the column name as well along with column values use [

 df
 #   col_a col_b
 # 1     1     5
 # 2     2     2
 # 3     3     8

If extracting by giving column name ($), only values would come, if extracting by column position ([), it gives column name + values in that column. This [ can hold element positions by number or name, but preserves name

$

 df$col_a
 # [1] 1 2 3

[

 df[1]
 #   col_a
 # 1     1
 # 2     2
 # 3     3


Behaviour of List (As we said above data.frame is a list)

 my_list <- list(a = 1:10, b = 1:3, c= 4)

To extract values in element a

Element Name - Gives Only values

 my_list$a
 # [1]  1  2  3  4  5  6  7  8  9 10

To extract values of element in position 1

Element Position - Gives Element Name + Values in the Element

 my_list[1]
 # $a
 # [1]  1  2  3  4  5  6  7  8  9 10

More help, in R console, type

 ?`[`
Sowmya S. Manian
  • 3,723
  • 3
  • 18
  • 30