I am having a problem in R with ranking countries by their average consumption income in 1950 using the Penn World Table Data. I have multiple years (i.e. from 1950-1959) in the data for all the countries.
The main thing I want to do is rank income for countries relative to other countries by year. I tried to use ifelse() and percent_rank() functions together but it didn’t work correctly (it just ranks a country’s income relative to other countries' income in all of the years).
Below is the R code:
install.packages("pwt10","dplyr")
library(pwt10)
data(pwt10.01)
library(dplyr)
PWT1950 <- subset(pwt10.01, pwt10.01$year %in% c("1950","1951","1952","1953","1954","1955","1956","1957","1958","1959") & i_cig %in% c("benchmark","extrapolated","interpolated") & i_xr=="market")
PWT1950$IncomeRank <- ifelse(PWT1950$year==1950,percent_rank(PWT1950$ccon/PWT1950$pop),
ifelse(PWT1950$year==1951,percent_rank(PWT1950$ccon/PWT1950$pop),
ifelse(PWT1950$year==1952,percent_rank(PWT1950$ccon/PWT1950$pop),
ifelse(PWT1950$year==1953,percent_rank(PWT1950$ccon/PWT1950$pop),
ifelse(PWT1950$year==1954,percent_rank(PWT1950$ccon/PWT1950$pop),
ifelse(PWT1950$year==1955,percent_rank(PWT1950$ccon/PWT1950$pop),
ifelse(PWT1950$year==1956,percent_rank(PWT1950$ccon/PWT1950$pop),
ifelse(PWT1950$year==1957,percent_rank(PWT1950$ccon/PWT1950$pop),
ifelse(PWT1950$year==1958,percent_rank(PWT1950$ccon/PWT1950$pop),
ifelse(PWT1950$year==1959,percent_rank(PWT1950$ccon/PWT1950$pop),
percent_rank(PWT1950$ccon/PWT1950$pop)))))))))))
I was expecting that after inputting the commands listed previously that this command subset(PWT1950,year==1959,select=c(IncomeRank,country)) would give me same result:
PWT1959 <- subset(pwt10.01,year=="1959" & i_cig %in% c("benchmark","extrapolated","interpolated") & i_xr=="market")
PWT1959$IncomeRank <- percent_rank(PWT1959$ccon/PWT1959$pop)
subset(PWT1959,select=c(IncomeRank,country))
Considering the two commands are giving me different results I suspect I made a mistake earlier.
Thanks to @GregorThomas solution to this question was fund. What fixed the issue was to use:
instead of:
For Comparing Nominal Incomes the command is
PWT1950 |> mutate(Nominal_Income_Rank = percent_rank(ccon * pl_con / pl_con[isocode == "USA"] / pop))Thanks for All The Help GregorThomas. I seriously appreciate it.