English 中文(简体)
R 用 ddply 使用 t
原标题:R using t with ddply
  • 时间:2012-05-24 17:40:38
  •  标签:
  • r
  • plyr

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

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

有办法用 < code> > NA < /code > 填充缺失的 < code > B4 < /code > 行还是忽略它? < code> t < /code > 函数或 < code> ddply 似乎窒息了,因为 < code > B < /code > 的长度与其他的长度不同。

Thanks, J--

最佳回答

@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

如果水平标签毫无意义, 您只想填充您曾经有多少值, 您可以使用 < code> 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

您想要的并不完全清楚, 因为您示例中的行被贴上好字母的标签, 而代码意味着以复合名称分割。 不清楚您真的要什么 。

问题回答

您希望您的行和列成为 Well 列中的字母和数字正确吗? 您可以将这些分成两个新列 :

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

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

然后我就从 reshape2 套件中 使用 dcast 包件:

dcast(df, well.letter~well.number, value.var= Result )




相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...