我有一個數據幀,名爲dd2
。我需要粘貼Left.Gene.Symbols
和Right.Gene.Symbols
中的值,我可以通過簡單地使用下面的代碼來完成這些操作,但如果缺少值,我不希望粘貼NDA。我希望它看起來像在combination
列中,如result
所示。忽略NA值,同時在R中粘貼兩個列值
mycode的
#to remove NAs
dd2[dd2 == 'NA'] <- NA
#pasting values together
result <- cbind(dd2,combination = paste(dd2[,"Left.Gene.Symbols"],dd2[,"Right.Gene.Symbols"],sep="*"))
數據
dd2<- structure(c("AMLM12001KP", "AMLM12001KP", "AMLM12001KP", "AMLM12001KP",
"AMLM12001KP", "AK2", "HFM1", "HFM1", "HFM1", "HFM1", NA, "PPT",
NA, "GGT", NA), .Dim = c(5L, 3L), .Dimnames = list(NULL, c("customer_sample_id",
"Left.Gene.Symbols", "Right.Gene.Symbols")))
結果
customer_sample_id Left.Gene.Symbols Right.Gene.Symbols combination
[1,] "AMLM12001KP" "AK2" NA AK2*
[2,] "AMLM12001KP" "HFM1" "PPT" HFM1*PPT
[3,] "AMLM12001KP" "HFM1" NA HFM1*
[4,] "AMLM12001KP" "HFM1" "GGT" HFM1*GGT
[5,] "AMLM12001KP" "HFM1" NA HFM1*
@RonakShah對不起只是糾正了。 – MAPK