I have a dataframe df:
colour shape
'red' circle
'blue' square
'blue' circle
'green' sphere
一个带有命名行/列的双重矩阵m
circle square sphere
red 1 4 7
blue 2 5 8
green 3 6 9
我想在DF中添加一个新列,使得我可以得到:
id colour shape
1 'red' circle
5 'blue' square
2 'blue' circle
9 'green' sphere
我尝试使用以下代码来实现,但似乎并没有起作用:
df$id <- m[df$colour,df$shape]
我也尝试了apply()等方法,但没有成功。有人能告诉我在不使用循环的情况下正确的做法吗?