vivirbr
vivirbr

Reputation: 133

Return col name for unique value in data frame

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

Answers (2)

Nishu Tayal
Nishu Tayal

Reputation: 20830

You should use either colnames(d[2]) or colnames(d)[2] to get the column names.

Upvotes: 0

tobiasegli_te
tobiasegli_te

Reputation: 1463

You have to index a vector that contains the colnames, try colnames(d)[2]

Upvotes: 1

Related Questions