[R] sum column by colnames

Henrique Dallazuanna wwwhsd at gmail.com
Thu Jan 21 12:49:39 CET 2010


Work's for me:

sapply(unique(union(names(data1), names(data2))),
function(n)Reduce('+', m[grep(n, names(m))]))
                1           2          3           4          5           6
 [1,]   0.2481509   0.2481509   1.240754   0.7444526   0.000000   0.9926034
 [2,]   2.5269063   1.2634532   1.263453   2.5269063   1.263453   3.1118565
 [3,]   0.0000000   6.6856351   3.342818   1.6714088   0.000000   1.6714088
 [4,]   3.3955301   6.7910603   0.000000   0.0000000   3.395530   3.3955301
 [5,]  17.6053685  17.6053685   0.000000  10.4981500   0.000000   5.8684562
 [6,]   0.0000000   0.0000000   9.145319   0.0000000   0.000000   9.1453187
 [7,]  24.9346349   0.0000000  26.548650  24.9346349  46.641240   0.0000000
 [8,]  16.0344100   0.0000000  73.195722   0.0000000  48.103220  16.0344054
 [9,]  21.2623400  24.2592465   0.000000  45.5215865  63.787020  63.7870201
[10,]  54.7977900  62.3859139 155.964785 117.1837039  27.398900  58.5918536
[11,] 142.6301076 117.4078128  39.135938   0.0000000   0.000000  34.4980557
[12,]   0.0000000  96.2453820   0.000000  48.1226910 133.348991  42.6131524
[13,]   0.0000000  58.1866603   0.000000 109.9836503   0.000000  51.7969933
[14,]  62.1019500   0.0000000   0.000000   0.0000000  62.101950   0.0000000
[15,] 163.3521313   0.0000000  81.676066   0.0000000   0.000000   0.0000000
[16,]   0.0000000   0.0000000   0.000000   0.0000000   0.000000   0.0000000
[17,]   0.0000000   0.0000000 109.858092   0.0000000   0.000000   0.0000000
[18,]   0.0000000   0.0000000   0.000000   0.0000000   0.000000   0.0000000
[19,]   0.0000000   0.0000000   0.000000 142.9800268   0.000000 142.9800268
[20,]          NA   0.0000000   0.000000          NA         NA          NA
[21,]          NA 202.4565276   0.000000          NA         NA          NA
[22,]          NA   0.0000000   0.000000          NA         NA          NA

