veda
March 13, 2019, 2:54am
1
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)
veda
March 14, 2019, 5:33am
4
It works. Thank you very much.
veda
March 14, 2019, 5:35am
5
It worked perfectly. Thanks.
mara
March 14, 2019, 12:18pm
6
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.
If your question has been answered, don't forget to mark the solution!
How do I mark a solution?
Find the reply you want to mark as the solution and look for the row of small gray icons at the bottom of that reply. Click the one that looks like a box with a checkmark in it:
[image]
Hovering over the mark solution button shows the label, "Select if this reply solves the problem". If you don't see the mark solution button, try clicking the three dots button ( ••• ) to expand the full set of options.
When a solution is chosen, the icon turns green and the hover label changes to: "Unselect if this reply no longer solves the problem". Success!
[solution_reply_author]
…
Thanks
1 Like
system
Closed
March 27, 2019, 2:04am
7
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.