Qbik
Qbik

Reputation: 6147

Create data.frame with variable as tag

I would like to create : data.frame(colname=1:11) by providing variable x="colname", I've tried data.frame(as.formula("x=1:11")) but id does not work, what is tag in data frame ?

Upvotes: 0

Views: 386

Answers (1)

G. Grothendieck
G. Grothendieck

Reputation: 269526

Here are some alternatives:

1) setNames

setNames(data.frame(1:11), "colname")

2) names<-

"names<-"(data.frame(1:10), "colname")

2a) names(...)<-

DF <- data.frame(1:11)
names(DF) <- "colname"

3) structure This one mucks with the internal structure and is not really recommended but has been added since it is possible to do.

structure(data.frame(1:11), .Names = "colname")

4) dimnames

as.data.frame(matrix(1:11, dimnames = list(NULL, "colname")))

Upvotes: 3

Related Questions