1

我需要像这样转换一些数据:

df<-data.frame(Plate=c("4660", "4660", "4660", "4660", "4660", "4660", "4660", "4660", "4660", "4660", "4660"), Well=c("A1", "A2", "A3", "A4", "B1", "B2", "B3", "C1", "C2", "C3", "C4"), Result=c(1, 10, 100, 1000, 1, 10, 100, 1, 10, 100, 1000), Compound=c("C1", "C1", "C1", "C1", "C2", "C2", "C2", "C3", "C3", "C3", "C3"))
cmpds <- ddply(df, .(Compound), .fun = "t")

我想要结束的是:

   1     2     3     4
A  1     10    100   1000
B  1     10    100   NA
C  1     10    100   1000

有没有办法用或忽略它来填充缺失的B4行?NAt功能或ddply似乎因B长度与其他长度不同而令人窒息。

谢谢,J--

4

2 回答 2

3

像@Justin 一样,我假设您的列名来自井规范的数字部分。如果是这样,这里有一个稍微更通用的解决方案(适用于非单个数字和非单个字母,嗯,字母。

library("gsubfn")
library("reshape2")

wells <- strapply(as.character(df$Well), ".*([A-Z]+)([0-9]+)", c, simplify=rbind)
colnames(wells) <- c("well.letter", "well.number")
df <- cbind(df, wells)

然后使用dcast

> dcast(df, Compound~well.number, value.var="Result")
  Compound 1  2   3    4
1       C1 1 10 100 1000
2       C2 1 10 100   NA
3       C3 1 10 100 1000

如果水平标签没有意​​义,而你只想填写你有多少个值,你可以这样做plyr

ddply(df, .(Compound), function(DF) {
  as.data.frame(t(DF$Result))
})

这使

  Compound V1 V2  V3   V4
1       C1  1 10 100 1000
2       C2  1 10 100   NA
3       C3  1 10 100 1000

您想要的并不是很清楚,因为您的示例中的行标有井字母,而代码暗示按复合名称拆分。不确定你真正想要的是哪个。

于 2012-05-24T18:10:25.833 回答
1

您希望您的行和列是 Well 列中的字母和数字吗?您可以将它们拆分为两个新列:

well.split <- strsplit(df$Well, '')

df$well.letter <- sapply(well.split, '[', 1)
df$well.number <- sapply(well.split, '[', 2)

然后我会dcastreshape2包中使用:

dcast(df, well.letter~well.number, value.var='Result')
于 2012-05-24T17:50:22.577 回答