将值从一个 table 复制到另一个,仅当第二个 table 具有特定值时
Copy values from one table to another, only where second table has specific values
我认为这会很简单,但我已经有一段时间没看过 R 了。
我有两个 table,我想用第一个的值基于第二个的值制作第三个 table。 (我想要 table 1 中的数字,任何时候 table 2 中相应的 row/column 都有一个“1”)
我在想 sapply
或 lapply
可能是我需要的,或者 dplyr 的东西?只是不确定如何。
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 - 所需输出):
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
一个更简单的选择是元素乘法,因为这些是数字列,因为任何数字乘以 0 returns 0 而那些乘以 1 给出数字本身(假设两个数据集具有相同的维度
df1 * df2
如果 'row' 是第一列,则通过删除第一列和 cbind
任何数据集的第一列来对数据集进行子集化
cbind(df1[1], df1[-1] * df2[-1])
我认为这会很简单,但我已经有一段时间没看过 R 了。
我有两个 table,我想用第一个的值基于第二个的值制作第三个 table。 (我想要 table 1 中的数字,任何时候 table 2 中相应的 row/column 都有一个“1”)
我在想 sapply
或 lapply
可能是我需要的,或者 dplyr 的东西?只是不确定如何。
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 - 所需输出):
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
一个更简单的选择是元素乘法,因为这些是数字列,因为任何数字乘以 0 returns 0 而那些乘以 1 给出数字本身(假设两个数据集具有相同的维度
df1 * df2
如果 'row' 是第一列,则通过删除第一列和 cbind
任何数据集的第一列来对数据集进行子集化
cbind(df1[1], df1[-1] * df2[-1])