Naming columns in a data table in R

I don’t word with data tables, but this is a solution that would work for data frames, and should hopefully generalize. The strategy is to use the fact that you can fill one vector with another vector, without ever having to use a loop.

 # make the example data sets
 D1 <- as.data.frame(matrix(data=(1:(20*181)), nrow=20, ncol=181))
 D2 <- data.frame(name=paste0("Name_",(1:181)))

 # make a copy of D1
 D1_named <- D1

 # the vector of D1 names is replaced by the value in D2
 # (both vectors must be of the same length)
 names(D1_named) <- D2$name

Browse More Popular Posts

Leave a Comment