paste - R concatenate row labels with colnames -


a<-as.factor(c('a','a','b','b','c','d')) b<-as.factor(c('a','b','c','c','d','a')) c<-as.factor(c('a','b','d','d','c','b')) x<-data.frame(a,b,c)    b c 1 a 2 b b 3 b c d 4 b c d 5 c d c 6 d b 

i have large data table (using datatable package) , take column names , append them row factor values easy identification.

so in above simple example (using data frame illustration) have

a    b   c a:a  b:a c:a a:a  b:b c:b a:b  b:c c:d .. .. a:d  b:a c:b 

i had tried (unsuccessfully) type of apply , paste combination. can't quite pass colname arguments paste each column correctly. ideas on how accomplish task large data tables? datatable approach great, dataframe fine well, since it's 1 time action.

data frame solution:

x[] <- mapply(function(n, f) {     levels(f) <- paste(n, levels(f), sep=":")     f }, names(x), x) 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -