基于向量替换 table 的单元格(空白单元格和没有字典的除外)

Replace cells of a table based on a vector (except for blank cells and without a dictionary)

我有一个这样的table(第一行是header):

ID Sire Dam
EL SF NA
GN SF UJ
SF SF NA

和这样的矢量:

G<-c("10325", "6788" ,"1140","6788","6789" , "14140", "6788" )

我想知道如何根据该向量(空白单元格除外)替换 table 的单元格以获得此 table(没有字典):

ID Sire Dam
10325 6788 NA
1140 6788 6789
14140 6788 NA

这是一种方法 -

tmp <- t(df)
df[] <- t(replace(tmp, !is.na(tmp), G))
df

#     ID Sire  Dam
#1 10325 6788 <NA>
#2  1140 6788 6789
#3 14140 6788 <NA>

替换是按列进行的,但您是按行更改值,因此需要转置。