-1

I have a data frame which contains some data (Claims) and two additional columns, containing row and column information, for example:

Claim row Column

5000 2 10

etc.

I have a matrix that contains some data as well as NAs. I'd like to replace the NAs with the data from the data frame, using the row/column information. So 5000 would replace the NA in the matrix that is currently in row 2, column 10.

Is there an easy way to accomplish this with R?

rcs
  • 67,191
  • 22
  • 172
  • 153
user2249626
  • 453
  • 2
  • 6
  • 15
  • 3
    Can you provide a [minimal reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? and What have you tried? – Jilber Urbina Sep 13 '13 at 09:05

2 Answers2

2
# If you have for example a matrix like this
m <- matrix(c(1:9), ncol=3, nrow=3)

# and information column such as:
info <- rbind(c(5000,2,2), c(8000,1,3))

# you can do like this:
m[info[,c(2:3)]] <- info[,1]
Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
-1

I think I've figured out a (bad) way of doing it: Loop through all the rows in the matrix, select all NA elements in the matrix and assign to the resulting vector a subset of the data frame where the row column matches the current row in the loop, i.e. something like this:

# Fill triangle
new_data <- data.frame(Claim = pred_claims, Column = cols, row = ro) # data frame 
full_triangle <- auto_incr # matrix to be filled

for (i in 2:10) {
  full_triangle[i,is.na(full_triangle[i,])] <- subset(new_data, row==i)$Claim
}
user2249626
  • 453
  • 2
  • 6
  • 15