Hi everybody I am trying to optimize below R code with for loops because it takes so much time to execute. I even tried compiler in R to turn the function to bytecode but performance even worse. So, Is there any way to write this code with apply functions
word_separation<-function(inp_data){
df<-NULL
for(k in 1:nrow(inp_data)){
vec<-unlist(strsplit(as.vector(inp_data[k,]),split=","))
if(length(vec)==1){
df<-rbind(df,data.frame(first_col=vec,second_col=vec))
}else{
temp_df<-NULL
for(i in 2:length(vec)){
for(j in i:length(vec){
temp_df<-rbind(temp_df,data.frame(first_col=vec[1],second_col=paste(vec[i:j],collapse=",")))
}
df<-rbind(df,temp_df)
df[df==""]<-NA
df<-df %>% unique() %>% na.omit()
}
}
}
return(df)
}
Here my inp_data dataframe has single column with data
column
Milk,Bread,Eggs,Jam
Apple,Milk,Beer
When passed to function returns an dataframe with columns, first column with first word and second column with combinations of other words in dataframe.
first_col second_col
Milk Bread
Milk Bread,Eggs
Milk Bread,Eggs,Jam
Milk Eggs
Milk Eggs,Jam
Milk Jam
Apple Milk
Apple Milk,Beer
Apple Beer