wesmantooth9071
wesmantooth9071

Reputation: 55

R - Nested loops for specific values in vectors

I'm pretty new to loops in R/in general and couldn't find an answer elsewhere.

I am using a nested loop to paste values of two vectors into a hyperlink. While I would like to only have three output strings, I am receiving nine strings in total. Maybe this is really easy, but any input is highly appreciated.

**Example:** 

start <- c("Value1", "Value2", "Value3")
end <- c("Value1.1", "Value2.1", "Value3.1")

summary <- c()

for (i in start) {
    for(j in end) {

  summary[i] <- print(paste('This is a Hyperlink Text', 
  i, 'here it continues', j ,'here it ends'))
    }  
}

**Actual Output:** 

[1] "This is a Hyperlink Text **Value1** here it continues **Value1.1** here it ends"
[1] "This is a Hyperlink Text **Value1** here it continues **Value2.1** here it ends"
[1] "This is a Hyperlink Text **Value1** here it continues **Value3.1** here it ends"
[1] "This is a Hyperlink Text **Value2** here it continues **Value1.1** here it ends"
[1] "This is a Hyperlink Text **Value2** here it continues **Value2.1** here it ends"
[1] "This is a Hyperlink Text **Value2** here it continues **Value3.1** here it ends"
[1] "This is a Hyperlink Text **Value3** here it continues **Value1.1** here it ends"
[1] "This is a Hyperlink Text **Value3** here it continues **Value2.1** here it ends"
[1] "This is a Hyperlink Text **Value3** here it continues **Value3.1** here it ends"


**Desired ouput:** 

"This is a Hyperlink Text **Value1** here it continues **Value1.1** here it ends"
"This is a Hyperlink Text **Value1** here it continues **Value2.1** here it ends"
"This is a Hyperlink Text **Value1** here it continues **Value3.1** here it ends"

Upvotes: 3

Views: 52

Answers (3)

Sathish
Sathish

Reputation: 12723

mapply(function(x, y) paste('This is a Hyperlink Text', x, 'here it continues', y ,'here it ends'), 
       x = start, y = end)

#                                                                    Value1 
# "This is a Hyperlink Text Value1 here it continues Value1.1 here it ends" 
#                                                                    Value2 
# "This is a Hyperlink Text Value2 here it continues Value2.1 here it ends" 
#                                                                    Value3 
# "This is a Hyperlink Text Value3 here it continues Value3.1 here it ends" 

Upvotes: 2

Ian Campbell
Ian Campbell

Reputation: 24838

How about like this?

start <- c("Value1", "Value2", "Value3")
end <- c("Value1.1", "Value2.1", "Value3.1")

summary <- c()
for (i in seq_along(start)) {
    summary[i] <- paste('This is a Hyperlink Text', 
                              start[i], 'here it continues', end[i] ,'here it ends')
}
print(summary)

Or with an apply function that takes two vectors as input from purrr.

library(purrr)
purrr:::map2(start,end,~ paste('This is a Hyperlink Text', .x, 'here it continues', .y ,'here it ends'))

Upvotes: 1

markhogue
markhogue

Reputation: 1179

Your nested loop isn't necessary if the values have the same index. The way it's set up is necessarily giving you i x j rows. So, here, using only one index, single loop:

for (i in 1:length(start)) {
  summary[i] <- print(paste('This is a Hyperlink Text', 
                              start[i], 'here it continues', end[i]  ,'here it ends'))

Upvotes: 2

Related Questions