I have a csv file contains 10000 row of this type
ref
ref smbole name
r1 ts table_spoon
r2 kn knife
r3 fr door
... ... ...
and a matrix extracted from some text files with R in this format (contains between 60 and 100 row)
data
ref smbole name
r2 kn NA
r3 NA door
NA NA table_spoon
NA NA door
... ... ...
I want to map the data matrix with values from ref matrix according to the NA values I mean to replace each NA with the equivalent value
my expected output is
ref smbole name
r2 kn knife
r3 fr door
r1 ts table_spoon
r3 fr door
I tried this code but it haven't change any thing
ref <- as.matrix(read.delim("name.csv", sep = "\t"))
fun <- function(rowi,r) {
res <- apply(as.data.frame(ref),1,function(x) {length(na.omit(match(na.omit(rowi),x)))})
IND <- which( max(data) == data )[1]
rowi[is.na(rowi)] <- unlist(genemap[IND,])[is.na(rowi)]
return(rowi)
}
as.data.frame(t(apply(data, 1, fun, ref))
)