create r function based on the content for each row

Dear R experts,

I would like to pass text content as function and produce another column.
Here is my data:
var1 var2 var3 input rule result
a d g "var1,var2" "a d"
b e h "var2,var3" "e h"
c f i "var1,var2,var3" "c f i"

I want to put the content in var1, var2, var3 together base on the rule in “input rule” column and produce “result” column. Take the first row of my data as an example, I want to produce “result” column like result[1]=paste(var1[1],var[1],sep= ‘ ‘). How can I achieve this without write paste function for each row. Suggestions will be appreciated, Thanks.

Best,
Veda

I believe you're after something like the following:

dataset <- data.frame(var1 = c("a", "b", "c"),
                      var2 = c("d", "e", "f"),
                      var3 = c("g", "h", "i"),
                      input_rule = c("var1, var2", "var2, var3", "var1, var2, var3"),
                      stringsAsFactors = FALSE)

(dataset <- within(data = dataset,
                   expr = {
                     result <- sapply(X = 1:length(input_rule),
                                      FUN = function(t)
                                      {
                                        indices <- which(x = names(x = dataset[1:3]) %in% strsplit(x = input_rule[t],
                                                                                                   split = ", ")[[1]])
                                        paste(dataset[t, indices])
                                      })
                   }))
#>   var1 var2 var3       input_rule  result
#> 1    a    d    g       var1, var2    a, d
#> 2    b    e    h       var2, var3    e, h
#> 3    c    f    i var1, var2, var3 c, f, i

Created on 2019-03-13 by the reprex package (v0.2.1)

Hope this helps.

PS For your future posts, please provide your data set using the datapasta package. It's much copy-paste friendly. If you're not familiar, check this out.

One more variation on how problem like this can be solved:

library(magrittr)

input <- data.frame(var1 = c("a", "b", "c"),
                      var2 = c("d", "e", "f"),
                      var3 = c("g", "h", "i"),
                      input_rule = c("var1, var2", "var2, var3", "var1, var2, var3"),
                      stringsAsFactors = FALSE) %>%
  tibble::rowid_to_column()

res <- input %>%
  tidyr::gather("var", "value", -rowid, -input_rule) %>%
  dplyr::filter(stringr::str_detect(input_rule, var)) %>%
  dplyr::group_by(rowid) %>%
  dplyr::summarise(res = paste(value, collapse = " "))

dplyr::left_join(input, res) %>%
  dplyr::select(-rowid)
#> Joining, by = "rowid"
#>   var1 var2 var3       input_rule   res
#> 1    a    d    g       var1, var2   a d
#> 2    b    e    h       var2, var3   e h
#> 3    c    f    i var1, var2, var3 c f i

Created on 2019-03-13 by the reprex package (v0.2.1)

It works. Thank you very much.

It worked perfectly. Thanks.

If your question's been answered (even if by you), would you mind choosing a solution? (See FAQ below for how).

Having questions checked as resolved makes it a bit easier to navigate the site visually and see which threads still need help.

Thanks

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.