I have been trying to write a function that finds the mean & median of a single row only and a column.
I have a list of countries and I want to pass on the dataset of a specific country and this will in return give me the mean of that column
country year score
Algeria 1980 -1.1201501
Algeria 1981 -1.0526943
Algeria 1982 -1.0561565
Algeria 1983 -1.1274560
Algeria 1984 -1.1353926
I have tried the below:
output <- function(dataset) {
mean_country <- mean(dataset[country, score])
median_country <- median(dataset[country, score])
return(list(mean_country, median_country)
}
and was expecting to test the function with output(dataset[Algeria, score]) and that it would give me the correct result.
I am aware it can quickly be done using rowMeans or tidyverse but I need to write it as a function and the above doesn't work.
Also, any input regarding the function returning a dataframe instead of a list, would be great.
Thank you in advance.