Tidy method to split multiple columns using tidyr::separate

后端 未结 5 997
别跟我提以往
别跟我提以往 2021-01-14 19:08

I have a data frame like so:

df <- structure(list(A = c(\"3 of 5\", \"1 of 2\", \"1 of 3\", \"1 of 3\", 
\"3 of 4\", \"2 of 7\"), B = c(\"2 of 2\", \"2 of         


        
5条回答
  •  伪装坚强ぢ
    2021-01-14 19:48

    Just another tidyverse way:

    purrr::map_dfc(names(df), function(i) {
    
     df %>% separate(i,
                  sep = "of",
                  remove = T,
                  into = c(paste0(i, "_attempted"), paste0(i, "_landed")))
    
     }) %>% dplyr::select(., contains("_"))
    

提交回复
热议问题