1

if I have a table that looks like this (read in with read.table):

http://i.stack.imgur.com/gaef6.png

(The 0s are placeholders so I have a consistent number of rows)

Is there a way that I can add values with the same coresponding names (both values for a, all 4 values for l) and output it as a data frame? Also, the rows are not consistent (i.e. some columns have 4 a's, some have 2)

The result should look like this:

http://i.stack.imgur.com/HrCt5.png

I can assemble the data frame with a, b etc as row names and the columns once the values are summed, but I cannot figure out how to sum them according to their corresponding names.

This is how I am currently approaching this:

  • read.table() for my table

  • define empty data.frame

  • use loop to extract and add column values by corresponding names? <-need help with this

  • cbind() data frame and column I just generated

  • continue through end of loop

-use row.names() and colnames() to change row and column names

My code so far:

setwd(wd)
read.table(dat.txt,sep="\t")->x
read.table(total.txt, sep="\t")->total
met<-c("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r")
y<-data.frame(),
total[2,]->name
for g in 1:ncol(x){
  #column will be the column with the combined values according to name
  cbind(y,column)
}
row.names(y)<-met
colnames(y)<-name
write.table(y,file="data.txt",sep="\t")

Any help would be much appreciated.

4

1 回答 1

0

您可以将其分为两部分:

(1) 聚合每一列 (2) 组合成一张漂亮的表格

使用 *apply 可以很好地完成它。我将其分解为 for 循环以使其更易于阅读

  ## sample df 
  df <- structure(list(thing1.met = c("a", "a", "b", "b", "c", "c", "d", "d", "e", "e", "f", "f", "g", "g", "h", "h", "i", "i", "j", "j", "k", "k", "l", "l", "m", "n", "o", "p", "q", "r"), thing1 = c(-0.57, 0.42, -1.12, -0.5, -0.94, -1.87, -2.22, -0.33, 1.45, 0.46, -1.96, -0.35, -1.01, 0.72, 0.04, -0.21, 0.81, 1.28, -0.52, -1.19, 0.03, -1.71, 0.53, -1.96, 1.58, -0.1, -0.88, 0.92, 0.02, -0.91), thing2.met = c("a", "a", "a", "b", "b", "c", "c", "c", "d", "e", "e", "f", "f", "g", "g", "h", "h", "i", "i", "j", "j", "k", "l", "l", "m", "n", "o", "p", "q", "r"), thing2 = c(-0.06, -0.7, 0.16, 1.96, 0.78, -0.65, -0.17, 0.89, 0.68, -0.93, -1.44, -0.16, -0.52, -0.19, 1.15, -0.77, 0.69, -0.48, 1.75, 1.62, -0.68, -1.06, -1.2, 1.42, -0.2, 1.33, 2.24, 0.35, 2, -1.21), thing3.met = c("a", "a", "a", "b", "c", "c", "d", "d", "e", "e", "f", "f", "g", "g", "g", "g", "g", "i", "k", "l", "l", "l", "m", "o", "o", "o", "p", "p", "p", "q"),     thing3 = c(1.27, 4.45, -2.42, -9.53, 3.33, 5.58, -2.94, 2.54,     12.44, 12.41, 7.6, 0.63, 5.67, -3.79, 12.28, 1.77, -0.4,     -0.04, 0.95, 4.93, 1.77, 0.37, -2.79, 2.36, 12.76, -5.4,     -4.73, -1.8, 0.52, -4.97)), .Names = c("thing1.met", "thing1", "thing2.met", "thing2", "thing3.met", "thing3"), row.names = c(NA, -30L), class = "data.frame")

  # create blank data frame
  results <- data.frame(row.names=met)

  # grab every other column
  cols <- seq(2, ncol(df), 2) 

  # aggregate over all m, over every pair of columns
  for (m in met) { 
    for (c in cols) {
      results[m, c/2] <- sum(df[,c][df[,c-1]==m])
    }
  }

  # clean up column names
  names(results) <- names(df)[cols]

  # final output
  results

例子:

    > results
       thing1 thing2 thing3
    a  -0.15  -0.60   3.30
    b  -1.62   2.74  -9.53
    c  -2.81   0.07   8.91
    d  -2.55   0.68  -0.40
    e   1.91  -2.37  24.85
    f  -2.31  -0.68   8.23
    g  -0.29   0.96  15.53
    h  -0.17  -0.08   0.00
    i   2.09   1.27  -0.04
    j  -1.71   0.94   0.00
    k  -1.68  -1.06   0.95
    l  -1.43   0.22   7.07
    m   1.58  -0.20  -2.79
    n  -0.10   1.33   0.00
    o  -0.88   2.24   9.72
    p   0.92   0.35  -6.01
    q   0.02   2.00  -4.97
    r  -0.91  -1.21   0.00
于 2012-11-29T00:07:56.110 回答