I thought this would be straightforward, but it's been a while since I've looked at R.
I have two tables, and I want to make a third table with values from the first based on values from the second. (I want the numbers from table 1, anytime the corresponding row/column from table 2 has a "1")
I was thinking sapply
or lapply
would probably be what I need, or something from dplyr? Just not sure how.
Table 1 (df1):
row sample.1 sample.2 sample.3
1 55 6788 4003
2 9000 135 1200
3 3400 2000 7500
4 92 348 227
5 4286 2731 6298
Table 2 (df2):
row sample.1 sample.2 sample.3
1 0 1 1
2 1 0 0
3 1 1 1
4 0 0 0
5 1 1 1
Table 3 ( df3 - desired output):
row sample.1 sample.2 sample.3
1 0 6788 4003
2 9000 0 0
3 3400 2000 7500
4 0 0 0
5 4286 2731 6298
An easier option would be an elementwise multiplication as these are numeric columns and because any number multiplied by 0 returns 0 while those multiplied by 1 gives the number itself (assuming both datasets have same dimensions
df1 * df2
If 'row' is the first column, subset the datasets by removing the first column and cbind
with the first column of any of the datasets
cbind(df1[1], df1[-1] * df2[-1])