3

I am trying to concatenate strings using mapply function in R. However, I want one of the strings to be variable in mapply function. I have a snippet of my code below:

strings<-data.frame(x=c("dsf","sdf","sdf"))
strings2<-data.frame(extension=c(".csv",".json",".xml"))

for (i in 1:3)
{
  strings_concat<-mapply(function(string1,string2) paste0(string1,string2),strings$x,strings2$extension[i])%>%
    data.frame()%>%
    unlist()%>%
    data.frame()

 #dosomething with strings_concat
}

But this is giving me the last iteration only

strings_concat

dsf.xml
sdf.xml
sdf.xml

bust instead, the desired output is as follows:

strings_concat

dsf.csv 
sdf.csv 
sdf.csv 

dsf.json 
sdf.json 
sdf.json 

dsf.xml
sdf.xml
sdf.xml

At every iteration, i want to combine strings_concat with another dataframe and save it. Can anyone help me if there is an easy way to do this in R?

user86907
  • 817
  • 9
  • 21

2 Answers2

2

Perhaps, outer is a better option here :

strings_concat <- c(outer(strings$x, strings2$extension, paste0))
strings_concat
#[1] "dsf.csv"  "sdf.csv"  "sdf.csv"  "dsf.json" "sdf.json" "sdf.json" 
#    "dsf.xml"  "sdf.xml"  "sdf.xml" 

You can add it in a data.frame :

df <- data.frame(strings_concat)

If you want to add some additional steps at each iteration you can use lapply :

lapply(strings2$extension, function(x) {
  strings_concat <- paste0(strings$x, x)
  #do something with strings_concat
})
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • this is a great way to solve this but i want to do something with strings_concat at each iteration, is there anyway i can manage that? for instance, in the first iteration, cbind(anotherdataframe,strings_concat), and do something with this combined data frame. do same thing for the rest of iteration – user86907 Aug 21 '20 at 01:23
  • In that case, I think you can use `lapply`. See updated answer if that helps. – Ronak Shah Aug 21 '20 at 01:26
  • Yes that is perfect Rokan, but the output I am getting as follows: [[1]] [1] "dsf.csv" "dsf.json" "dsf.xml" [[2]] [1] "sdf.csv" "sdf.json" "sdf.xml" [[3]] [1] "sdf.csv" "sdf.json" "sdf.xml" – user86907 Aug 21 '20 at 01:43
  • Yes that is perfect Rokan, but the output I am getting as follows: [[1]] [1] "dsf.csv" "dsf.json" "dsf.xml" Instead, I want csv to be in one group, json to be in one group nad xml to be in one group so that i can iterate through them and combine them with another df. for instance, if i save the function results as lists, then lists[[1]]= [[1]] [1] "dsf.csv" "sdf.csv" "sdf.csv" – user86907 Aug 21 '20 at 01:50
  • You can iterate over `strings2$extension` instead in `lapply`. I have updated the answer. – Ronak Shah Aug 21 '20 at 01:52
1

All you should need to do is make sure you are continually augmenting your dataset. So I think this should do the trick:

strings<-data.frame(x=c("dsf","sdf","sdf"))
strings2<-data.frame(extension=c(".csv",".json",".xml"))

# We are going to keep adding things to results
results = NULL

for (i in 1:3)
{
  strings_concat<-mapply(function(string1,string2) paste0(string1,string2),strings$x,strings2$extension[i])%>%
    data.frame()%>%
    unlist()%>%
    data.frame()
 # Here is where we keep adding things to results
 results = rbind(results, strings_concat)
}

print(results)

Caution: not in front a computer with R so this code is untested

Cliff AB
  • 1,160
  • 8
  • 15