在不同长度的柱中融化数百个观察结果

时间:2015-06-05 05:31:36

标签: r dataframe melt

我有一些数据已经发给我,看起来像这样: enter image description here

每一行都是一项实验,控制了5个变量(前五列)并给出了存储在以下(数百个)X,X1 ......等列中的结果(读数)。

我想融化我的data.frame,以便我只有一个结果变量存储所有实验的所有读数,保留5个解释变量。问题是每个实验都产生了不同数量的读数。所以我不知道该怎么做。

我输入了以下数据框的摘录:

structure(list(dose = c(0, 0.65, 1.625, 3.25, 6.1), ponte = structure(c(1L, 
1L, 1L, 1L, 1L), .Label = c("P252224", "P312256"), class = "factor"), 
    jour = structure(c(3L, 3L, 3L, 3L, 3L), .Label = c("J1", 
    "J2", "J3"), class = "factor"), normalise = structure(c(1L, 
    1L, 1L, 1L, 1L), .Label = "non", class = "factor"), box = structure(c(1L, 
    1L, 1L, 1L, 1L), .Label = c("FB1", "FB2"), class = "factor"), 
    X = c(4653.625, 3965.312, 7922.779, 6688.122, 9635.559), 
    X.1 = c(4020.349, 4630.506, 8388.648, 7496.008, NA), X.2 = c(NA, 
    4695.361, 6832.299, 8592.608, NA), X.3 = c(NA, NA, 7504.947, 
    9375.783, NA), X.4 = c(NA, NA, 8391.027, 9080.961, NA), X.5 = c(NA, 
    NA, NA, 10213.631, NA)), .Names = c("dose", "ponte", "jour", 
"normalise", "box", "X", "X.1", "X.2", "X.3", "X.4", "X.5"
), row.names = c(NA, -5L), class = "data.frame")

1 个答案:

答案 0 :(得分:0)

此代码会删除名称以X开头的所有列,并添加X.All列,其中包含所有X列的行平均值。您没有提到如何聚合非NA X.n值,因此请将mean替换为您想要的函数:

df=structure(... your structure from example)
df=cbind(df[,setdiff(colnames(df),grep('^X',colnames(df),value = T))], X.All=apply(df[,grep('^X',colnames(df))], MARGIN=1, FUN=function(x) mean(x,na.rm=T)))