[R] Merging vector data into one file
Mark Altaweel
maltaweel at anl.gov
Mon Feb 1 05:22:32 CET 2010
Hi,
I had another question. If you had say a vector (e.g., called data)
with 235 elements and each element looked like the following
data[[1]]
Column A-B Column Z-S Column A-S....
1 2 5 .......
data[[2]]
Column Z-B Column A-S Column A-B....
2 1 3 .......
Anyway, each element consists of one row that lists the names of the
columns and the second row is the counts of those columns. What I
wanted to do is merge all the elements from the vector so that I
aggregate the counts for every column in the vector elements. So if a
column name (e.g., Column A-B) is present in two elements, then I
would want those elements that have the same column name to aggregated
their counts; however, if the column name is unique then I simply just
want to integrate that column with the total. The example below shows
the result of what I mean using the two elements above:
result=data[[1]] + data[[2]].......
Column A-B Column Z-S Column A-S Column Z-B.........
4 2 6 2.......
So what I want would take the 235 elements, aggregate the column names
and counts, and produce one output variable (e.g., called result)
that has all the column names and counts present in the 235 elements.
I tried using sapply, (e.g., sapply(data,function(.df){sum(.df)}) ),
but this only just provided aggregate counts without producing the
column names. I tried an aggregate() function, but that didnt
aggregate my data exactly the way I wanted, perhaps I got the syntax
wrong though. Anyway, is there a better and easier way to do this?
Thanks in a advance again.
Mark
More information about the R-help
mailing list