Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using variable value as column name in data.frame or cbind

Is there a way in R to have a variable evaluated as a column name when creating a data frame (or in similar situations like using cbind)?

For example

a <- "mycol";
d <- data.frame(a=1:10)

this creates a data frame with one column named a rather than mycol.

This is less important than the case that would help me remove quite a few lines from my code:

a <- "mycol";
d <- cbind(some.dataframe, a=some.sequence)

My current code has the tortured:

names(d)[dim(d)[2]] <- a;

which is aesthetically barftastic.

like image 529
JasonMond Avatar asked Mar 29 '13 19:03

JasonMond


Video Answer


1 Answers

Is structure(data.frame(1:10),names="mycol") aesthetically pleasing to you? :-)

like image 137
Ferdinand.kraft Avatar answered Oct 10 '22 21:10

Ferdinand.kraft