I have a matrix 16 columns 366 rows and I want to replace the NA
with the value of a vector length 16.
All the time I am getting this error:
for (i in 1:16) {
for (j in 1:366) {
if(is.na(ChSiz(i,j)==TRUE) {
Error: unexpected '{' in:
" for (j in 1:366) {
if(is.na(ChSiz(i,j)==TRUE) {"
> ChSiz[i,j]<-x[i]
Error in ChSiz[i, j] <- x[i] : object 'j' not found
> }
Error: unexpected '}' in " }"
> }
Error: unexpected '}' in " }"
> }
Error: unexpected '}' in "}"
From what I read, you want to replace the
NA
s in each column of a matrix with a corresponding value of a vector.Lets say this is your data set and your replacement vector
Here's a quite simple vectorized way to replace the
NA
s with corresponding valuesThis is basically takes the advantage
is.na
returning a logical matrix that can be used as a subsetting index and thecol
function which convertsvec
to a matrix.