Reputation: 498
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!
Upvotes: 0
Views: 170
Reputation: 3833
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
?`[`
Upvotes: 1
Reputation: 13125
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"
Upvotes: 2