2

Basically what I need to perform is to transform my country variables into per capita terms, i.e. divide all values by the country's population.

So I have df:

country <- c("A","B","C","D")
income <- c(10,20,30,40)
cars <- c(100,200,300,400)
df <- data.frame(country,income,cars)

And I want to divide all columns by dfpop$pop

pop <- c(1,2,3,4)

dfpop <- data.frame(country,pop)

any ideas?

j0k
  • 22,600
  • 28
  • 79
  • 90
Lucarno
  • 393
  • 4
  • 14
  • 2
    Without some [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) data and code, its hard to say how best to do this. Please edit your question accordingly using something like `dput(head(Y))`. However, my guess would be you should add the column of population to data.frame `y` and proceed from there – Justin Aug 23 '12 at 14:44

1 Answers1

3

Since you only have one observation per country there's not need of aggregating data, just a division using df/variable

Taking into account merge pointed out above by @lselzer you can try:

 DF <- merge(df, dfpop)
 DF[,-c(1,4)]/DF[,4]
  income cars
1     10  100
2     10  100
3     10  100
4     10  100

But also without using merge:

df[,-1]/pop
  income cars
1     10  100
2     10  100
3     10  100
4     10  100

The advantage of using merge is the matching of country names, this will ensure that you're dividing each variable of country A by the population of country A. The second approach does not ensures the matching of country variables, so you have to take care using this approach.

Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138