,我有以下的數據幀:如何通過交替列名複製數據幀列
df <- structure(list(Vertebral = c(16.43, 1.06, 0.38), BoneMarrow = c(19.69,
1.16, 1.13)), .Names = c("Vertebral", "BoneMarrow"), row.names = c("Gnai3",
"Cdc45", "Cav2"), class = "data.frame")
df
#> Vertebral BoneMarrow
#> Gnai3 16.43 19.69
#> Cdc45 1.06 1.16
#> Cav2 0.38 1.13
我想要做的就是創建一個看起來像這樣的數據幀:
即對於每個原始列名稱,創建兩個副本:以'control'和'treated'作爲後綴。
Vertebral.control BoneMarrow.control Vertebral.Treated BoneMarrow.Treated
Gnai3 16.43 19.69 16.43 19.69
Cdc45 1.06 1.16 1.06 1.16
Cav2 0.38 1.13 0.38 1.13
我該怎麼做?
我堅持用這個代碼:
library(tidyverse)
subtype <- colnames(df)
expand.grid(subtype,c("Control","Treated")) %>%
mutate(new_col = paste0(Var1,".",Var2))
#> Var1 Var2 new_col
#> 1 Vertebral Control Vertebral.Control
#> 2 BoneMarrow Control BoneMarrow.Control
#> 3 Vertebral Treated Vertebral.Treated
#> 4 BoneMarrow Treated BoneMarrow.Treated
我可以請你?什麼功能!做??它的含義是什麼?我們如何參考它? – Onyambu
@Onyambu,看我的編輯和鏈接。 – mt1022