r - automatically generate string from data frame row -
i trying create string data frame row. little example this:
df <- data.frame(name=c('bla'), firstname=c('dada'), other=c('dum')) sprintf('name:%s firstname:%s other:%s', df[1,'name'], df[1,'firstname'], df[1,'other'])
i trying create function can provide data frame argument , creates string like.
i started try sprintf
can't arguments right:
sprintf(paste(sub('$',':%s',names(df)), collapse=' '), df[1,])
how can unlist or anyhow automatically use arguments? working solution be:
sprintf(paste(sub('$',':%s',names(df)), collapse=' '), df[1,'name'], df[1,'firstname'], df[1,'other'])
perhaps do.call
help:
df <- data.frame(name=c('bla'), firstname=c('dada'), other=c('dum')) fmt <- 'name:%s firstname:%s other:%s' do.call(sprintf, c(df, fmt = fmt)) # [1] "name:bla firstname:dada other:dum"
Comments
Post a Comment