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 tabledefine 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 generatedcontinue 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.