0

I am working on a data frame with the following columns - country, factor, year, number of technicians.

What i want to achieve: create a new data frame containing only the most recent data (i.e. highest year) for each country (NOTE: there are several rows of data for each country).

I have created a function to isolate the country and year as follows. (NOTE: main dataset = numeric)

#Function to isolate 1 country
one_country1 <- function(x) {
        a <- numeric %>% filter(Factor == x)
        return(a) }

#Function to isolate latest year 
latest_country <- function(y){
        b <- y %>% filter(Year == max(Year))
        return(b) }

#Function to isolate both country and latest year 
best_data <- function(z){
        G <- latest_country(one_country1(z))
        return(G)}

I then made this into a for loop to apply it to each country as follows.

z <- 1
loop_data <- for(z in 1:114){
        print(best_data(z))}

This produces the correct data but it is in a strange format that is not a data frame. When I try 'typeof' it says 'NULL' and I can't seem to convert it into a data frame using simple as.data.frame functions or starting with an empty data frame and incorporating rbind.data.frame into the function. The results appear as follows:

Country Factor Year Technicians
1   Yemen    112 2010        1809
  Country Factor Year Technicians
1  Zambia    113 2012        1126
   Country Factor Year Technicians
1 Zimbabwe    114 2018        1126

typeof(loop_data) = NULL

Any help about how to rework this code to output a data frame would be much appreciated! I've only started leaning R a couple of weeks ago so please forgive how amateurish and untidy the code may be!

Laz
  • 1

1 Answers1

0
Country = df$Country

for (country in Country) {
loop_data <- df%>%
group_by(Country)
filter(Year == max(Year)) 
}

alternatively you could try

for (country in Country) {
    loop_data <- df%>%
    filter(Country == country)%>%
    filter(Year == max(Year)) 
    }

or

for (country in Country) {
    loop_data <- df%>%
    filter(Country == country)%>%
    slice(which.max(Year)) 
    }
ale_ish
  • 159
  • 7