samedi 30 novembre 2019

Is there a way to match two different data frames for multiple columns

I want to make a link between columns by the condition. I have two data frames as follows:

df1<-read.table(text=" gol
4
7
6
9
",header=TRUE)

and the second df is :

df2<-read.table(text=" cost1     cost2   cost3   cost4
7       9       5       13
3       12      4       14
9       13      3       11
5       6       2       13
4       3       5       12
8       16      6       9
9       11      2       9
6       14      11      12
5       10      14      6
2       9       4       12

",header=TRUE)

The condition is, for example, in df2, cost1, if the value is greater or equal to 4 in df1, it gets "y" else it gets "n". in Cost 2 if the value greater or equal to 7 in df1, it gets "y" else it gets "n" and so on. Please assume I have more than four columns.

The outcome would be as follows:

output<-read.table(text=" cost1  cost2   cost3   cost4   out1    out2    out3    out4
7       9       5       13      y       y       n       y
                   3    12      4       14      n       y       n       y
                   9    13      3       11      y       y       n       y
                   5    6       2       13      y       n       n       y
                   4    3       5       12      y       n       n       y
                   8    16      6       9       y       y       y       y
                   9    11      2       9       y       y       n       y
                   6    14      11      12      y       y       y       y
                   5    10      14      6       y       y       y       n
                   2    9       4       12      n       y       n       y
                   ",header=TRUE)

I just now I need to do it using ifelse, but I struggled to do it for this example. Your help very miuch appriciated.

Aucun commentaire:

Enregistrer un commentaire