Q:

Create names for a list of data frames based on a value in that data frame

I am trying to use a unique value in a column to name another column in a list of data frames
library(data.table)

a <- c("BZ", "BZ", "BZ", "US", "US", "US")
b <- c(1, 3, 5, 2, 4, 6)
c <- data.table(a, b)
d <- split(c, c$a)

I am wanting to then drop column A and use the unique value from column a to name column B
For example, if I just had one dataframe with one a value, I could do this:
c <- subset(c, a == "BZ")
x <- unique(c$a)
setnames(c, "b", x)

Is there a way to perform this on a list of data frames?

A:

We can use lapply to loop over the list of data.tables, extract the first element of 'a' (as there is only one unique element and set the names of the column with setnames
library(data.table)
d1 <- lapply(d, function(x) setnames(x[, .(b)], x$a[1]))

Or we could have used a subset of columns before split
d2 <- split(c[, .(b)], c$a)
Map(setnames, d2, names(d2))

If we don't want to drop the column
lapply(d,  function(x) setnames(x, "b", x$a[1]))

Or use Map 
Map(setnames, d, old = "b", new = names(d))

