-5

Possible Duplicate:
Idiomatic R code for partitioning a vector by an index and performing an operation on that partition
How to calculate median of profits for a particular country

I am trying to find average of a column by using another column in R.It's so easy to do it in SQL but not able to find proper functions and implement them.Here is some sample data as below.

data("Forbes2000", package = "HSAUR")
head(Forbes2000)

##    rank                name        country             category  sales profits  assets marketvalue
## 1    1           Citigroup  United States              Banking  94.71   17.85 1264.03      255.30
## 2    2    General Electric  United States        Conglomerates 134.19   15.59  626.93      328.54
## 3    3 American Intl Group  United States            Insurance  76.66    6.46  647.66      194.87
## 4    4          ExxonMobil  United States Oil & gas operations 222.88   20.96  166.99      277.02
## 5    5                  BP United Kingdom Oil & gas operations 232.57   10.27  177.57      173.54
## 6    6     Bank of America  United States              Banking  49.01   10.81  736.45      117.55
Community
  • 1
  • 1
Teja
  • 13,214
  • 36
  • 93
  • 155
  • 2
    THere are many ways of doing this. look at the `plyr`, `data.table`, `sqldf` packages (to mention a few), and in base R the functions `aggregate`, `tapply` and `ave`. There are a multitude of related question on SO. – mnel Aug 30 '12 at 01:14
  • 2
    You should try searching again, this question has been answered 100s of times on SO. Check out the `plyr`, `data.table` tags as well as the `ddply` function in particular, or `aggregate`, or `ave`, or any other number of functions. [This question](http://stackoverflow.com/questions/10748253/idiomatic-r-code-for-partitioning-a-vector-by-an-index-and-performing-an-operati/10748470#10748470O) provides a reasonably extensive list of most of your options and some relative timings for each. – Chase Aug 30 '12 at 01:16
  • 1
    Part of the negativity toword this question is probably due to the impossibility of importing that file with the usual read.* function in R. You can do it with `read.fwf` in the utils package but it's a pain. Learn to post the output of `dput()` applied to the head of your dataframe. – IRTFM Aug 30 '12 at 01:27
  • 2
    @Vutukuri, Less than an hour before this question was asked, I edited 2 of your questions to make them reproducible by adding `data("Forbes2000", package = "HSAUR")` ... – GSee Aug 30 '12 at 01:29
  • Also, one of [the answers](http://stackoverflow.com/a/12188188/967840) I gave would work for this if you substitute `mean` for `median` – GSee Aug 30 '12 at 01:33
  • Might help to tell us which 2 columns you had in mind. – neilfws Aug 30 '12 at 01:52

1 Answers1

2

With a data.frame named dat that looked like:

     rank              name  country       category  sales profits assets marketvalue
21     21   DaimlerChrysler Germany    Consumer_dur 157.13    5.12 195.58       47.43

Try (untested d/t the numerous spaces in the text preventing read.table from making sense of it):

aggregate(dat[ , c("sales", "profits", "assets", "marketvalue")],   # cols to aggregate
          dat["country"],                                           # group column
          FUN=mean)                          # aggregation function
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
IRTFM
  • 258,963
  • 21
  • 364
  • 487