在查看不同的线程时,我了解到 stats 包中的函数 reshape。我在一个“虚拟”数据集上使用它没有问题,并成功将其从长格式转换为宽格式。然而,我不知道为什么它在我的数据上不起作用,因为它和我使用的对象几乎相同,数据类型也相似。我希望你们能帮助我找出它行为异常的原因。
无论如何,这个没有问题:
> df <- data.frame(
+ year = c(rep(2000, 12), rep(2001, 12)),
+ month = rep(1:12, 2),
+ values = rnorm(24)
+ )
# year month values
1 2000 1 1.52435428
2 2000 2 -0.89394797
3 2000 3 0.75965499
4 2000 4 1.21497443
转换为宽格式:
df_wide <- reshape(df, idvar="year", timevar="month", v.names="values", direction="wide")
# year values_1 values_2 values_3 values_4 values_5 values_6 values_7 values_8 values_9 values_10 values_11 values_12
1 2000 1.524354 -0.893948 0.759655 1.2149744 -1.3237634 -0.08681768 0.5208436 -0.2602807 0.6378904 -0.9852600 -1.128048 -0.1466028
2 2001 1.913969 -1.966720 -0.947688 0.8375891 -0.1015944 1.11812723 -1.5164472 -0.7089485 0.5975851 0.2514546 -1.578210 -0.9044418
但是当使用我的数据时,它看起来就像这样:
my_df <- dput(head(experiment, 30))
structure(list(transcript = c("TR100743-c0_g1_i3", "TR100743-c0_g1_i3",
"TR100743-c0_g1_i3", "TR100743-c0_g1_i3", "TR100743-c0_g1_i3",
"TR100987-c0_g1_i2", "TR100987-c0_g1_i2", "TR100987-c0_g1_i2",
"TR100987-c0_g1_i2", "TR100987-c0_g1_i2", "TR101301-c4_g1_i16",
"TR101301-c4_g1_i16", "TR101301-c4_g1_i16", "TR101301-c4_g1_i16",
"TR101301-c4_g1_i16", "TR102190-c1_g1_i1", "TR102190-c1_g1_i1",
"TR102190-c1_g1_i1", "TR102190-c1_g1_i1", "TR102190-c1_g1_i1",
"TR102346-c0_g2_i1", "TR102346-c0_g2_i1", "TR102346-c0_g2_i1",
"TR102346-c0_g2_i1", "TR102346-c0_g2_i1", "TR102352-c4_g2_i5",
"TR102352-c4_g2_i5", "TR102352-c4_g2_i5", "TR102352-c4_g2_i5",
"TR102352-c4_g2_i5"), hours = c(0, 2, 8, 24, 48, 0, 2, 8, 24,
48, 0, 2, 8, 24, 48, 0, 2, 8, 24, 48, 0, 2, 8, 24, 48, 0, 2,
8, 24, 48), exp.change = c(NA, -43.1958273184645, -61.3014008509066,
964.925115099619, -52.7060728326392, NA, -46.2563848585369, 3.29396898799807,
-99.9994681489801, 106710484.025972, NA, -29.6341333478577, 522.224859380388,
40.4737694947169, -1.34388206141046, NA, -18.7670826937756, 5.49472822880452,
55.1072690537026, 33.5824607349752, NA, -99.999962131178, 789697313.24393,
18.6337471833012, 52.4442959208125, NA, -31.3334122297108, 9.64745757892995,
28.48552519881, 70.5808772231999), response = c("Primary", "Primary",
"Primary", "Primary", "Primary", "Primary", "Primary", "Primary",
"Primary", "Primary", "Primary", "Primary", "Primary", "Primary",
"Primary", "Tertiary", "Tertiary", "Tertiary", "Tertiary", "Tertiary",
"Primary", "Primary", "Primary", "Primary", "Primary", "Primary",
"Primary", "Primary", "Primary", "Primary")), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -30L))
# transcript hours exp.change response
1 TR100743-c0_g1_i3 0 NA Primary
2 TR100743-c0_g1_i3 2 -43.2 Primary
3 TR100743-c0_g1_i3 8 -61.3 Primary
4 TR100743-c0_g1_i3 24 965. Primary
5 TR100743-c0_g1_i3 48 -52.7 Primary
6 TR100987-c0_g1_i2 0 NA Primary
7 TR100987-c0_g1_i2 2 -46.3 Primary
8 TR100987-c0_g1_i2 8 3.29 Primary
9 TR100987-c0_g1_i2 24 -100.0 Primary
10 TR100987-c0_g1_i2 48 106710484. Primary
当我尝试“重塑”它时,出现了这个错误:
my_df_wide <- reshape(my_df, idvar = c("transcript", "response"), timevar = "hours", v.names="exp.change", direction = "wide")
# transcript response `exp.change.c(0, 2, 8, 24, 48)`
1 TR100743-c0_g1_i3 Primary NA
2 TR100987-c0_g1_i2 Primary NA
3 TR101301-c4_g1_i16 Primary NA
4 TR102190-c1_g1_i1 Tertiary NA
5 TR102346-c0_g2_i1 Primary NA
6 TR102352-c4_g2_i5 Primary NA
7 TR10396-c0_g1_i6 Primary NA
8 TR11844-c0_g2_i1 Secondary NA
9 TR12672-c1_g2_i1 Primary NA
10 TR12672-c1_g2_i2 Primary NA
是因为空值吗?我真的不知道为什么它会表现得这样...非常感谢任何帮助。