Say I have a data frame that looks like this:
sample tissue disease
s1 brain glioblastoma
s2 skin melanoma
s3 liver NA
s4 kidney carcinoma
s5 nerve NA
How would I go about replacing all NAs with "Normal [corresponding tissue]"? E.g., for sample 3, NA would be replaced with "liver normal" and for sample 5 NA would be replaced with "nerve normal".
I appreciate any help!
CodePudding user response:
We create a logical vector based on the NA occurrence in 'disease' and then paste
the corresponding 'tissue' elements with 'normal' to update the 'disease' column
i1 <- is.na(df1$disease)
df1$disease[i1] <- paste(df1$tissue[i1], "normal")
Or more concisely in data.table
library(data.table)
setDT(df1)[is.na(disease), disease := paste0(tissue, 'normal')]