Reputation: 133
I need to know how can I return the column name for a unique value in a data frame Like that example:
L3 <- LETTERS[1:3]
fac <- sample(L3, 10, replace = TRUE)
(d <- data.frame(x = 1, y = 1:10, fac = fac))
This command just return to me NULL
colnames(d[5,2])
but the result should be "y"
How can I fix this?
Upvotes: 1
Views: 72
Reputation: 20830
You should use either colnames(d[2])
or colnames(d)[2]
to get the column names.
Upvotes: 0
Reputation: 1463
You have to index a vector that contains the colnames,
try colnames(d)[2]
Upvotes: 1