Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeat each element in a string a certain number of times

Tags:

string

r

I'm using the rep() function to repeat each element in a string a number of times. Each character I have contains information for a state, and I need the first three elements of the character vector repeated three times, and the fourth element repeated five times.

So lets say I have the following character vectors.

al <- c("AlabamaCity", "AlabamaCityST", "AlabamaCityState", "AlabamaZipCode") 
ak <- c("AlaskaCity", "AlaskaCityST", "AlaskaCityState", "AlaskaZipCode")
az <- c("ArizonaCity", "ArizonaCityST", "ArizonaCityState", "ArizonaZipCode")
ar <- c("ArkansasCity", "ArkansasCityST", "ArkansasCityState", "ArkansasZipCode")

I want to end up having the following output.

AlabamaCity
AlabamaCity
AlabamaCity
AlabamaCityST
AlabamaCityST
AlabamaCityST
AlabamaCityState
AlabamaCityState
AlabamaCityState
AlabamaZipCode
AlabamaZipCode
AlabamaZipCode
AlabamaZipCode
AlabamaZipCode
AlabamaZipCode
...

I was able to get the desired output with the following command, but it's a little inconvenient when I'm running through all fifty states. Plus, I might have another column with 237 cities in Alabama, and I'll inevitably run into problems matching up the names in the first column with the values in the second column.

   dat = data.frame(name=c(rep(al[1:3],each=3), rep(al[4],each=6), 
                rep(ak[1:3],each=3), rep(ak[4],each=6)))
   dat


   dat2 = data.frame(name=c(rep(al[1:3],each=3), rep(al[4],each=6), 
                rep(ak[1:3],each=3), rep(ak[4],each=6)),
                city=c(rep("x",each=15), rep("y",each=15)))
   dat2

Of course, in real life, the 'x' and 'y' won't be single values.

So my question concerns if there is a more efficient way of performing this task. And closely related to the question, when does it become important to ditch procedural programming in favor of OOP in R. (not a programmer, so the second part may be a really stupid question) More importantly, is this a task where I should look for a oop related solution.

like image 237
ATMathew Avatar asked Jun 30 '11 17:06

ATMathew


People also ask

How do you repeat a string and number of times?

repeated = new String(new char[n]). replace("\0", s); Where n is the number of times you want to repeat the string and s is the string to repeat. No imports or libraries needed.

How do you repeat a string times?

Syntax: string. repeat(count); Parameter: Accepts an integer count which is the number of times we want to repeat the string.

How do you repeat a string 3 times in python?

In Python, we utilize the asterisk operator to repeat a string. This operator is indicated by a “*” sign. This operator iterates the string n (number) of times.

How do you repeat a number multiple times in python?

Using the * Operator The * operator can also be used to repeat elements of a list. When we multiply a list with any number using the * operator, it repeats the elements of the given list. Here, we just have to keep in mind that to repeat the elements n times, we will have to multiply the list by (n+1).


2 Answers

According to ?rep, times= can be a vector. So, how about this:

dat <- data.frame(name=rep(al, times=c(3,3,3,6)))

It would also be more convenient if your "state" data were in a list.

stateData <- list(al,ak,az,ar)
Data <- lapply(stateData, function(x) data.frame(name=rep(x, times=c(3,3,3,6))))
Data <- do.call(rbind, Data)
like image 114
Joshua Ulrich Avatar answered Oct 20 '22 07:10

Joshua Ulrich


I think you can combine the times() argument of rep to work through a list with sapply(). So first, we need to make our list object:

vars <- list(al, ak, az, ar)


# Iterate through each object in vars. By default, this returns a column for each list item.
# Convert to vector and then to data.frame...This is probably not that efficient.
as.data.frame(as.vector(sapply(vars, function(x) rep(x, times = c(3,3,3,6)))))

1                                                         AlabamaCity
2                                                         AlabamaCity
3                                                         AlabamaCity
4                                                       AlabamaCityST
....snip....
....snip....
57                                                    ArkansasZipCode
58                                                    ArkansasZipCode
59                                                    ArkansasZipCode
60                                                    ArkansasZipCode
like image 43
Chase Avatar answered Oct 20 '22 09:10

Chase