Add row to data frame with dplyr

后端 未结 5 1283
隐瞒了意图╮
隐瞒了意图╮ 2021-02-03 23:15

I have this sample data:

cvar <- c(\"2015-11-01\",\"2015-11-02\",\"All\")
nvar1 <- c(12,10,5)
nvar2 <- c(7,5,6)
data <- cbind.data.frame(cvar,nvar1,n         


        
5条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-03 23:51

    If someone is still looking for an universal solution I would be use:

    cvar <- c("2015-11-01","2015-11-02","All")
    nvar1 <- c(12,10,5)
    nvar2 <- c(7,5,6)
    data <- tibble::tibble(cvar,nvar1,nvar2)
    
    purrr::map_df(data, ~c(.x, ifelse(is.numeric(.x), sum(.x, na.rm=TRUE), NA)))
    

    P.S. I use tibble to keep character because a data frame converts them to factor and base::c "destroy" them

提交回复
热议问题