On Thu, Jan 21, 2010 at 9:02 AM, Alfredo Alessandrini
<alfreale74 at gmail.com> wrote:
> I've try it...
>
> But don't work.
>
>> data1
>              1           2          3           4         5           6
> 5     0.2481509   0.2481509   1.240754   0.7444526  0.000000   0.9926034
> 10    2.5269063   1.2634532   1.263453   2.5269063  1.263453   2.5269063
> 15    0.0000000   6.6856351   3.342818   1.6714088  0.000000   1.6714088
> 20    3.3955301   6.7910603   0.000000   0.0000000  3.395530   3.3955301
> 25   17.6053685  17.6053685   0.000000   0.0000000  0.000000   5.8684562
> 30    0.0000000   0.0000000   9.145319   0.0000000  0.000000   9.1453187
> 35   13.2743249   0.0000000  26.548650  13.2743249  0.000000   0.0000000
> 40    0.0000000   0.0000000  73.195722   0.0000000  0.000000   0.0000000
> 45    0.0000000  24.2592465   0.000000  24.2592465  0.000000   0.0000000
> 50    0.0000000  62.3859139 155.964785  62.3859139  0.000000  31.1929569
> 55   39.1359376 117.4078128  39.135938   0.0000000  0.000000   0.0000000
> 60    0.0000000  96.2453820   0.000000  48.1226910 48.122691   0.0000000
> 65    0.0000000  58.1866603   0.000000  58.1866603  0.000000   0.0000000
> 70    0.0000000   0.0000000   0.000000   0.0000000  0.000000   0.0000000
> 75  163.3521313   0.0000000  81.676066   0.0000000  0.000000   0.0000000
> 80    0.0000000   0.0000000   0.000000   0.0000000  0.000000   0.0000000
> 85    0.0000000   0.0000000 109.858092   0.0000000  0.000000   0.0000000
> 90    0.0000000   0.0000000   0.000000   0.0000000  0.000000   0.0000000
> 95    0.0000000   0.0000000   0.000000 142.9800268  0.000000 142.9800268
> 100 161.4695650   0.0000000   0.000000   0.0000000  0.000000   0.0000000
> 105   0.0000000   0.0000000   0.000000   0.0000000  0.000000   0.0000000
> 110   0.0000000 202.4565276   0.000000   0.0000000  0.000000   0.0000000
>> data2
>           1        4        5          6
> 5    0.00000  0.00000  0.00000  0.0000000
> 10   0.00000  0.00000  0.00000  0.5849502
> 15   0.00000  0.00000  0.00000  0.0000000
> 20   0.00000  0.00000  0.00000  0.0000000
> 25   0.00000 10.49815  0.00000  0.0000000
> 30   0.00000  0.00000  0.00000  0.0000000
> 35  11.66031 11.66031 46.64124  0.0000000
> 40  16.03441  0.00000 48.10322 16.0344054
> 45  21.26234 21.26234 63.78702 63.7870201
> 50  54.79779 54.79779 27.39890 27.3988967
> 55 103.49417  0.00000  0.00000 34.4980557
> 60   0.00000  0.00000 85.22630 42.6131524
> 65   0.00000 51.79699  0.00000 51.7969933
> 70  62.10195  0.00000 62.10195  0.0000000
> 75   0.00000  0.00000  0.00000  0.0000000
> 80   0.00000  0.00000  0.00000  0.0000000
> 85   0.00000  0.00000  0.00000  0.0000000
> 90   0.00000  0.00000  0.00000  0.0000000
> 95   0.00000  0.00000  0.00000  0.0000000
>> m <- merge(data1, data2, by = 0, all = TRUE, sort = FALSE)
>> m
>   Row.names         1.x           2          3         4.x       5.x
> 1          5   0.2481509   0.2481509   1.240754   0.7444526  0.000000
> 2         10   2.5269063   1.2634532   1.263453   2.5269063  1.263453
> 3         15   0.0000000   6.6856351   3.342818   1.6714088  0.000000
> 4         20   3.3955301   6.7910603   0.000000   0.0000000  3.395530
> 5         25  17.6053685  17.6053685   0.000000   0.0000000  0.000000
> 6         30   0.0000000   0.0000000   9.145319   0.0000000  0.000000
> 7         35  13.2743249   0.0000000  26.548650  13.2743249  0.000000
> 8         40   0.0000000   0.0000000  73.195722   0.0000000  0.000000
> 9         45   0.0000000  24.2592465   0.000000  24.2592465  0.000000
> 10        50   0.0000000  62.3859139 155.964785  62.3859139  0.000000
> 11        55  39.1359376 117.4078128  39.135938   0.0000000  0.000000
> 12        60   0.0000000  96.2453820   0.000000  48.1226910 48.122691
> 13        65   0.0000000  58.1866603   0.000000  58.1866603  0.000000
> 14        70   0.0000000   0.0000000   0.000000   0.0000000  0.000000
> 15        75 163.3521313   0.0000000  81.676066   0.0000000  0.000000
> 16        80   0.0000000   0.0000000   0.000000   0.0000000  0.000000
> 17        85   0.0000000   0.0000000 109.858092   0.0000000  0.000000
> 18        90   0.0000000   0.0000000   0.000000   0.0000000  0.000000
> 19        95   0.0000000   0.0000000   0.000000 142.9800268  0.000000
> 20       105   0.0000000   0.0000000   0.000000   0.0000000  0.000000
> 21       110   0.0000000 202.4565276   0.000000   0.0000000  0.000000
> 22       100 161.4695650   0.0000000   0.000000   0.0000000  0.000000
>           6.x       1.y      4.y      5.y        6.y
> 1    0.9926034   0.00000  0.00000  0.00000  0.0000000
> 2    2.5269063   0.00000  0.00000  0.00000  0.5849502
> 3    1.6714088   0.00000  0.00000  0.00000  0.0000000
> 4    3.3955301   0.00000  0.00000  0.00000  0.0000000
> 5    5.8684562   0.00000 10.49815  0.00000  0.0000000
> 6    9.1453187   0.00000  0.00000  0.00000  0.0000000
> 7    0.0000000  11.66031 11.66031 46.64124  0.0000000
> 8    0.0000000  16.03441  0.00000 48.10322 16.0344054
> 9    0.0000000  21.26234 21.26234 63.78702 63.7870201
> 10  31.1929569  54.79779 54.79779 27.39890 27.3988967
> 11   0.0000000 103.49417  0.00000  0.00000 34.4980557
> 12   0.0000000   0.00000  0.00000 85.22630 42.6131524
> 13   0.0000000   0.00000 51.79699  0.00000 51.7969933
> 14   0.0000000  62.10195  0.00000 62.10195  0.0000000
> 15   0.0000000   0.00000  0.00000  0.00000  0.0000000
> 16   0.0000000   0.00000  0.00000  0.00000  0.0000000
> 17   0.0000000   0.00000  0.00000  0.00000  0.0000000
> 18   0.0000000   0.00000  0.00000  0.00000  0.0000000
> 19 142.9800268   0.00000  0.00000  0.00000  0.0000000
> 20   0.0000000        NA       NA       NA         NA
> 21   0.0000000        NA       NA       NA         NA
> 22   0.0000000        NA       NA       NA         NA
>> sapply(unique(union(names(data1), names(data2))), function(n)Reduce('+', m[grep(n, names(m))]))
> list()
>>
>
>
> 2010/1/20 Henrique Dallazuanna <wwwhsd at gmail.com>:
>> Try this:
>>
>>> m <- merge(data1, data2, by = 0, all = TRUE, sort = FALSE)
>>> sapply(unique(union(names(data1), names(data2))), function(n)Reduce('+', m[grep(n, names(m))]))
>>
>> On Wed, Jan 20, 2010 at 10:59 AM, Alfredo Alessandrini
>> <alfreale74 at gmail.com> wrote:
>>> Hi,
>>>
>>> I've this dataframes:
>>>
>>>> data1
>>>             1        2 3         4             5                6
>>> 5    0.4963017 0 0 0.2481509  1.9852069 0.4963017
>>> 10   0.0000000 0 0 0.0000000  0.6317266 0.0000000
>>> 15   0.0000000 0 0 0.0000000  0.0000000 0.0000000
>>> 20   0.0000000 0 0 3.3955301  0.0000000 0.0000000
>>> 25   0.0000000 0 0 0.0000000  0.0000000 0.0000000
>>> 30   0.0000000 0 0 0.0000000  0.0000000 0.0000000
>>> 35   0.0000000 0 0 0.0000000  0.0000000 0.0000000
>>>
>>>> data2
>>>           1        4              5                6
>>> 5    0.00000  0.00000  0.00000  0.0000000
>>> 10   0.00000  0.00000  0.00000  0.5849502
>>> 15   0.00000  0.00000  0.00000  0.0000000
>>> 20   0.00000  0.00000  0.00000  0.0000000
>>> 25   0.00000 10.49815  0.00000  0.0000000
>>>
>>> Can I sum the dataframe, by colnames?
>>>
>>> The sum is:
>>>
>>>             1        2 3         4             5                6
>>> 5    0.4963017 0 0 0.2481509  1.9852069 0.4963017
>>> 10   0.0000000 0 0 0.0000000  0.6317266 0.5849502
>>> 15   0.0000000 0 0 0.0000000  0.0000000 0.0000000
>>> 20   0.0000000 0 0 3.3955301  0.0000000 0.0000000
>>> 25   0.0000000 0 0 10.498150   0.0000000 0.0000000
>>> 30   0.0000000 0 0 0.0000000  0.0000000 0.0000000
>>> 35   0.0000000 0 0 0.0000000  0.0000000 0.0000000
>>>
>>>
>>> Thanks,
>>>
>>> Alfredo
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>
>>
>> --
>> Henrique Dallazuanna
>> Curitiba-Paraná-Brasil
>> 25° 25' 40" S 49° 16' 22" O
>>
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O



More information about the R-help mailing list