user10072460
user10072460

Reputation:

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.

Upvotes: 0

Views: 31

Answers (2)

ThomasIsCoding
ThomasIsCoding

Reputation: 101335

Here is a solution without using for-loop:

r <- setNames(data.frame(as.matrix(df2)>=outer(rep(1,nrow(df2)),df1$gol)),
              paste0("out",seq(ncol(df2))))
r[r==T] <- "y"
r[r==F] <- "n"
res <- cbind(df2,r)

which gives:

> res
   cost1 cost2 cost3 cost4 out1 out2 out3 out4
1      7     9     5    13    y    y    n    y
2      3    12     4    14    n    y    n    y
3      9    13     3    11    y    y    n    y
4      5     6     2    13    y    n    n    y
5      4     3     5    12    y    n    n    y
6      8    16     6     9    y    y    y    y
7      9    11     2     9    y    y    n    y
8      6    14    11    12    y    y    y    y
9      5    10    14     6    y    y    y    n
10     2     9     4    12    n    y    n    y

Upvotes: 1

dc37
dc37

Reputation: 16178

There is probably better and more elegant solution that I haven't think yet, but using a for loop with an ifelse function, you can do something like that:

dfx <- data.frame(matrix(ncol=ncol(df2),nrow = nrow(df2)))
for(i in 1:ncol(df2))
{
  dfx[,i] <- ifelse(df2[,i] >= df1[i,1],"y","n")
  colnames(dfx)[i] <- paste0("out",i)
}
out <- cbind(df2,dfx)

And you get the following output:

> out
   cost1 cost2 cost3 cost4 out1 out2 out3 out4
1      7     9     5    13    y    y    n    y
2      3    12     4    14    n    y    n    y
3      9    13     3    11    y    y    n    y
4      5     6     2    13    y    n    n    y
5      4     3     5    12    y    n    n    y
6      8    16     6     9    y    y    y    y
7      9    11     2     9    y    y    n    y
8      6    14    11    12    y    y    y    y
9      5    10    14     6    y    y    y    n
10     2     9     4    12    n    y    n    y

Upvotes: 0

Related Questions