Adding values corresponding to matched column items

Hello - I have two sample datasets which are as follows:

structure(list(CellType = structure(c(1L, 2L, 3L, 3L, 1L, 4L, 
4L), .Label = c("CellA", "CellB", "CellC", "CellD"), class = "factor"), 
    Gene1 = c(0.1, 0.2, 0.1, 0.3, 0.5, NA, NA), Gene2 = c(0.1, 
    0.2, 0.1, 0.3, 0.5, 0.2, 0.2), Gene3 = c(0.1, 0.2, 0.1, 0.3, 
    0.5, 0.5, 0.5)), class = "data.frame", row.names = c(NA, 
-7L))

And here is the second one:

structure(list(CellType = structure(c(1L, 1L, 2L, 3L, 4L), .Label = c("CellD", 
"CellX", "CellY", "CellZ"), class = "factor"), Gene1 = c(10L, 
20L, 20L, 30L, 10L), Gene2 = c(0.1, 0.2, 0.1, 0.3, 0.5), Gene3 = c(0.1, 
0.2, 0.1, 0.3, 0.5)), class = "data.frame", row.names = c(NA, 
-5L))

I would like to add items in 'Gene 1' column that match in 'Cell Type' column from the second data frame to the first one. In this case, only two cell type items match which are 'CellD'. Is there a way for me to add the corresponding values of '10' and '20' from the Gene 1 column of my second dataframe into my 'NA' fields in dataframe # 1?

Thanks!

Hi @Adam52: Did you want to keep the tables separately, or combine them into a new table?

And in your sample data, the two CellD rows are identical, so one could freely insert the values 10 and 20 where there are currently NA's -- do you expect this to always be the case?

@dromano thanks for following up. I would like to combine them into one single table such that I would retain only the first table with all NA values replaced with the ones from the second table.

Do you expect NA's to only occur in rows that are otherwise identical?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.