[R] Assigning a function to the 'times' argument of rep()

z2.0 zack.abrahamson at gmail.com
Mon Feb 13 04:56:48 CET 2012


Question: 

I'm trying to use paste() with rep() to reformat a series of values as zip
codes. e.g., if column 1 looks like:

52775
83111
99240
4289
112
57701
20001

I want rows 4 and 5 to read,

"04289"
"00112"

My thought was this:

> perry_frame$zip <- ifelse(nchar(as.character(perry_frame$zip))<5,
 
paste(rep("0",times=(5-nchar(as.character(perry_frame$zip)))),perry_frame$zip,sep=''),
  as.character(perry_frame$zip))

But R throws the following:

Error in rep("0", times = (5 - nchar(as.character(perry_frame$zip)))) : 
  invalid 'times' argument

Is there a reason this doesn't work?

Thanks,

Zack

--
View this message in context: http://r.789695.n4.nabble.com/Assigning-a-function-to-the-times-argument-of-rep-tp4382849p4382849.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list