0

I have a below data frame for which I must search the string values and return the other columns It should return values depending on the input I give

Ex : 
Enter the number you want to search
Input 1 = 14
Inout 2 = 16

Combining above two, I have 14,16 as a string, then it should return me the below

rhs        name 
15          salt

Below is the Dataframe where we can search our inputs(it should search only on lhs )

DF:

lhs         rhs              name
32,39,6     65          jackfruit
39,6,65     32          coffee
14,16,26    15         salts
16,20,4     26           marshmallows
16,26,33    4            veggies
53          31           candy    

This search should accept for any number of strings and should be able to search. For suppose, my input is 14,16,26 it should return the value

rhs name
15  salt

Also, if its only 16, then it should return

Rhs      name
15        salts 
26        marshmallows 
4           veggies.

I have tried using the below code but it just uses the order for example:

CODE:

df[grep('16,20', df$lhs),]

output :

rhs   name
    26    marshmallows

But if change my search like below,

CODE:

df[grep('16,4', df$lhs),] (#leaving the number 20 )

the above one gives me an error.

Expected output :

 rhs   name
    26    marshmallows
pylearner
  • 1,358
  • 2
  • 10
  • 26

2 Answers2

1

You can try that:

df <- read.table(text = "lhs         rhs              name
32,39,6     65          jackfruit
39,6,65     32          coffee
14,16,26    15         salts
16,20,4     26           marshmallows
16,26,33    4            veggies
53          31           candy    ", header = T, stringsAsFactors = F)

# define input values:
input1 <- 14
input2 <- 16

library(dplyr)

# create a data frame in long format for search:
search_df <- unnest(mutate(df, lhs = strsplit(x = lhs, split = "\\D+")))

# now search for names where all inputs are in lhs:
search_df %>% 
  group_by(name) %>% 
  filter(all(c(input1, input2) %in% lhs)) %>% 
  ungroup() %>% 
  distinct(rhs, name)

Edit: If you want more flexibility regarding inputs, just go for one:

input <- c(14, 16)
search_df %>% 
  group_by(name) %>% 
  filter(all(input %in% lhs)) %>% 
  ungroup() %>% 
  distinct(rhs, name)
Tino
  • 2,091
  • 13
  • 15
  • what if I dont enter any value for `input 2` ? and my `input 1` is still 14, it should return me the one which contains only 14 ...can we get that ? – pylearner May 11 '18 at 10:58
  • @pylearner maybe it is better to use just one input but give it more than one value, for example `input <- c(14)` or `input <- c(14, 16)` . Otherwise, just set `input2 <- NULL`. But my advise would be to go for the single-input-approach. – Tino May 11 '18 at 11:56
  • Thanks Tino, Also look at my solution .. :) – pylearner May 13 '18 at 18:29
0
library(rje)
myfun <- function(input){
    result <- apply(rules, 1, function(x){is.subset(input, as.vector(unlist(strsplit(x[2], ','))))})
    result <- rules[result, ]

    return (result)
}
pylearner
  • 1,358
  • 2
  • 10
  • 26