Reputation: 135
I'm sure this is simple but cannot find an answer for it. I have a data frame df:
df <- data.frame(replicate(10,sample(0:10,1000,rep=TRUE)))
I then have another smaller data frame that has cell references (row and column numbers) referring to specific cells in df that should be empty (i.e. NA):
cellRefs <- data.frame(replicate(2,sample(1:10,10,rep=TRUE)))
How do I go about replacing all the cells in df with NA that are referred to by the row and column references from cellRefs?
Upvotes: 3
Views: 124
Reputation: 886938
We could convert the 'cellRefs' into a matrix
and use as row/column index to assign those elements in 'df' to NA
df[as.matrix(cellRefs)] <- NA
Upvotes: 3
Reputation: 994
Maybe not the fastest way, but it works with a loop across cellRefs rows.
for (i in 1:nrow(cellRefs)){
df[cellRefs[i,1],cellRefs[i,2]] = NA
}
Upvotes: 1