last non blank case from list of columns

Hello,
I intend to perform something like this :

country1=c("USA","PER","JPN","KOR")
country2=c(NA,"ARG","VEN","TAI")
country3=c(NA,"GRE",NA,"BUL")



dataz=data_frame(country1,country2,country3)
str(dataz)
dataz$des=ifelse(!is.na(dataz$country3),dataz$country3,
                    ifelse(!is.na(dataz$country2),dataz$country2,
                    ifelse(!is.na(dataz$country1),dataz$country1,NA)))

desired=c("USA","GRE","VEN","BUL")
desired

As you can see, I used only 3 columns or variables, i mean, using country 1, 2, and 3.
But how can I obtain the same using 10 or more columns? I mean, I would like to declare in a list the variables to treat, like,
list_country=c("country1","country2","country3","country4","country5","country6","country7","country8","country9","country10)

And over that list, write the code. Because, as you imagine, write 10 times ifelse is easy to corrupt.

And, as a mini question, I tried to create the data frame country using data.frame and I get this:

1      USA     <NA>     <NA>   4
2      PER      ARG      GRE   2
3      JPN      VEN     <NA>   3
4      KOR      TAI      BUL   1


'data.frame':	4 obs. of  3 variables:
 $ country1: Factor w/ 4 levels "JPN","KOR","PER",..: 4 3 1 2
 $ country2: Factor w/ 3 levels "ARG","TAI","VEN": NA 1 3 2
 $ country3: Factor w/ 2 levels "BUL","GRE": NA 2 NA 1

what was wrong with that code? I think that treating the vectors as factors messed up the ifelse code. That's whay I employed the dplyr::data_frame. Strange.

Thanks for your time and interest.
Good luck.

my solution is

library(tidyverse)



(dataz <- tibble(country1=c("USA","PER","JPN","KOR"),
                 country2=c(NA,"ARG","VEN","TAI"),
                 country3=c(NA,"GRE",NA,"BUL")))

pivot_longer(dataz %>% mutate(rownum=row_number()),
             cols=c(country1:country3),
             names_to="position",
             values_to = "country") %>% na.omit() %>% 
  group_by(rownum) %>% slice_max(order_by = position) %>% pull(country)
1 Like

Thanks. I'll mark this thread as solved.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.