[R] Joining tables with different order and matched values

jim holtman jholtman at gmail.com
Sun May 14 16:04:17 CEST 2017


Here is a solution to the "shared values" question

> library(stringr)
> input <- read.table(text = "A             B
+
+ 1,2,5       3,8,7
+
+ 2,4,6       7,6,3  ",
+     header = TRUE,
+     as.is = TRUE
+     )
>
> input$'shared values' <- apply(input, 1, function(x){
+     toString(intersect(str_extract_all(x[1], "[^,]")[[1]],
+               str_extract_all(x[2], "[^,]")[[1]]
+               ))
+ })
>
> input
      A     B shared values
1 1,2,5 3,8,7
2 2,4,6 7,6,3             6



Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

On Mon, May 8, 2017 at 10:56 AM, abo dalash <abo_dlsh at hotmail.com> wrote:

> Hi All ..,
>
>
> I have 2 tables and I'm trying to have some information from the 1st table
> to appear in the second table with different order.
>
>
> For Example, let's say this is my 1st table :-
>
>
>
> Drug name           indications
>
>  Ibuprofen                Pain
>
>  Simvastatin            hyperlipidemia
>
> losartan                   hypertension
>
>
>
> my 2nd table is in different order for the 1st column :-
>
>
> Drug name       indications
>
>
> Simvastatin
>
> losartan
>
> Ibuprofen
>
> Metformin
>
>
> I wish to see the indication of each drug in my 2nd table subsisted from
> the information in my 1st table so the final table
>
> would be like this
>
>
> Drug name       indications
>
>
> Simvastatin     hyperlipidemia
>
> losartan           hypertension
>
> Ibuprofen       pain
>
> Metformin    N/A
>
>
> I have been trying to use Sqldf package and right join function but not
> able to formulate the correct syntax.
>
>
> I'm also trying to identify rows contain at least one shared value  in a
> dataset called 'Values":
>
>
> >Values
>
> A             B
>
> 1,2,5       3,8,7
>
> 2,4,6       7,6,3
>
>
>
> Columns A & B in the first row do not share any value while in the 2nd row
> they have a single shared value which is 6.
>
> The result I wish to see :-
>
>
> A             B             shared values
>
> 1,2,5       3,8,7             N/A
>
> 2,4,6       7,6,3               6
>
>
> I tried this syntax : SharedValues <- Values$A == Values$B but this
> returns logical results and what I wish to have
>
> is a new data frame including the new vector "shared values" showing the
> information exactly as above.
>
>
>
>
> Kind Regards
>
>
>
>
>
>
>         [[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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list