[R] How to replace match words whith colum name of data frame?
Fox, John
jfox at mcmaster.ca
Sat Jul 1 16:13:38 CEST 2017
Dear ?,
I'm sure that there are many ways to do what you want; here's one:
> cbind(concept_df, category=
+ ifelse(apply(
+ sapply(chemical_df$chemical,
+ function(x) grepl(x, concept_df$concept)),
+ 1, any),
+ "chemical", ""))
concept category
1 butan acid chemical
2 nano diamond particl
3 slurri composit
4 composit ph polis chemical
5 inorgan particl
6 grind liquid
7 liquid formul
8 nanoparticl
9 size abras particl
10 agent malic acid chemical
Or, if you're wedded to magrittr:
> sapply(chemical_df$chemical,
+ function(x) grepl(x, concept_df$concept)) %>%
+ apply(1, any) %>%
+ ifelse("chemical", "") %>%
+ cbind(concept_df, category=.)
concept category
1 butan acid chemical
2 nano diamond particl
3 slurri composit
4 composit ph polis chemical
5 inorgan particl
6 grind liquid
7 liquid formul
8 nanoparticl
9 size abras particl
10 agent malic acid chemical
I hope this helps,
John
--------------------------------------
John Fox, Professor Emeritus
McMaster University
Hamilton, Ontario, Canada
Web: socserv.mcmaster.ca/jfox
> -----Original Message-----
> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of ? ”
> Sent: Saturday, July 1, 2017 3:44 AM
> To: r-help at r-project.org
> Subject: [R] How to replace match words whith colum name of data frame?
>
> I have two data frame. I want to use "chemical_df" to match "concept_df
> "
>
>
> concept_df <- data.frame(concept=c("butan acid ", "nano diamond
> particl", "slurri composit", "composit ph polis", " inorgan particl ",
> "grind liquid", "liquid formul", "nanoparticl", "size abras particl",
> "agent malic acid"))
>
> chemical_df <- data.frame(chemical=c("basic", "alkalin", "alkali",
> "acid", " ph ", "hss"))
>
> Here is my match code:
>
> library(magrittr)
> match_df <- NULL
> for (i in 1:length(chemical_df$chemical)) { match_df<-
> data.frame(category=concept_df[grep(chemical_df$chemical[i],
> concept_df$concept), ] ) %>%
> rbind(match_df)
> }
>
> But I don't want this result:
> concept category
> 1 butan acid butan acid
> 2 nano diamond particl
> 3 slurri composit
> 4 composit ph polis composit ph polis
> 5 inorgan particl
> 6 grind liquid
> 7 liquid formul
> 8 nanoparticl
> 9 size abras particl
> 10 agent malic acid agent malic acid
>
> I desire to get :
> concept category
> 1 butan acid chemical
> 2 nano diamond particl
> 3 slurri composit
> 4 composit ph polis chemical
> 5 inorgan particl
> 6 grind liquid
> 7 liquid formul
> 8 nanoparticl
> 9 size abras particl
> 10 agent malic acid chemical
>
> Is any way to solve it?
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list