r - paste not returning values concatenated -


i trying column names of dataframe use them in call, apply call returns values separated, instead of concatenated correctly. did wrong here?

df<-data.frame(c(1,2,3),c(4,5,6)) colnames(df)<-c("hi","bye") apply(df,2,function(x){     paste("subscale_scores$",colnames(x),sep="")  #this command trying run        #lm(paste("subscale_scores",colnames(x))~surveys$npitotal+ipip$extraversion+ipip$agreeableness+ipip$conscientiousness+ipip$emotionalstability+ipip$intelimagination) }) 

goal output:

subscale_scores$hi subscale_scores$bye 

is there need apply?

is mean?

paste0('subscale_scores$', names(df)) # [1] "subscale_scores$hi"  "subscale_scores$bye" 

if need them concatenated newline say, add , sep='\n'.

the paste0 shorthand paste(..., sep="").


a note on lm call later - if want lm(y ~ ...) y each of columns separately, try:

lms <- lapply(colnames(df),               function (y) {                   # construct formula                   frm <- paste0('subscale_scores$', y, ' ~ surveys$npitotal+ipip$extraversion+ipip$agreeableness+ipip$conscientiousness+ipip$emotionalstability+ipip$intelimagination')                   lm(frm)               }) names(lms) <- colnames(df) 

then lms$hi contain output of lm(subscale_scores$hi ~ ...) , on.

or if aim combine columns (y1 + y2 ~ ...)

then paste0('subscale_scores$', names(df), collapse='+') give subscale_scores$hi+subscale_scores$bye


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -