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.
Berend Hasselman
2012-Feb-13 05:37 UTC
[R] Assigning a function to the 'times' argument of rep()
On 13-02-2012, at 04:56, z2.0 wrote:> 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"This might help x <- c(52775, 83111, 99240, 4289, 112, 57701, 20001)> formatC(x,format="d", flag="0", width=5)[1] "52775" "83111" "99240" "04289" "00112" "57701" "20001" Berend
Jorge I Velez
2012-Feb-13 05:40 UTC
[R] Assigning a function to the 'times' argument of rep()
?formatC HTH, Jorge.-* * On Sun, Feb 12, 2012 at 10:56 PM, z2.0 <> wrote:> 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. > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Petr Savicky
2012-Feb-13 08:24 UTC
[R] Assigning a function to the 'times' argument of rep()
On Sun, Feb 12, 2012 at 07:56:48PM -0800, z2.0 wrote:> 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?Hi. Working solutions were suggested by others. The answer to your question is that "times" argument should either be a single number or a vector of the same length as the first argument. However, "0" has length 1 5 - nchar(as.character(perry_frame$zip)) [1] 0 0 0 1 2 0 0 Hope this helps. Petr Savicky.
Maybe Matching Threads
- I'm sure I'm missing something with formatC() or sprintf()
- How to efficiently compare each row in a matrix with each row in another matrix?
- Prediction from censReg?
- passing xlim to coord_map in ggplot2
- Unique instances of a string in a vector -- there must be a better way to do this