I'm trying to use R to print some data in a custom fashion for use in a separate program. It keeps trying to left pad my numbers, and I can't quite get it to stop. I have not been able to find anything in the ?format, ?print, ?cat, ?etc. to quite fix my problem. Also, searching for fixed width or variable width results in people looking to solve somewhat different problems (and most looking to change the style of padding -- not remove it).
Take the following data setup:
> df.play <- data.frame(name = c('a','b','c'), value = c(1,11,111))
> df.play
name value
1 a 1
2 b 11
3 c 111
This is my desired output
#Goal
> for (j in seq(nrow(df.play))) {cat(as.character(df.play[j,1]),'.',df.play[j,2],'\n',sep='')}
a.1
b.11
c.111
How do I get this output format without explicitly looping (preferably avoiding external libraries)?
#Naive Attempt 1
# Why does it left pad the second "column"?
# How do I get it to stop?
# Why does cat even parse it as a column to begin with?
junk<-apply(df.play,1,function(x) cat(x[1],'.',x[2],'\n',sep=''))
a. 1
b. 11
c.111
#Naive Attempt 2
# Perhaps this will help provide some insight.
# The number is a string before it gets to cat. Why?
t<-apply(df.play,1,function(x) cat(x[1],'.',sprintf('%d',x[2]),'\n',sep=''))
Error in sprintf("%d", x[2]) :
invalid format '%d'; use format %s for character objects