R - Function to create a data.frame containing manipulated data from another data.frame -
hi new r , have question. have data.frame (df) containing 30 different types of statistics years 1960-2012 100 different countries. here example of looks like:
country statistic.type 1960 1961 1962 1963 ... 2012 __________________________________________________________________________________ 1 albania death rate 10 21 13 24 25 2 albania birth rate 7 15 6 10 9 3 albania life expectancy 8 12 10 7 20 4 albania population 10 30 27 18 13 5 brazil death rate 14 20 22 13 18 6 brazil birth rate ... 7 brazil life expectancy ... 8 brazil population ... 9 cambodia death rate ... 10 cambodia birth rate ... etc...
note there 55 columns in total , values in each of 53 year columns made purposes of question.
i need writing function takes inputs country , statistic type , returns new data.frame 2 columns shows year , value in each year given country , statistic type. example, if input country=brazil , statistic.type=death rate function, new data.frame should like:
year value _____________________ 1 1960 14 2 1961 20 3 1962 22 ... 51 2012 18
i have no idea on how this, if can give me ideas/code/packages install helpful.
thank much!
if df
data.frame, need this:
f <- function(country, statistic.type, data=df) { values <- data[data$country==country & data$statistic.type==statistic.type,-(1:2)] cbind(year=names(df)[-(1:2)], value=values) }
use as
f(country="brazil", statistic.type="death rate")
Comments
Post a Comment