我有一个相当大的 data.frame,我必须对其进行一些处理。目前的结构是:
V1 V2 V3 V4 V5 V6 V7 V8 ... Vn Vn+1
chr1 1 A T sample_1 value_1 sample_2 value_4 ... sample_n value_7
chr1 40 T C sample_1 value_2 sample_2 value_5 ... sample_n value_8
chr1 60 A T sample_1 value_3 sample_2 value_6 ... sample_n value_9
.
.
.
chrX 160 A T sample_1 value_x sample_2 value_y ... sample_n value_ni
例如对于 data_frame:
df <- structure(list(V1 = c(10L, 10L, 10L, 10L, 10L, 10L), V2 = c(3387501L,
4174142L, 6419754L, 6419765L, 6419897L, 6419912L), V3 = c("T",
"A", "C", "T", "G", "A"), V4 = c("A",
"T", "A", "A", "C", "G"), V5 = c("LP2000748-DNA_H02",
"LP2000748-DNA_H02", "LP2000748-DNA_H02", "LP2000748-DNA_H02",
"LP2000748-DNA_H02", "LP2000748-DNA_H02"), V6 = c("0/0", "0/0",
"1/1", "0/0", "0/0", "0/0"), V7 = c("LP2000748-DNA_A03", "LP2000748-DNA_A03",
"LP2000748-DNA_A03", "LP2000748-DNA_A03", "LP2000748-DNA_A03",
"LP2000748-DNA_A03"), V8 = c("0/0", "0/0", "1/1", "0/1", "0/0",
"0/0"), V9 = c("LP2000795-DNA_B01", "LP2000795-DNA_B01", "LP2000795-DNA_B01",
"LP2000795-DNA_B01", "LP2000795-DNA_B01", "LP2000795-DNA_B01"
), V10 = c("0/0", "0/0", "1/1", "0/0", "0/0", "0/0")), row.names = c(NA,
-6L), class = c("data.table", "data.frame"))
我最终想要的是这样的表格:
V1 V2 V3 V4 sample_1 sample_2 ... sample_n
chr1 1 A T value_1 value_4 ... value_7
chr1 40 T C value_2 value_5 ... value_8
chr1 60 A T value_3 value_6 ... value_9
.
.
.
chrX 160 A T value_x value_y ... value_ni
到目前为止,我在 R 中尝试过的是:
samples_data <- seq(from = 5, to = dim(df)[2],by=2) variable_data <- samples_data + 1
new_df <- reshape2::dcast(df, V1 + V2 + V3 ~ colnames(df)[samples_data], value.var= colnames(df)[variable_data])
但我收到此错误消息:
recursive indexing failed at level 2 In addition: Warning message: In if (!(value.var %in% names(data))) { : the condition has length > 1 and only the first element will be used
有没有人对如何解决这个问题或如何重塑 df 有任何建议?
谢谢!